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
19 changes: 19 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ StoryData<Checkpoint> load_story_data(
template <typename Checkpoint>
std::vector<Checkpoint> story_run_rows(std::span<const Checkpoint> rows, size_t first, size_t last);

// `rows` cut where a run reloads: a new segment starts at each row after
// the first that `story` marks reload_when_chained, and the run loads that
// row's start save (the autosave the row before ended on) before it. No
// segments for no rows.
template <typename Checkpoint>
std::vector<std::vector<Checkpoint>> story_run_segments(std::span<const Checkpoint> rows, const StoryRows& story);



namespace detail{
Expand Down Expand Up @@ -110,6 +117,18 @@ std::vector<Checkpoint> story_run_rows(std::span<const Checkpoint> rows, size_t
return std::vector<Checkpoint>(rows.begin() + first, rows.begin() + last + 1);
}

template <typename Checkpoint>
std::vector<std::vector<Checkpoint>> story_run_segments(std::span<const Checkpoint> rows, const StoryRows& story){
std::vector<std::vector<Checkpoint>> segments;
for (const Checkpoint& row : rows){
if (segments.empty() || story.reload_when_chained.contains(row.id)){
segments.emplace_back();
}
segments.back().push_back(row);
}
return segments;
}



} // namespace Pokemon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,72 @@ UnitTestResult check_rows(const RowsCase& test){
}


struct SegmentsCase{
std::string name;
struct Input{
std::vector<std::string> rows;
std::set<std::string, std::less<>> reload_when_chained = {};
};
Input input;
struct Expectation{
// The segments' ids, " | " between segments.
std::string segments;
};
Expectation expectation;
};

const SegmentsCase SEGMENTS_CASES[] = {
{
.name = "no reload: one segment",
.input = {.rows = {"01.00", "01.01", "01.02"}},
.expectation = {.segments = "01.00 01.01 01.02"},
},
{
.name = "a reload before a row in the middle",
.input = {.rows = {"01.00", "01.01", "01.02"}, .reload_when_chained = {"01.02"}},
.expectation = {.segments = "01.00 01.01 | 01.02"},
},
{
.name = "a reload before every row after the first",
.input = {.rows = {"01.01", "01.02", "01.03"}, .reload_when_chained = {"01.02", "01.03"}},
.expectation = {.segments = "01.01 | 01.02 | 01.03"},
},
{
.name = "the run's first row does not start a second segment",
.input = {.rows = {"01.02", "01.03"}, .reload_when_chained = {"01.02"}},
.expectation = {.segments = "01.02 01.03"},
},
{
.name = "no rows, no segments",
.input = {.rows = {}, .reload_when_chained = {"01.02"}},
.expectation = {.segments = ""},
},
};

UnitTestResult check_segments(const SegmentsCase& test){
std::vector<StoryCheckpoint> rows;
for (const std::string& id : test.input.rows){
StoryCheckpoint row;
row.id = id;
rows.push_back(std::move(row));
}
StoryRows story;
story.reload_when_chained = test.input.reload_when_chained;
std::vector<std::string> segments;
for (const std::vector<StoryCheckpoint>& segment : story_run_segments<StoryCheckpoint>(rows, story)){
segments.push_back(joined(ids(segment)));
}
std::string got;
for (const std::string& segment : segments){
got += (got.empty() ? "" : " | ") + segment;
}
if (got != test.expectation.segments){
return std::format("expected \"{}\", got \"{}\"", test.expectation.segments, got);
}
return true;
}


template <typename Case>
class StoryFilesTest : public UnitTest{
public:
Expand Down Expand Up @@ -258,6 +324,9 @@ void add_tests_StoryFiles(UnitTestDatabase& database){
for (const RowsCase& test : ROWS_CASES){
database.add<StoryFilesTest<RowsCase>>("rows", test, check_rows);
}
for (const SegmentsCase& test : SEGMENTS_CASES){
database.add<StoryFilesTest<SegmentsCase>>("segments", test, check_segments);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ void StoryEngine<PoseType>::save_if_passed(const StoryCheckpoint& checkpoint, St
}
const Outcome saved = m_game.save(m_backend, checkpoint);
report.saved = saved.succeeded();
m_backend.log(std::format("[Save] {}: {}", checkpoint.id, report.saved ? "saved" : std::format("save failed: {}", to_string(saved))));
if (report.saved){
m_backend.log(std::format("[Save] {}: saved", checkpoint.id));
}else if (saved.failure == NOT_SAVED){
m_backend.log(std::format("[Save] {}: not saved ({})", checkpoint.id, saved.detail));
}else{
m_backend.log(std::format("[Save] {}: save failed: {}", checkpoint.id, to_string(saved)));
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ StoryAction<ZonePose> game_action(std::string name, StoryActionHandler<ZonePose>
};
}

// The test game. Its save saves the checkpoints of `autosaves`; its
// reload works unless `reload_fails`.
StoryGame<ZonePose> test_game(std::set<std::string> autosaves, bool reload_fails, TestWorld& world){
// The test game. Its save saves the checkpoints of `autosaves`, fails on
// those of `save_failures` and does not save at the others; its reload
// works unless `reload_fails`.
StoryGame<ZonePose> test_game(
std::set<std::string> autosaves, std::set<std::string> save_failures, bool reload_fails, TestWorld& world
){
StoryGame<ZonePose> game;
game.id = "test";
game.actions.push_back(game_action("ok", [](StoryBackend<ZonePose>&, const StoryActionCall&, RunState&){
Expand Down Expand Up @@ -78,8 +81,13 @@ StoryGame<ZonePose> test_game(std::set<std::string> autosaves, bool reload_fails
}
return outcome_failed("no_fade", "no white fade in 3 slices");
}));
game.save = [autosaves = std::move(autosaves)](StoryBackend<ZonePose>&, const StoryCheckpoint& checkpoint){
return autosaves.contains(checkpoint.id) ? outcome_ok() : outcome_failed("no_autosave");
game.save = [autosaves = std::move(autosaves), save_failures = std::move(save_failures)](
StoryBackend<ZonePose>&, const StoryCheckpoint& checkpoint
){
if (autosaves.contains(checkpoint.id)){
return outcome_ok();
}
return save_failures.contains(checkpoint.id) ? outcome_failed("save_error", "the disk is full") : outcome_not_saved("no autosave here");
};
game.reload = [reload_fails](StoryBackend<ZonePose>& backend){
backend.press(NintendoSwitch::BUTTON_HOME, /*hold=*/80ms, /*release=*/80ms);
Expand Down Expand Up @@ -135,6 +143,7 @@ struct SegmentCase{
std::vector<StoryCheckpoint> checkpoints;
size_t attempts = 1;
std::set<std::string> autosaves = {};
std::set<std::string> save_failures = {};
bool reload_fails = false;
std::vector<Observation<ZonePose>> looks = {};
StoryBackendKind kind = StoryBackendKind::SCRIPT;
Expand All @@ -157,6 +166,16 @@ const SegmentCase SEGMENT_CASES[] = {
.input = {.checkpoints = {checkpoint_of("01.00", {"ok", "press_a"}), checkpoint_of("01.01", {"ok"})}, .autosaves = {"01.00", "01.01"}},
.expectation = {.summary = "done | runs 01.00:pass/1 01.01:pass/1 | reloads 0", .inputs = "press A", .line = "[Save] 01.00: saved"},
},
{
.name = "a checkpoint the game does not save at is not saved, with the reason",
.input = {.checkpoints = {checkpoint_of("01.00", {"ok"})}},
.expectation = {.summary = "done | runs 01.00:pass/1 | reloads 0", .inputs = "", .line = "[Save] 01.00: not saved (no autosave here)"},
},
{
.name = "a save that fails is logged as failed",
.input = {.checkpoints = {checkpoint_of("01.00", {"ok"})}, .save_failures = {"01.00"}},
.expectation = {.summary = "done | runs 01.00:pass/1 | reloads 0", .inputs = "", .line = "[Save] 01.00: save failed: save_error: the disk is full"},
},
{
.name = "a failed step stops the segment with its event",
.input = {.checkpoints = {checkpoint_of("01.00", {"ok", "fail", "press_a"})}},
Expand Down Expand Up @@ -220,7 +239,7 @@ const SegmentCase SEGMENT_CASES[] = {

UnitTestResult check_segment(const SegmentCase& test, CancellableScope& scope){
TestWorld world;
const StoryGame<ZonePose> game = test_game(test.input.autosaves, test.input.reload_fails, world);
const StoryGame<ZonePose> game = test_game(test.input.autosaves, test.input.save_failures, test.input.reload_fails, world);
FakeStoryBackend<ZonePose> backend({.looks = test.input.looks, .kind = test.input.kind}, scope);
StoryEngine<ZonePose> engine(game, backend);
RunState state;
Expand Down
16 changes: 14 additions & 2 deletions SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include <functional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h"
#include "Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h"
Expand Down Expand Up @@ -45,6 +47,15 @@ struct StoryAction{
StoryActionHandler<PoseType> handler = {};
};

// The failure of a game's save at a checkpoint's end that the game does
// not save at, or not yet known to: no error, the engine logs it as
// "not saved (<detail>)".
inline constexpr std::string_view NOT_SAVED = "not_saved";
// NOT_SAVED, `reason` its detail: "save unverified".
inline Outcome outcome_not_saved(std::string reason){
return outcome_failed(std::string(NOT_SAVED), std::move(reason));
}

// A game, for the engine.
template <StoryPose PoseType>
struct StoryGame{
Expand All @@ -53,8 +64,9 @@ struct StoryGame{
// The game's actions. A kind's first is its default, the action of a
// step that names none.
std::vector<StoryAction<PoseType>> actions = {};
// The game's save (a wait for an autosave) at the end of `checkpoint`.
// Optional: with none, nothing saves.
// The game's save (a wait for an autosave) at the end of `checkpoint`;
// outcome_not_saved() where it does not save. Optional: with none,
// nothing saves.
std::function<Outcome(StoryBackend<PoseType>& backend, const StoryCheckpoint& checkpoint)> save = {};
// Back to the last save. A succeeded reload that knows where it left
// the player returns that pose in its Outcome. Optional: with none, a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Inference/PokemonLZA_SelectionArrowDetector.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector_Tests.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.h"
#include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.h"
Expand All @@ -47,6 +48,7 @@ void add_tests(UnitTestDatabase& database){
add_tests_OverworldPartySelectionDetector(database);
add_tests_SelectionArrowDetector(database);
add_tests_AutoStoryDetectors(database);
add_tests_AutoStoryDialogChoiceBox(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
Expand Up @@ -197,6 +197,18 @@ ImageRGB32 mission_banner_frame(size_t dx, BannerIcon icon){
return image;
}

// The white title card after the plaza's white fade ("POKEMON LEGENDS:
// Z-A" centered in black, x 450-1450, y 520-560): 01.01's end, where a
// rerun from 01.01 finds the game before it reloads. Each letter a black
// block 30 px wide, 20 px apart.
ImageRGB32 title_card_frame(){
ImageRGB32 image = solid_frame(0xffffffff);
for (size_t x = 450; x + 30 <= 1450; x += 50){
fill_box(x, /*min_y=*/520, x + 30, /*max_y=*/560, /*color=*/0xff000000, image);
}
return image;
}

constexpr uint32_t WHITE = 0xffffffff;
constexpr uint32_t BLACK = 0xff000000;
constexpr uint32_t GRAY = 0xff808080;
Expand Down Expand Up @@ -232,7 +244,12 @@ const SyntheticFrameCase SYNTHETIC_FRAME_CASES[] = {
{
.name = "a white frame",
.input = {.frame = []{ return solid_frame(WHITE); }},
.expectation = {.reads = {.autosave = false, .advance_arrow = false}},
.expectation = {.reads = {.platform = false, .autosave = false, .advance_arrow = false}},
},
{
.name = "the white title card is not the platform",
.input = {.frame = title_card_frame},
.expectation = {.reads = {.platform = false}},
},
{
.name = "a gray frame",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* PokemonLZA AutoStory - Dialog choice box
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "CommonFramework/ImageTools/ImageStats.h"
#include "PokemonLZA_AutoStoryDialogChoiceBox.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


namespace{

// Rows read from the bottom up before the reader stops looking.
constexpr size_t MAX_CHOICE_ROWS = 6;
// A menu has two rows at least. One white strip alone is not one: light
// pavement reads as a highlighted row (ArrivalToAd 0038).
constexpr size_t MIN_CHOICE_ROWS = 2;

// The row pitch and the bottom row's strips at 1920 x 1080. Measured on
// the console on the tourist's two rows (Traditional Chinese)
// and the Research Lab elevator's three (Japanese): the panel x 1408 to
// 1728 ends at y 808 in both, its highlight x 1418 to 1715, the bottom
// row's from y 732 to 800, each row above 80 higher. The text starts at
// x 1443 on every row.
constexpr double ROW_PITCH = 80.0 / 1080;
constexpr double STRIP_TOP = 748.0 / 1080;
constexpr double STRIP_HEIGHT = 40.0 / 1080;

// Right of the row's text, inside the highlight.
ImageFloatBox right_strip(size_t from_bottom){
return ImageFloatBox(1650.0 / 1920, STRIP_TOP - ROW_PITCH * from_bottom, 55.0 / 1920, STRIP_HEIGHT);
}

// Where every row's text starts.
ImageFloatBox text_strip(size_t from_bottom){
return ImageFloatBox(1445.0 / 1920, STRIP_TOP - ROW_PITCH * from_bottom, 80.0 / 1920, STRIP_HEIGHT);
}

enum class ChoiceSlot{
HIGHLIGHTED,
PLAIN,
OTHER,
};

// A slot's right strip: the highlighted row 255 white, flat; a plain row
// the panel's dark blue, (35-45, 60-73, 108-120), spread 4-5. Its text
// strip: spread 220-240 on every row. Above the elevator's top row, the
// wall is as dark and blue, (23-24, 58-60, 102-104), but its text strip
// is flat (spread 5); above the tourist's, pavement, spread 34-36.
ChoiceSlot classify_slot(const ImageViewRGB32& frame, size_t from_bottom){
const ImageStats right = image_stats(extract_box_reference(frame, right_strip(from_bottom)));
if (right.count == 0 || right.stddev.sum() > 30){
return ChoiceSlot::OTHER;
}
const FloatPixel& average = right.average;
if (average.r >= 200 && average.g >= 200 && average.b >= 200){
return ChoiceSlot::HIGHLIGHTED;
}
const bool dark_blue =
average.r <= 70 && average.g <= 100 &&
average.b >= 80 && average.b <= 150 &&
average.b >= average.r + 40;
if (!dark_blue){
return ChoiceSlot::OTHER;
}
const ImageStats text = image_stats(extract_box_reference(frame, text_strip(from_bottom)));
if (text.count == 0 || text.stddev.sum() < 100){
return ChoiceSlot::OTHER;
}
return ChoiceSlot::PLAIN;
}

} // namespace


const ImageFloatBox& dialog_choice_box(){
static const ImageFloatBox box{0.654308, 0.481553, 0.295529, 0.312621};
return box;
}

std::optional<DialogChoiceRows> read_dialog_choice_rows(const ImageViewRGB32& frame){
if (!frame || frame.width() < 192 || frame.height() < 108){
return std::nullopt;
}
size_t count = 0;
std::optional<size_t> highlighted_from_bottom;
for (; count < MAX_CHOICE_ROWS; count++){
const ChoiceSlot slot = classify_slot(frame, count);
if (slot == ChoiceSlot::OTHER){
break;
}
if (slot == ChoiceSlot::HIGHLIGHTED){
if (highlighted_from_bottom){
return std::nullopt;
}
highlighted_from_bottom = count;
}
}
if (!highlighted_from_bottom || count < MIN_CHOICE_ROWS){
return std::nullopt;
}
return DialogChoiceRows{.count = count, .selected = count - 1 - *highlighted_from_bottom};
}



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