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 @@ -53,7 +53,7 @@



UnitTestDatabase make_UNIT_TESTS_ALL(){

Check warning on line 56 in SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::ComputerPrograms::make_UNIT_TESTS_ALL(): function_lines=45 (advisory above 25, limit 50)
UnitTestDatabase ret;

add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal());
Expand All @@ -78,6 +78,7 @@
Pokemon::add_tests_MapData(ret);
Pokemon::add_tests_RuntimeMap(ret);
Pokemon::add_tests_RouteLog(ret);
Pokemon::add_tests_ScreenEvents(ret);
Pokemon::add_tests_StoryEvents(ret);
Pokemon::add_tests_StorySegments(ret);
Pokemon::add_tests_StorySteps(ret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace PokemonAutomation{
namespace Pokemon{


// Registers unit tests for the shared screen events and the judgements over them.
void add_tests_ScreenEvents(UnitTestDatabase& database);
// Registers unit tests for story event sequencing and execution.
void add_tests_StoryEvents(UnitTestDatabase& database);
// Registers unit tests for story segment running, verification, and branching.
Expand Down
85 changes: 85 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* Scene Judges
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "Pokemon_SceneJudges.h"

namespace PokemonAutomation{
namespace Pokemon{


WarpSeen note_warp(WarpSeen seen, const ScreenEvents& events, const std::string& banner_read){
if (seen.fade == WarpFade::NONE){
seen.fade = fade_in(events);
}
if (seen.banner.empty() && events.has(ScreenEvent::LOCATION_BANNER)){
seen.banner = banner_read;
}
return seen;
}

std::string describe(const WarpSeen& seen){
if (!seen.any()){
return "nothing";
}
std::string out = seen.fade == WarpFade::NONE ? "" : warp_fade_name(seen.fade);
if (!seen.banner.empty()){
out += (out.empty() ? "banner \"" : ", banner \"") + seen.banner + "\"";
}
return out;
}


SceneMove TalkOver::look(const ScreenEvents& look, WallClock now){

Check warning on line 35 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::TalkOver::look( const ScreenEvents & look , WallClock now): function_lines=31 (advisory above 25, limit 50)
if (look.has(ScreenEvent::BATTLE_STARTED)){
m_quiet_since = WallClock::max();
m_calm_since = WallClock::max();
return SceneMove::BATTLE;
}
if (fade_in(look) != WarpFade::NONE){
m_fade = fade_in(look);
}
m_banner |= look.has(ScreenEvent::LOCATION_BANNER);
const bool box = look.has(ScreenEvent::DIALOG_BOX);
const bool calm = !box && look.has(ScreenEvent::PLAYER_CAN_MOVE) && warped();
const bool quiet = calm && look.has(ScreenEvent::SCREEN_STILL);
if (!calm){
m_calm_since = WallClock::max();
}else if (m_calm_since == WallClock::max()){
m_calm_since = now;
}
if (!quiet){
m_quiet_since = WallClock::max();
}else if (m_quiet_since == WallClock::max()){
m_quiet_since = now;
}
const bool over =
(quiet && now - m_quiet_since >= TALK_OVER_QUIET) ||
(calm && now - m_calm_since >= TALK_OVER_CALM);
if (over && m_rule.end_on_quiet){
return SceneMove::DONE;
}
return box ? SceneMove::NEXT : SceneMove::B;
}

bool TalkOver::warped() const{
return !m_rule.warp || m_fade != WarpFade::NONE || m_banner;
}

std::string TalkOver::describe() const{
if (m_fade == WarpFade::NONE && !m_banner){
return "no fade or banner";
}
std::string out = m_fade == WarpFade::NONE ? "" : warp_fade_name(m_fade);
if (m_banner){
out += out.empty() ? "banner read" : ", banner read";
}
return out;
}



} // namespace Pokemon
} // namespace PokemonAutomation
99 changes: 99 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* Scene Judges
*
* From: https://github.com/PokemonAutomation/
*
* Judgements a story step makes from screen events (Pokemon_ScreenEvents.h)
* over time, not from one look (#268's design note): a warp is a fade or
* the banner of the map arrived on, and a talk or a scene is over when the
* overworld has been quiet for a while. Pure: tests drive them with
* scripted scenes, and every game shares them.
*
*/

#ifndef PokemonAutomation_Pokemon_SceneJudges_H
#define PokemonAutomation_Pokemon_SceneJudges_H

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

namespace PokemonAutomation{
namespace Pokemon{


// What a walk or a step saw of a warp (#268): a fade or the location
// banner of the map arrived on. Either one alone is the warp.
struct WarpSeen{
WarpFade fade = WarpFade::NONE;
// The banner read, "" if none.
std::string banner = {};

bool any() const{
return fade != WarpFade::NONE || !banner.empty();
}
};
// `seen` after a frame's events: their fade, and `banner_read` when they
// show LOCATION_BANNER. The first of each counts.
WarpSeen note_warp(WarpSeen seen, const ScreenEvents& events, const std::string& banner_read);
// How it was seen, for the log: a black fade, banner "Route 203".
std::string describe(const WarpSeen& seen);


// What to do after a look.
enum class SceneMove{
NEXT, // A box shows: press the talk's button.
B, // No box: press B. It never starts a talk on the overworld
// (A would, and answers Yes on a prompt).
BATTLE, // A battle started.
DONE, // The talk or scene is over.
};

// What ends a talk-over, besides the quiet.
struct TalkOverRule{
// A map change must come first: a fade or the banner. Only the quiet
// after it counts: r30's scene went quiet for 5 s before the fade home.
bool warp = false;
// False when a battle is to follow (a trainer's lines pause for more
// than 4 s before it, r210d): no quiet ends the talk then.
bool end_on_quiet = true;
};

// The overworld with no box and a still screen this long ends it: a scene
// pauses while someone walks off (Dawn and Barry after Rowan leaves, r23),
// a scripted walk-in ran 5 s with no text before Mom spoke (r31), and
// Roark's badge speech pauses for the fanfare (lab-B 2026-10-31 09:45 and
// 2026-11-08 15:06). The clock is the last look that was not quiet.
constexpr Milliseconds TALK_OVER_QUIET = Milliseconds(5000);
// The overworld with no box this long ends it, still or not: wanderers or
// grass can keep the screen from ever being still.
constexpr Milliseconds TALK_OVER_CALM = Milliseconds(15000);

// When a talk or a scene is over, from looks alone. A look is a frame's
// events: DIALOG_BOX, PLAYER_CAN_MOVE, SCREEN_STILL, a fade,
// LOCATION_BANNER (the expected map's) and BATTLE_STARTED are read.
class TalkOver{
public:
explicit TalkOver(TalkOverRule rule = {})
: m_rule(rule)
{}

SceneMove look(const ScreenEvents& look, WallClock now);

// Whether the warp the rule asks for has shown (true without one).
bool warped() const;
// What it has seen, for the log: "black fade, banner read".
std::string describe() const;

private:
TalkOverRule m_rule;
WarpFade m_fade = WarpFade::NONE;
bool m_banner = false;
WallClock m_quiet_since = WallClock::max();
WallClock m_calm_since = WallClock::max();
};



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

#include "CommonFramework/ImageTools/ImageBoxes.h"
#include "CommonFramework/ImageTypes/ImageRGB32.h"
#include "Pokemon_ScreenEvents.h"

namespace PokemonAutomation{
namespace Pokemon{


const char* screen_event_name(ScreenEvent event){
switch (event){
case ScreenEvent::FADE_BLACK: return "black fade";
case ScreenEvent::FADE_WHITE: return "white fade";
case ScreenEvent::LOCATION_BANNER: return "location banner";
case ScreenEvent::DIALOG_BOX: return "dialog box";
case ScreenEvent::CHOICE_BOX: return "choice box";
case ScreenEvent::INTERACT_PROMPT: return "interact prompt";
case ScreenEvent::BATTLE_STARTED: return "battle started";
case ScreenEvent::BATTLE_ENDED: return "battle ended";
case ScreenEvent::PLAYER_CAN_MOVE: return "player can move";
case ScreenEvent::SCREEN_STILL: return "screen still";
}
return "?";
}

const char* warp_fade_name(WarpFade fade){
switch (fade){
case WarpFade::NONE: return "no fade";
case WarpFade::BLACK: return "black fade";
case WarpFade::WHITE: return "white fade";
}
return "?";
}


ScreenEvents::ScreenEvents(std::initializer_list<ScreenEvent> events){
for (ScreenEvent event : events){
set(event, true);
}
}
void ScreenEvents::set(ScreenEvent event, bool on){
if (on){
m_bits |= bit(event);
}else{
m_bits &= ~bit(event);
}
}
ScreenEvents ScreenEvents::from_bits(uint32_t bits){
ScreenEvents out;
out.m_bits = bits & ((uint32_t(1) << SCREEN_EVENT_COUNT) - 1);
return out;
}

ScreenEvents fade_events(WarpFade fade){
switch (fade){
case WarpFade::NONE:
return {};
case WarpFade::BLACK:
return {ScreenEvent::FADE_BLACK};
case WarpFade::WHITE:
return {ScreenEvent::FADE_WHITE};
}
return {};
}
WarpFade fade_in(const ScreenEvents& events){
if (events.has(ScreenEvent::FADE_BLACK)){
return WarpFade::BLACK;
}
if (events.has(ScreenEvent::FADE_WHITE)){
return WarpFade::WHITE;
}
return WarpFade::NONE;
}
std::string describe(const ScreenEvents& events){
std::string out;
for (size_t c = 0; c < SCREEN_EVENT_COUNT; c++){
const ScreenEvent event = (ScreenEvent)c;
if (!events.has(event)){
continue;
}
if (!out.empty()){
out += ", ";
}
out += screen_event_name(event);
}
return out.empty() ? "nothing" : out;
}


WarpFade fade_of(const ImageStats& middle){
if (middle.stddev.sum() > FADE_MAX_STDDEV_SUM){
return WarpFade::NONE;
}
const double sum = middle.average.sum();
return sum <= FADE_MAX_DARK_SUM ? WarpFade::BLACK : sum >= FADE_MIN_WHITE_SUM ? WarpFade::WHITE : WarpFade::NONE;
}
WarpFade fade_of_screen(const ImageViewRGB32& screen){
return fade_of(image_stats(extract_box_reference(screen, ImageFloatBox(0.1, 0.1, 0.8, 0.8))));
}


ScreenEventWatcher::ScreenEventWatcher(ScreenEventObserver& observer, ScreenEvents stop_on)
: VisualInferenceCallback("ScreenEventWatcher")
, m_observer(observer)
, m_stop_on(stop_on)
{}
void ScreenEventWatcher::make_overlays(VideoOverlaySet& items) const{
m_observer.make_overlays(items);
}
bool ScreenEventWatcher::process_frame(const VideoSnapshot& frame){
const ScreenEvents events = m_observer.observe(frame, frame.timestamp);
m_seen.fetch_or(events.bits(), std::memory_order_acq_rel);
if (!events.any_of(m_stop_on)){
return false;
}
m_stopped_on.store(events.bits(), std::memory_order_release);
return true;
}
void ScreenEventWatcher::reset(){
m_seen.store(0, std::memory_order_release);
m_stopped_on.store(0, std::memory_order_release);
}



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