Skip to content

Commit 8fbfede

Browse files
committed
Make Y-COMM detector work both offline and online.
1 parent 5c5b753 commit 8fbfede

7 files changed

Lines changed: 57 additions & 32 deletions

File tree

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,12 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
335335

336336
auto snapshot = feed.snapshot();
337337

338-
WhiteDialogBoxDetector detector;
339-
detector.make_overlays(overlays);
340-
cout << detector.detect(snapshot) << endl;
338+
YCommIconWatcher detector;
339+
cout << detector.process_frame(snapshot, current_time()) << endl;
340+
341+
// WhiteDialogBoxDetector detector;
342+
// detector.make_overlays(overlays);
343+
// cout << detector.detect(snapshot) << endl;
341344

342345

343346
#if 0

SerialPrograms/Source/PokemonSwSh/Inference/PokemonSwSh_YCommDetector.cpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ bool YCommMenuDetector::process_frame(const ImageViewRGB32& frame, WallClock tim
5454
}
5555

5656

57-
namespace{
58-
59-
ImageFloatBox YCOMM_ICON_BOX{0.007, 0.944, 0.032, 0.054};
60-
61-
}
6257

6358
class YCommIconMatcher : public ImageMatch::WaterfillTemplateMatcher{
6459
public:
@@ -85,36 +80,52 @@ const YCommIconMatcher& YCommIconMatcher::instance(){
8580
}
8681

8782

88-
YCommIconDetector::YCommIconDetector(bool is_on)
89-
: VisualInferenceCallback("YCommIconDetector")
83+
YCommIconDetector::YCommIconDetector(Color color, bool is_on)
84+
: m_color(color)
85+
, m_icon(0.007, 0.944, 0.032, 0.054)
86+
, m_left(0.004362, 0.961165, 0.007634, 0.025243)
87+
, m_right(0.038000, 0.961165, 0.007634, 0.025243)
9088
, m_is_on(is_on)
9189
{}
9290

9391
void YCommIconDetector::make_overlays(VideoOverlaySet& items) const{
94-
items.add(COLOR_RED, YCOMM_ICON_BOX);
92+
items.add(COLOR_RED, m_icon);
93+
items.add(COLOR_RED, m_left);
94+
items.add(COLOR_RED, m_right);
9595
}
9696

97-
bool YCommIconDetector::process_frame(const ImageViewRGB32& frame, WallClock timestamp){
97+
bool YCommIconDetector::detect(const ImageViewRGB32& screen){
9898

9999
const std::vector<std::pair<uint32_t, uint32_t>> filters = {
100100
{combine_rgb(0, 0, 150), combine_rgb(100, 100, 255)},
101101
{combine_rgb(0, 0, 150), combine_rgb(127, 127, 255)},
102102
{combine_rgb(0, 0, 150), combine_rgb(191, 191, 255)},
103103
};
104104

105-
const size_t min_size = (size_t)(350. * frame.total_pixels() / (1920 * 1080.));
105+
const size_t min_size = (size_t)(350. * screen.total_pixels() / (1920 * 1080.));
106106

107-
const bool detected = match_template_by_waterfill(
108-
frame.size(),
109-
extract_box_reference(frame, YCOMM_ICON_BOX),
107+
if (match_template_by_waterfill(
108+
screen.size(),
109+
extract_box_reference(screen, m_icon),
110110
YCommIconMatcher::instance(),
111111
filters,
112112
{min_size, SIZE_MAX},
113113
120,
114114
[](Kernels::Waterfill::WaterfillObject& object) -> bool { return true; }
115-
);
115+
)){
116+
return m_is_on;
117+
}
118+
119+
ImageStats left = image_stats(extract_box_reference(screen, m_left));
120+
if (!is_solid(left, {0.0410557, 0.211144, 0.747801}, 0.20)){
121+
return !m_is_on;
122+
}
123+
ImageStats right = image_stats(extract_box_reference(screen, m_right));
124+
if (!is_solid(right, {0.0410557, 0.211144, 0.747801}, 0.20)){
125+
return !m_is_on;
126+
}
116127

117-
return m_is_on ? detected : !detected;
128+
return m_is_on;
118129
}
119130

120131

SerialPrograms/Source/PokemonSwSh/Inference/PokemonSwSh_YCommDetector.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#ifndef PokemonAutomation_PokemonSwSh_YCommDetector_H
88
#define PokemonAutomation_PokemonSwSh_YCommDetector_H
99

10+
#include "Common/Cpp/Color.h"
1011
#include "CommonFramework/ImageTools/ImageBoxes.h"
12+
#include "CommonTools/VisualDetector.h"
1113
#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h"
1214

1315
namespace PokemonAutomation{
@@ -35,19 +37,29 @@ class YCommMenuDetector : public VisualInferenceCallback{
3537

3638
// Detect the blue Y letter as the Y Comm symbol in the lower left corner of the screen on the
3739
// overworld, when Y Comm is active.
38-
class YCommIconDetector : public VisualInferenceCallback{
40+
class YCommIconDetector : public StaticScreenDetector{
3941
public:
4042
// If `is_on` is true, `process_frame()` returns true if it finds the Y letter.
4143
// Otherwise, `process_frame()` returns true if the Y letter is not found.
42-
YCommIconDetector(bool is_on);
44+
YCommIconDetector(Color color, bool is_on);
4345

4446
virtual void make_overlays(VideoOverlaySet& items) const override;
45-
46-
virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override final;
47+
virtual bool detect(const ImageViewRGB32& screen) override;
4748

4849
private:
50+
Color m_color;
51+
ImageFloatBox m_icon;
52+
ImageFloatBox m_left;
53+
ImageFloatBox m_right;
54+
4955
bool m_is_on;
5056
};
57+
class YCommIconWatcher : public DetectorToFinder<YCommIconDetector>{
58+
public:
59+
YCommIconWatcher(Color color = COLOR_RED, bool is_on = true)
60+
: DetectorToFinder("YCommIconWatcher", std::chrono::milliseconds(250), color, is_on)
61+
{}
62+
};
5163

5264

5365

SerialPrograms/Source/PokemonSwSh/Programs/EggPrograms/PokemonSwSh_EggAutonomous.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ bool EggAutonomous::run_batch(
320320
while (num_eggs_hatched < 5 || m_num_eggs_retrieved < 5){
321321
// Detect when Y-Comm icon disappears. This is the time an egg is hatching
322322
const bool y_comm_visible_when_egg_hatching = false;
323-
YCommIconDetector egg_hatching_detector(y_comm_visible_when_egg_hatching);
323+
YCommIconWatcher egg_hatching_detector(COLOR_RED, y_comm_visible_when_egg_hatching);
324324

325325
bool restart_bike_loop = false;
326326
for (size_t i_bike_loop = 0; i_bike_loop < this->LOOPS_PER_FETCH && bike_loop_count < MAX_BIKE_LOOP_COUNT;){
@@ -493,7 +493,7 @@ void EggAutonomous::wait_for_egg_hatched(
493493
){
494494
env.console.overlay().add_log("Egg hatching " + std::to_string(num_hatched_eggs) + "/5", COLOR_GREEN);
495495
const bool y_comm_visible_at_end_of_egg_hatching = true;
496-
YCommIconDetector end_egg_hatching_detector(y_comm_visible_at_end_of_egg_hatching);
496+
YCommIconWatcher end_egg_hatching_detector(COLOR_RED, y_comm_visible_at_end_of_egg_hatching);
497497
const int ret = run_until<ProControllerContext>(
498498
env.console, context,
499499
[](ProControllerContext& context){
@@ -538,7 +538,7 @@ size_t EggAutonomous::talk_to_lady_to_fetch_egg(
538538
);
539539

540540
const bool y_comm_visible_at_end_of_dialog = true;
541-
YCommIconDetector dialog_over_detector(y_comm_visible_at_end_of_dialog);
541+
YCommIconWatcher dialog_over_detector(COLOR_RED, y_comm_visible_at_end_of_dialog);
542542
switch (ret){
543543
case 0:
544544
++num_eggs_retrieved;
@@ -846,7 +846,7 @@ bool EggAutonomous::process_hatched_pokemon(
846846
}else{
847847
// Leave menu, go back to overworld
848848
const bool y_comm_visible = true;
849-
YCommIconDetector y_comm_detector(y_comm_visible);
849+
YCommIconWatcher y_comm_detector(COLOR_RED, y_comm_visible);
850850
const int ret = run_until<ProControllerContext>(
851851
env.console, context,
852852
[](ProControllerContext& context){
@@ -873,7 +873,7 @@ void EggAutonomous::mash_B_until_y_comm_icon(
873873
){
874874
context.wait_for_all_requests();
875875
const bool y_comm_visible = true;
876-
YCommIconDetector y_comm_detector(y_comm_visible);
876+
YCommIconWatcher y_comm_detector(COLOR_RED, y_comm_visible);
877877
int ret = run_until<ProControllerContext>(
878878
env.console, context,
879879
[](ProControllerContext& context){

SerialPrograms/Source/PokemonSwSh/Programs/RNG/PokemonSwSh_DailyHighlightRNG.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void DailyHighlightRNG::interact_with_trader(SingleSwitchProgramEnvironment& env
180180
VideoOverlaySet boxes(env.console);
181181
SelectionArrowFinder arrow_detector(env.console, ImageFloatBox(0.5, 0.58, 0.2, 0.08));
182182
arrow_detector.make_overlays(boxes);
183-
YCommIconDetector y_comm_icon_detector(true);
183+
YCommIconWatcher y_comm_icon_detector;
184184
size_t tries = 0;
185185

186186
while (true){
@@ -387,7 +387,7 @@ void DailyHighlightRNG::prepare_game_state(SingleSwitchProgramEnvironment& env,
387387
void DailyHighlightRNG::return_to_overworld(SingleSwitchProgramEnvironment& env, ProControllerContext& context, bool wait_after_detection){
388388
context.wait_for_all_requests();
389389
env.console.log("Returning to the overworld.");
390-
YCommIconDetector y_comm_icon_detector(true);
390+
YCommIconWatcher y_comm_icon_detector;
391391
int ret = run_until<ProControllerContext>(
392392
env.console, context,
393393
[](ProControllerContext& context){

SerialPrograms/Source/PokemonSwSh/Programs/ShinyHuntAutonomous/PokemonSwSh_ShinyHuntAutonomous-Regigigas2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ bool ShinyHuntAutonomousRegigigas2::kill_and_return(VideoStream& stream, ProCont
8181

8282
while (true){
8383
RaidCatchWatcher detector(stream.overlay());
84-
YCommIconDetector overworld(true);
84+
YCommIconWatcher overworld;
8585
context.wait_for_all_requests();
8686
int result = wait_until(
8787
stream, context,

SerialPrograms/Source/Tests/PokemonSwSh_Tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ int test_pokemonSwSh_CheckNurseryArrowFinder(const ImageViewRGB32& image, bool t
8989
}
9090

9191
int test_pokemonSwSh_YCommIconDetector(const ImageViewRGB32& image, bool target){
92-
const bool is_on = true;
93-
YCommIconDetector detector(is_on);
92+
YCommIconWatcher detector;
9493
bool result = detector.process_frame(image, current_time());
9594
TEST_RESULT_EQUAL(result, target);
9695
return 0;

0 commit comments

Comments
 (0)