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
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
#include "Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h"
#include "Pokemon_AutoStorySelect_Tests.h"
#include "Pokemon_AutoStoryStats_Tests.h"
#include "Pokemon_Interrupts_Tests.h"
#include "Pokemon_SceneJudges_Tests.h"
#include "Pokemon_ScreenEvents_Tests.h"
#include "Pokemon_StoryEvents_Tests.h"
#include "Pokemon_StoryLog_Tests.h"
#include "Pokemon_StorySavePolicy_Tests.h"
#include "Pokemon_StorySegments_Tests.h"
#include "Pokemon_StoryStepExecutor_Tests.h"
#include "Pokemon_Transitions_Tests.h"
#include "Types/Pokemon_StoryActionArg_Tests.h"

namespace PokemonAutomation{
Expand All @@ -49,6 +52,9 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){
add_tests_StoryEnginePolicy(database);
add_tests_StoryEngineRuns(database);
add_tests_ScreenEvents(database);
add_tests_SceneJudges(database);
add_tests_Interrupts(database);
add_tests_Transitions(database);
}


Expand Down
91 changes: 91 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FadeProfiles.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* Fade Profiles
*
* From: https://github.com/PokemonAutomation/
*
* Per-game fade thresholds (FadeProfile) and the transition kinds the tracker
* reports. Their definitions are in Pokemon_Transitions.cpp.
*
*/

#ifndef PokemonAutomation_Pokemon_FadeProfiles_H
#define PokemonAutomation_Pokemon_FadeProfiles_H

#include <cstdint>
#include <string>
#include "Common/Cpp/Time.h"

namespace PokemonAutomation{
namespace Pokemon{


// Whether the screen differs after a transition from before it. The
// tracker has only frame statistics; the caller compares the frames, or
// says it did not.
enum class SceneChange{
UNKNOWN,
SAME,
DIFFERENT,
};


// A game's fades, as data. Two tiers of the same test: a frame is uniform
// (the fade's hold) when its middle's stddev sum is at most
// `uniform_max_stddev_sum`, and partial (on the way down or up) when it is
// at most `partial_max_stddev_sum`. Either way it is dark or white by its
// rgb sum.
struct FadeProfile{
double uniform_max_stddev_sum = 10;
double partial_max_stddev_sum = 60;
// Dark at or below this rgb sum; a partial frame up to `partial_dark_max_sum`.
double dark_max_sum = 225;
double partial_dark_max_sum = 300;
// White at or above this rgb sum; a partial frame from `partial_white_min_sum`.
double white_min_sum = 500;
double partial_white_min_sum = 450;

// Partial frames for longer than this, with no hold, are the scene's
// own look (a dim cave floor, a street at night), not a fade.
Milliseconds max_fall = Milliseconds(1000);
// A transition still dark or white after this has timed out: the
// screen did not come back.
Milliseconds max_hold = Milliseconds(8000);

// Whether the game fades to white between maps (BDSP's cave exits).
// Without, every white is an encounter's flash.
bool has_white_fade = true;
// A white held this long or less, back on the same scene, is a flash.
Milliseconds flash_max_hold = Milliseconds(150);
// A white held this long or more, not back on the same scene, is a
// white fade. Between the two the white is unsure.
Milliseconds white_fade_min_hold = Milliseconds(400);
};

// BDSP. The uniform tier is the shared walk's (FADE_MAX_STDDEV_SUM, the
// Oreburgh Gate's textured floor is no fade); the partial
// tier is run_into_door()'s 60, which caught the doorway's dim fade frames.
// A cave mouth's fade is dim (45 to 64 a channel), hence the dark sum of 225.
// The flash and white-fade holds are provisional: no console frames of an
// encounter flash and a cave exit have been timed side by side yet, so the
// band between them is wide and reports WHITE_UNSURE.
extern const FadeProfile BDSP_FADE_PROFILE;
// FRLG. The GBA fades to true black, so the dark tiers are the shared
// BlackScreenDetector's 100 and a little above; it has no white fade between
// maps, so every white is an encounter's flash.
extern const FadeProfile FRLG_FADE_PROFILE;


// What a transition was.
enum class TransitionKind : uint8_t{
FADE_BLACK, // A door, a cave mouth, a scene cut, a battle's own fade.
FADE_WHITE, // A cave exit: a long white onto another scene.
FLASH, // An encounter's flash: a short white, back on the scene.
WHITE_UNSURE, // A white that may be either: wait for a battle.
};
// "black fade", for the log.
std::string transition_kind_name(TransitionKind kind);



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
280 changes: 280 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
/* Interrupts
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <algorithm>
#include <chrono>
#include <format>
#include <string_view>
#include "Pokemon_Interrupts.h"

namespace PokemonAutomation{
namespace Pokemon{


std::string warp_judgement_name(WarpJudgement judgement){
switch (judgement){
case WarpJudgement::NO_FADE: return "no fade";
case WarpJudgement::FADE_SHOWING: return "fade still showing";
case WarpJudgement::TOO_SHORT: return "fade too short";
case WarpJudgement::SAME_SCENE: return "same scene after the fade";
case WarpJudgement::WARPED: return "warped";
case WarpJudgement::FLASH: return "an encounter's flash";
case WarpJudgement::WHITE_UNSURE: return "a white, flash or fade";
}
return "?";
}

std::string interrupt_kind_name(InterruptKind kind){
switch (kind){
case InterruptKind::NONE: return "none";
case InterruptKind::WILD_ENCOUNTER: return "wild encounter";
case InterruptKind::TRAINER_SIGHT: return "trainer sight";
case InterruptKind::DIALOG: return "dialog";
case InterruptKind::WARP: return "warp";
case InterruptKind::UNSETTLED: return "unsettled";
}
return "?";
}


namespace{

using Changes = std::vector<ScreenEventChange>;

Changes changes_in(const ScreenEventLog& log, InterruptWindow window){
Changes out;
for (const ScreenEventChange& change : log.changes()){
if (window.start <= change.time && change.time <= window.end){
out.push_back(change);
}
}
return out;
}

bool rose(const ScreenEventChange& change, ScreenEvent event){
return change.event == event && change.edge == ScreenEventEdge::ROSE;
}
bool fade_rose(const ScreenEventChange& change){
return rose(change, ScreenEvent::FADE_BLACK) || rose(change, ScreenEvent::FADE_WHITE);
}

// The first rise of `event`, if any.
std::optional<ScreenEventChange> first_rise(const Changes& changes, ScreenEvent event){
for (const ScreenEventChange& change : changes){
if (rose(change, event)){
return change;
}
}
return std::nullopt;
}

// The last fade that went down, and when it came back up.
std::optional<FadeSpan> last_fade(const Changes& changes){
std::optional<FadeSpan> out;
for (const ScreenEventChange& change : changes){
if (fade_rose(change)){
out = FadeSpan{fade_in({change.event}), change.time, std::nullopt};
}else if (out && !out->up && change.edge == ScreenEventEdge::FELL && fade_events(out->fade).has(change.event)){
out->up = change.time;
}
}
return out;
}

ScreenEvent fade_event(WarpFade fade){
return fade == WarpFade::WHITE ? ScreenEvent::FADE_WHITE : ScreenEvent::FADE_BLACK;
}
void add_fade_evidence(const FadeSpan& fade, Changes& evidence){
evidence.push_back({fade.down, fade_event(fade.fade), ScreenEventEdge::ROSE});
if (fade.up){
evidence.push_back({*fade.up, fade_event(fade.fade), ScreenEventEdge::FELL});
}
}


// A battle, and whose. The battle's fade is the last one that went down
// no more than battle_after_fade before it; a box that rose before that
// fade (or before the battle, with no fade) is a trainer's lines. A box
// after the fade is the battle's own opening text.
std::optional<InterruptVerdict> battle_verdict(const Changes& changes, const InterruptKnobs& knobs){

Check warning on line 102 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::battle_verdict( const Changes & changes , const InterruptKnobs & knobs): function_lines=33 (advisory above 25, limit 50)
const std::optional<ScreenEventChange> battle = first_rise(changes, ScreenEvent::BATTLE_STARTED);
if (!battle){
return std::nullopt;
}
std::optional<ScreenEventChange> fade;
for (const ScreenEventChange& change : changes){
if (change.time > battle->time){
break;
}
if (fade_rose(change) && battle->time - change.time <= knobs.battle_after_fade){
fade = change;
}
}
const WallClock cut = fade ? fade->time : battle->time;
std::optional<ScreenEventChange> lines;
for (const ScreenEventChange& change : changes){
if (change.time >= cut){
break;
}
if (rose(change, ScreenEvent::DIALOG_BOX) && cut - change.time <= knobs.sight_lines_lead){
lines = change;
break;
}
}
InterruptVerdict out{lines ? InterruptKind::TRAINER_SIGHT : InterruptKind::WILD_ENCOUNTER, {}, false};
for (const std::optional<ScreenEventChange>& change : {lines, fade, battle}){
if (change){
out.evidence.push_back(*change);
}
}
return out;
}

// A warp: a banner, or a fade back up with no battle after it for
// battle_after_fade. A fade still showing, or back up too recently, may
// yet be an encounter's flash.
std::optional<InterruptVerdict> warp_verdict(

Check warning on line 139 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::warp_verdict( const ScreenEventLog & log , InterruptWindow window , const Changes & changes , const InterruptKnobs & knobs): function_lines=36 (advisory above 25, limit 50)
const ScreenEventLog& log, InterruptWindow window, const Changes& changes, const InterruptKnobs& knobs
){
const WarpVerdict warp = judge_warp(log, window, knobs, SceneChange::UNKNOWN);
const std::optional<ScreenEventChange> banner = first_rise(changes, ScreenEvent::LOCATION_BANNER);
InterruptVerdict out{InterruptKind::WARP, {}, false};
if (warp.fade && warp.judgement != WarpJudgement::TOO_SHORT){
add_fade_evidence(*warp.fade, out.evidence);
}
if (banner){
out.evidence.push_back(*banner);
std::stable_sort(out.evidence.begin(), out.evidence.end(), [](const auto& a, const auto& b){
return a.time < b.time;
});
return out;
}
switch (warp.judgement){
case WarpJudgement::FADE_SHOWING:
out.kind = InterruptKind::UNSETTLED;
return out;
case WarpJudgement::WARPED:{
const WallClock seen_until = std::min(window.end, log.last_frame().value_or(window.end));
if (seen_until - *warp.fade->up < knobs.battle_after_fade){
out.kind = InterruptKind::UNSETTLED;
}
return out;
}
case WarpJudgement::NO_FADE:
case WarpJudgement::TOO_SHORT:
case WarpJudgement::SAME_SCENE:
case WarpJudgement::FLASH:
case WarpJudgement::WHITE_UNSURE:
break;
}
return std::nullopt;
}

std::optional<InterruptVerdict> dialog_verdict(const Changes& changes){
const std::optional<ScreenEventChange> box = first_rise(changes, ScreenEvent::DIALOG_BOX);
if (!box){
return std::nullopt;
}
return InterruptVerdict{InterruptKind::DIALOG, {*box}, false};
}

} // namespace


WarpVerdict judge_warp(
const ScreenEventLog& log, InterruptWindow window,
const InterruptKnobs& knobs, SceneChange change
){
WarpVerdict out{WarpJudgement::NO_FADE, last_fade(changes_in(log, window))};
if (!out.fade){
return out;
}
if (!out.fade->up){
out.judgement = WarpJudgement::FADE_SHOWING;
}else if (*out.fade->up - out.fade->down < knobs.min_fade_hold){
out.judgement = WarpJudgement::TOO_SHORT;
}else if (change == SceneChange::SAME){
out.judgement = WarpJudgement::SAME_SCENE;
}else{
out.judgement = WarpJudgement::WARPED;
}
return out;
}

WarpVerdict judge_warp(const Transition& transition, const InterruptKnobs& knobs){
const WarpFade fade = transition.kind == TransitionKind::FADE_BLACK ? WarpFade::BLACK : WarpFade::WHITE;
WarpVerdict out{WarpJudgement::WARPED, FadeSpan{fade, transition.start, transition.end}};
if (!transition.end){
out.judgement = WarpJudgement::FADE_SHOWING;
}else if (transition.kind == TransitionKind::FLASH){
out.judgement = WarpJudgement::FLASH;
}else if (transition.change == SceneChange::SAME){
out.judgement = WarpJudgement::SAME_SCENE;
}else if (transition.kind == TransitionKind::WHITE_UNSURE){
out.judgement = WarpJudgement::WHITE_UNSURE;
}else if (transition.peak && transition.change != SceneChange::DIFFERENT && transition.hold() < knobs.min_fade_hold){
out.judgement = WarpJudgement::TOO_SHORT;
}
return out;
}


std::string describe(const InterruptVerdict& verdict, WallClock origin){
std::string out = interrupt_kind_name(verdict.kind);
std::string_view separator = ": ";
for (const ScreenEventChange& change : verdict.evidence){
out += std::format(
"{}{:+} ms {} {}",
separator,
std::chrono::duration_cast<Milliseconds>(change.time - origin).count(),
screen_event_name(change.event),
change.edge == ScreenEventEdge::ROSE ? "rose" : "fell"
);
separator = ", ";
}
if (verdict.log_truncated){
out += " (log truncated)";
}
return out;
}


InterruptVerdict classify_interrupt(
const ScreenEventLog& log, InterruptWindow window, const InterruptKnobs& knobs
){
const Changes changes = changes_in(log, window);
std::optional<InterruptVerdict> verdict = battle_verdict(changes, knobs);
if (!verdict){
verdict = warp_verdict(log, window, changes, knobs);
}
if (!verdict){
verdict = dialog_verdict(changes);
}
InterruptVerdict out = verdict.value_or(InterruptVerdict{});
out.log_truncated = log.dropped() > 0 &&
(log.changes().empty() || window.start <= log.changes().front().time);
return out;
}


std::optional<WallClock> interrupt_step_time(const InterruptVerdict& verdict, const std::vector<EventLag>& lags){
for (const ScreenEventChange& change : verdict.evidence){
if (change.edge != ScreenEventEdge::ROSE){
continue;
}
for (const EventLag& lag : lags){
if (lag.event == change.event){
return change.time - lag.lag;
}
}
}
return std::nullopt;
}



} // namespace Pokemon
} // namespace PokemonAutomation
Loading
Loading