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
99 changes: 99 additions & 0 deletions SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* Screen Event Types
*
* From: https://github.com/PokemonAutomation/
*
* What a frame shows, in words every game shares.
*
*/

#ifndef PokemonAutomation_CommonTools_ScreenEvents_ScreenEventTypes_H
#define PokemonAutomation_CommonTools_ScreenEvents_ScreenEventTypes_H

#include <cstdint>
#include <format>
#include <initializer_list>
#include <string_view>
#include "Common/Cpp/EnumStringMap.h"

namespace PokemonAutomation{


// One thing a frame shows. A game reports the ones its detectors can see.
enum class ScreenEvent : uint8_t{
FADE_BLACK, // A uniform dark screen: a door, a cave mouth, a scene cut.
FADE_WHITE, // A uniform white screen: a cave exit.
LOCATION_BANNER, // A location banner. An observer given an expected
// map reports only that map's.
DIALOG_BOX, // A dialogue box.
CHOICE_BOX, // A choice (Yes/No, a menu of answers).
INTERACT_PROMPT, // A button prompt to interact ("A Talk").
BATTLE_STARTED, // A battle shows (its menu, its HUD or its opening).
BATTLE_ENDED, // A battle's closing screen.
PLAYER_CAN_MOVE, // The overworld with the player in control.
SCREEN_STILL, // The screen barely changed since the frame before.
SWITCH_HOME_MENU, // The Switch's HOME menu: the game is not on the screen.
};
// "dialog box", for the log.
const EnumStringMap<ScreenEvent>& SCREEN_EVENT_NAMES();


// How a screen's fade looks: the kind of FADE_BLACK / FADE_WHITE.
enum class WarpFade{
NONE,
BLACK, // a door's black, or a cave mouth's dim fade
WHITE, // a cave exit's white, with no battle after it
};
const EnumStringMap<WarpFade>& WARP_FADE_NAMES();


// The events of one frame, or of all the frames a watcher saw.
class ScreenEvents{
public:
ScreenEvents() = default;
ScreenEvents(std::initializer_list<ScreenEvent> events);

bool has(ScreenEvent event) const{
return (m_bits & bit(event)) != 0;
}
bool any_of(const ScreenEvents& events) const{

Check warning on line 58 in SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: public 'ScreenEvents::any_of' has no caller outside its own files and tests; make it private or drop it (ignore if a later PR in the stack calls it) (fork AGENTS.md "Types, names and comments")
return (m_bits & events.m_bits) != 0;
}
// Adds `event`, or removes it when `on` is false (as std::bitset::set).
void set(ScreenEvent event, bool on);
void merge(const ScreenEvents& events){

Check warning on line 63 in SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: public 'ScreenEvents::merge' has no caller outside its own files and tests; make it private or drop it (ignore if a later PR in the stack calls it) (fork AGENTS.md "Types, names and comments")
m_bits |= events.m_bits;
}

bool operator==(const ScreenEvents& other) const = default;

private:
static uint32_t bit(ScreenEvent event){
return uint32_t(1) << (uint32_t)event;
}

uint32_t m_bits = 0;
};



} // namespace PokemonAutomation


// "dialog box, player can move"; "nothing" when empty.
template <>
struct std::formatter<PokemonAutomation::ScreenEvents> : std::formatter<std::string_view>{
auto format(const PokemonAutomation::ScreenEvents& events, std::format_context& context) const{
auto out = context.out();
bool first = true;
for (const auto& [event, name] : PokemonAutomation::SCREEN_EVENT_NAMES()){
if (!events.has(event)){
continue;
}
out = std::format_to(out, "{}{}", first ? "" : ", ", name);
first = false;
}
return first ? std::format_to(out, "nothing") : out;
}
};

#endif
119 changes: 119 additions & 0 deletions SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* Screen Events
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "CommonFramework/ImageTools/ImageBoxes.h"
#include "CommonFramework/ImageTools/ImageDiff.h"
#include "CommonFramework/ImageTools/ImageStats.h"
#include "CommonTools/Images/SolidColorTest.h"
#include "ScreenEventTypes.h"
#include "ScreenFades.h"
#include "ScreenEvents.h"

namespace PokemonAutomation{


namespace{

// A fade is uniform over the middle 80% of the screen, as the shared
// Black/WhiteScreenWatcher test: a dim cave floor is textured, no fade.
constexpr double FADE_MAX_STDDEV_SUM = 10;
// Dark up to an rgb sum of 225: a cave mouth's fade is dim, not black.
constexpr double FADE_MAX_DARK_SUM = 225;
// White from an rgb sum of 500, as the shared WhiteScreenWatcher's.
constexpr double FADE_MIN_WHITE_SUM = 500;

} // namespace


const EnumStringMap<ScreenEvent>& SCREEN_EVENT_NAMES(){
static const EnumStringMap<ScreenEvent> names{
{ScreenEvent::FADE_BLACK, "black fade"},
{ScreenEvent::FADE_WHITE, "white fade"},
{ScreenEvent::LOCATION_BANNER, "location banner"},
{ScreenEvent::DIALOG_BOX, "dialog box"},
{ScreenEvent::CHOICE_BOX, "choice box"},
{ScreenEvent::INTERACT_PROMPT, "interact prompt"},
{ScreenEvent::BATTLE_STARTED, "battle started"},
{ScreenEvent::BATTLE_ENDED, "battle ended"},
{ScreenEvent::PLAYER_CAN_MOVE, "player can move"},
{ScreenEvent::SCREEN_STILL, "screen still"},
{ScreenEvent::SWITCH_HOME_MENU, "Switch HOME menu"},
};
return names;
}

const EnumStringMap<WarpFade>& WARP_FADE_NAMES(){
static const EnumStringMap<WarpFade> names{
{WarpFade::NONE, "no fade"},
{WarpFade::BLACK, "black fade"},
{WarpFade::WHITE, "white fade"},
};
return names;
}


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 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;
}


// Black is is_black() (SolidColorTest, BlackScreenDetector's test) with
// the fade's thresholds.
// White is not is_white(): that also wants an even grey, and a fade to
// white can be tinted.
WarpFade fade_of(const ImageStats& middle){
if (is_black(middle, FADE_MAX_DARK_SUM, FADE_MAX_STDDEV_SUM)){
return WarpFade::BLACK;
}
if (middle.stddev.sum() > FADE_MAX_STDDEV_SUM){
return WarpFade::NONE;
}
return middle.average.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))));
}


bool same_screen(const VideoSnapshot& a, const VideoSnapshot& b){
if (!a || !b || a->width() != b->width() || a->height() != b->height()){
return false;
}
return ImageMatch::pixel_RMSD(a, b) < 2.0;
}


} // namespace PokemonAutomation
41 changes: 41 additions & 0 deletions SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Screen Events
*
* From: https://github.com/PokemonAutomation/
*
* A game's detectors, read frame by frame as shared screen events.
*
*/

#ifndef PokemonAutomation_CommonTools_ScreenEvents_ScreenEvents_H
#define PokemonAutomation_CommonTools_ScreenEvents_ScreenEvents_H

#include "Common/Cpp/Time.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
#include "ScreenEventTypes.h"

namespace PokemonAutomation{


// A game's detectors, as events. It may keep state between frames, so one
// observer watches one stream.
class ScreenEventObserver{
public:
virtual ~ScreenEventObserver() = default;

// What `frame` shows; `now` is the caller's clock. A null frame shows
// nothing.
virtual ScreenEvents observe(const VideoSnapshot& frame, WallClock now) = 0;

// The boxes the detectors read, for the video overlay.
virtual void make_overlays(VideoOverlaySet& items) const = 0;
};


// Whether the screen barely changed from `a` to `b` (SCREEN_STILL).
bool same_screen(const VideoSnapshot& a, const VideoSnapshot& b);



} // namespace PokemonAutomation
#endif
Loading