Skip to content
Merged
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
102 changes: 102 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPreflight.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* AutoStory Story Preflight
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <array>
#include <chrono>
#include <string>
#include "Common/Cpp/Time.h"
#include "CommonFramework/ImageTools/FloatPixel.h"
#include "CommonFramework/ImageTools/ImageBoxes.h"
#include "CommonFramework/ImageTools/ImageStats.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "CommonTools/Images/SolidColorTest.h"
#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h"
#include "Pokemon_StoryPreflight.h"

namespace PokemonAutomation{
namespace Pokemon{

using namespace std::chrono_literals;


namespace{

// Color bars are seven flat vertical stripes of different colors.
constexpr size_t BAR_COUNT = 7;
// A stripe flatter than this (stddev sum) is one solid color.
constexpr double BAR_MAX_STDDEV = 30;
// Two stripes this far apart (RGB distance) are different bars.
constexpr double BAR_MIN_DISTANCE = 60;
// A frame older than this is a stalled feed.
constexpr auto FRAME_MAX_AGE = 2000ms;

bool shows_color_bars(const ImageViewRGB32& frame){
std::array<FloatPixel, BAR_COUNT> bars;
for (size_t c = 0; c < BAR_COUNT; c++){
const double width = 1.0 / BAR_COUNT;
const ImageFloatBox stripe(c * width + width * 0.25, 0.1, width * 0.5, 0.5);
const ImageStats stats = image_stats(extract_box_reference(frame, stripe));
if (stats.stddev.sum() > BAR_MAX_STDDEV){
return false;
}
bars[c] = stats.average;
}
for (size_t c = 1; c < BAR_COUNT; c++){
if (euclidean_distance(bars[c - 1], bars[c]) < BAR_MIN_DISTANCE){
return false;
}
}
return true;
}

bool frame_shows_picture(const ImageViewRGB32& frame){
if (!frame){
return false;
}
if (is_black(frame)){
return false;
}
return !shows_color_bars(frame);
}

} // namespace

StoryPreflight StoryPreflight::read(NintendoSwitch::ConsoleHandle& console, NintendoSwitch::ProControllerContext& context){
StoryPreflight preflight;
preflight.controller_ready = context.controller().is_ready();
const VideoSnapshot snapshot = console.video().snapshot();
const bool fresh = snapshot && current_time() - snapshot.timestamp < FRAME_MAX_AGE;
preflight.video_alive = fresh && frame_shows_picture(snapshot);
preflight.game_running = preflight.video_alive
&& !NintendoSwitch::HomeMenuDetector(console, COLOR_RED).detect(snapshot);
return preflight;
}


Outcome StoryPreflight::check() const{
std::string failed;
auto add = [&failed](bool ok, std::string_view what){
if (ok){
return;
}
if (!failed.empty()){
failed += "; ";
}
failed += what;
};
add(video_alive, "no live video");
add(controller_ready, "the controller is not ready");
add(game_running, "the game is not running");
if (failed.empty()){
return outcome_ok();
}
return outcome_failed(std::string(FAILURE), failed);
}



} // namespace Pokemon
} // namespace PokemonAutomation
50 changes: 50 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPreflight.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* AutoStory Story Preflight
*
* From: https://github.com/PokemonAutomation/
*
* What the story run's preflight hook (StoryGameHooks::preflight) reads
* off the console before anything is pressed, and its verdict. The same
* for every game, so BDSP and FRLG lend the same reader. The start
* sequence that runs it is in Pokemon_StoryRun.h.
*
*/

#ifndef PokemonAutomation_Pokemon_StoryPreflight_H
#define PokemonAutomation_Pokemon_StoryPreflight_H

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

namespace PokemonAutomation{
namespace Pokemon{


// What a game reads off the console before a run presses anything.
struct StoryPreflight{
// The failure of a preflight that failed: the console or its setup,
// not the story. Nothing answers it with a reload or a recovery.
static constexpr std::string_view FAILURE = "precondition_failed";

// Frames are arriving and show a picture (not black, not color bars).
bool video_alive = false;
bool controller_ready = false;
// The game is on screen, not the Switch's HOME menu.
bool game_running = false;

// outcome_ok() when every check passed, else FAILURE naming each
// check that failed.
Outcome check() const;

// Reads the console: a frame newer than two seconds that shows a
// picture (not black, not the capture card's color bars), the
// controller ready, and that frame not the HOME menu. Presses nothing.
static StoryPreflight read(NintendoSwitch::ConsoleHandle& console, NintendoSwitch::ProControllerContext& context);
};



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
30 changes: 30 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ namespace Pokemon{

namespace{

// "its save", "a new game", "the game as it is": for the start line.
std::string_view story_start_name(StoryStart start){
switch (start){
case StoryStart::NEW_GAME:
return "a new game";
case StoryStart::LIVE:
return "the game as it is";
case StoryStart::FROM_SAVE:
return "its save";
}
return "?";
}

void log_line(const std::string& line, StoryGameHooks& game){
if (game.log){
game.log(line);
Expand All @@ -37,6 +50,18 @@ std::string story_verify_summary(std::string_view checkpoint, const VerifyPolicy
);
}

Outcome run_story_preflight(StoryGameHooks& game){
if (!game.preflight){
log_line("[Story] preflight: not checked: the game has no preflight", game);
return outcome_ok();
}
const Outcome outcome = game.preflight().check();
if (!outcome.succeeded()){
log_line(std::format("[Story] preflight failed, nothing pressed: {}", outcome.detail), game);
}
return outcome;
}

Outcome load_story_save(std::string_view reason, StoryGameHooks& game, RunState& state){
log_line(std::format("[Story] {}: loading the save", reason), game);
Outcome outcome = game.load_save(state);
Expand All @@ -58,6 +83,11 @@ Outcome load_story_save(std::string_view reason, StoryGameHooks& game, RunState&
} // namespace

Outcome prepare_story_start(std::string_view checkpoint, StoryStart start, StoryGameHooks& game, RunState& state){
log_line(std::format("[Story] start: checkpoint {}, from {}", checkpoint, story_start_name(start)), game);
const Outcome preflight = run_story_preflight(game);
if (!preflight.succeeded()){
return preflight;
}
switch (start){
case StoryStart::NEW_GAME:
log_line(std::format("[Story] {}: starts a new game, no save to load", checkpoint), game);
Expand Down
36 changes: 22 additions & 14 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRun.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* AutoStory Story Run

Check warning on line 1 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRun.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<header>: header_symbols=7 (advisory above 6, limit 8)
*
* From: https://github.com/PokemonAutomation/
*
Expand All @@ -8,9 +8,19 @@
* A game lends only what really differs: how it loads its save (BDSP
* resets from HOME, FRLG soft-resets) and how it checks where the save is.
*
* Two rules hold for every game:
* These rules hold for every game:
* - Before anything is pressed, the game's preflight must see the video
* alive, the controller ready and the game running (not HOME). One
* that fails is the setup's failure, not the story's: nothing is
* touched.
* - A run that starts at a checkpoint with a save loads that save first,
* so it never walks from wherever the game was left.
* so it never walks from wherever the game was left. There is no
* "just loaded" shortcut: a load or place check that fails stops the
* run before anything is saved.
* - The first checkpoint's own save at its start runs only for a new
* game: after a load the save is already there, and from the game as
* it is the place is unconfirmed (console B, 2026-11-11: a run on the
* HOME menu "saved" first).
* - A failure leaves the game where it failed. Nothing here closes the
* game; the user (or the next run from the save) resumes from there.
*
Expand All @@ -26,23 +36,18 @@
#include "Pokemon_StoryCheckpointTypes.h"
#include "Pokemon_StoryOutcome.h"
#include "Pokemon_StorySegments.h"
#include "Pokemon_StoryPreflight.h"
#include "Types/Pokemon_StoryStart.h"

namespace PokemonAutomation{
namespace Pokemon{


// How the game reaches the start checkpoint.
enum class StoryStart{
// A new game from the title screen: there is no save.
NEW_GAME,
// The game as another program left it: nothing is loaded.
LIVE,
// The checkpoint's save, loaded first.
FROM_SAVE,
};

// What a game lends the story run.
struct StoryGameHooks{
// Reads the console before anything else (see StoryPreflight). Without
// it the run logs that nothing was checked.
MoveOnlyFunction<StoryPreflight()> preflight;
// Loads the last save: the game reset (from HOME or soft) and back in
// the overworld. A failed Outcome says why.
Act load_save;
Expand All @@ -63,8 +68,11 @@
inline constexpr std::string_view STORY_LEFT_WHERE_FAILED = "AutoStory failed: the game stays where it failed.";

// Gets the game to where checkpoint `checkpoint` starts, as `start` says.
// FROM_SAVE always loads the save (logging "[Story] 00.04b: loading the
// save"), checks its place and rereads it, and sets the "save_loaded" flag.
// First logs the checkpoint starting ("[Story] start: checkpoint 00.04b,
// from its save") and runs the game's preflight; one that fails returns
// StoryPreflight::FAILURE with nothing pressed. FROM_SAVE then always
// loads the save (logging "[Story] 00.04b: loading the save"), checks its
// place and rereads it, and sets the "save_loaded" flag.
Outcome prepare_story_start(std::string_view checkpoint, StoryStart start, StoryGameHooks& game, RunState& state);

// The runner's hooks: reload is load_story_save(); save, log and status
Expand Down
Loading
Loading