Repository navigation
Expand file tree
/
Copy pathNintendoSwitch_HomeMenuDetector.cpp
More file actions
168 lines (142 loc) · 5.73 KB
/
Copy pathNintendoSwitch_HomeMenuDetector.cpp
File metadata and controls
168 lines (142 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* Home Menu Detector
*
* From: https://github.com/PokemonAutomation/
*
*/
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
#include "CommonTools/Images/SolidColorTest.h"
#include "NintendoSwitch_HomeMenuDetector.h"
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
namespace NintendoSwitch{
HomeMenuDetector::HomeMenuDetector(ConsoleHandle& console, Color color)
: m_color(color)
, m_console_type(console, color)
, m_top(0.510223, 0.019835, 0.441450, 0.034711)
// , m_iconrow_left(0.02, 0.70, 0.13, 0.15)
, m_iconrow_right(0.85, 0.70, 0.13, 0.15)
, m_iconrow_icons(0.20, 0.70, 0.60, 0.15)
, m_bottom_row(0.10, 0.92, 0.10, 0.05)
, m_bottom_icons(0.70, 0.92, 0.28, 0.05)
, m_user_icons(0.05, 0.05, 0.2, 0.08)
, m_game_slot(0.08, 0.25, 0.10, 0.38)
, m_close_game_top(0.226358, 0.272648, 0.407445, 0.033989)
{}
void HomeMenuDetector::make_overlays(VideoOverlaySet& items) const{
m_console_type.make_overlays(items);
items.add(m_color, m_top);
// items.add(m_color, m_iconrow_left);
items.add(m_color, m_iconrow_right);
items.add(m_color, m_iconrow_icons);
items.add(m_color, m_bottom_row);
items.add(m_color, m_bottom_icons);
items.add(m_color, m_user_icons);
items.add(m_color, m_game_slot);
items.add(m_color, m_close_game_top);
}
//
// This miraculously works on both Switch 1 and Switch 2.
//
bool HomeMenuDetector::detect(const ImageViewRGB32& screen){
return detect_only(screen);
#if 0
if (!detect_only(screen)){
return false;
}
try{
m_console_type.commit_to_cache();
}catch (UserSetupError&){
screen.save("HomeMenuDetector-Failure.png");
throw;
}
return true;
#endif
}
void HomeMenuDetector::commit_state(){
m_console_type.commit_to_cache();
}
bool HomeMenuDetector::detect_only(const ImageViewRGB32& screen){
ImageStats stats_close_game_top = image_stats(extract_box_reference(screen, m_close_game_top)); // the top portion of the Close game popup
// ensure m_close_game_top is NOT uniform in color. If it is, then we likely are looking at the Close Game popup.
if (stats_close_game_top.stddev.sum() < 50){
return false;
}
ImageStats stats_bottom_row = image_stats(extract_box_reference(screen, m_bottom_row));
// cout << stats_bottom_row.average << stats_bottom_row.stddev << endl;
bool white;
// cout << "stats_bottom_row.average.sum() = " << stats_bottom_row.average.sum() << endl;
if (stats_bottom_row.average.sum() < 200){
white = false;
}else if (stats_bottom_row.average.sum() > 500){
white = true;
}else{
return false;
}
// cout << "white: " << white << endl;
ImageStats stats_top = image_stats(extract_box_reference(screen, m_top));
// cout << stats_top.average << stats_top.stddev << endl;
if (stats_top.stddev.sum() > 20){
return false;
}
ImageStats stats_bottom_icons = image_stats(extract_box_reference(screen, m_bottom_icons));
// cout << stats_bottom_icons.average << stats_bottom_icons.stddev << endl;
if (stats_bottom_icons.stddev.sum() < 10){
return false;
}
// ImageStats stats_bottom_left = image_stats(extract_box_reference(screen, m_bottom_row));
ImageStats stats_bottom_right = image_stats(extract_box_reference(screen, m_iconrow_right));
// cout << stats_bottom_left.average << stats_bottom_left.stddev << endl;
// cout << stats_bottom_right.average << stats_bottom_right.stddev << endl;
if (white){
if (!is_white(stats_bottom_row) || !is_white(stats_bottom_right)){
// cout << "asdf" << endl;
return false;
}
}else{
if (!is_grey(stats_bottom_row, 0, 200) || !is_grey(stats_bottom_right, 0, 200)){
// cout << "qwer" << endl;
return false;
}
}
ImageStats stats_bottom_middle = image_stats(extract_box_reference(screen, m_iconrow_icons));
if (stats_bottom_middle.stddev.sum() < 50){
return false;
}
// cout << euclidean_distance(stats_bottom_row.average, stats_bottom_row.average) << endl;
if (euclidean_distance(stats_bottom_row.average, stats_bottom_row.average) > 20){
// cout << "qwer = " << euclidean_distance(stats_bottom_row.average, stats_bottom_row.average) << endl;
return false;
}
// cout << euclidean_distance(stats_bottom_row.average, stats_bottom_right.average) << endl;
if (euclidean_distance(stats_bottom_row.average, stats_bottom_right.average) > 20){
// cout << "asdf" << endl;
return false;
}
// cout << euclidean_distance(stats_bottom_row.average, stats_bottom_row.average) << endl;
if (euclidean_distance(stats_bottom_row.average, stats_bottom_right.average) > 20){
// cout << "zxcv" << endl;
return false;
}
// cout << euclidean_distance(stats_top.average, stats_bottom_row.average) << endl;
if (euclidean_distance(stats_top.average, stats_bottom_row.average) > 20){
// cout << "xcvb" << endl;
return false;
}
ImageStats stats_user_icons = image_stats(extract_box_reference(screen, m_user_icons));
// cout << stats_user_icons.stddev << endl;
if (stats_user_icons.stddev.sum() < 40){
return false;
}
ImageStats stats_game_slot = image_stats(extract_box_reference(screen, m_game_slot));
// cout << stats_game_slot.stddev << endl;
if (stats_game_slot.stddev.sum() < 50){
return false;
}
// Read the console type only when we have confirmed the home menu.
m_console_type.detect_only(screen);
return true;
}
}
}