Repository navigation
Expand file tree
/
Copy pathScreenMachine.cpp
More file actions
207 lines (182 loc) · 7.21 KB
/
Copy pathScreenMachine.cpp
File metadata and controls
207 lines (182 loc) · 7.21 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Screen Machine
*
* From: https://github.com/PokemonAutomation/
*
*/
#include <algorithm>
#include <format>
#include <string>
#include <utility>
#include "CommonFramework/ImageTools/ImageDiff.h"
#include "ScreenMachine.h"
namespace PokemonAutomation{
using namespace std::chrono_literals;
namespace{
const ScreenState* classify(const std::vector<ScreenState>& states, const ImageViewRGB32& screen){
for (const ScreenState& state : states){
if (state.matches && state.matches(screen)){
return &state;
}
}
return nullptr;
}
} // namespace
std::string_view screen_event_name(ScreenEvent event){
switch (event){
case ScreenEvent::FADE_BLACK: return "fade_black";
case ScreenEvent::FADE_WHITE: return "fade_white";
case ScreenEvent::TEXT_BOX: return "text_box";
case ScreenEvent::BATTLE: return "battle";
case ScreenEvent::CAN_MOVE: return "can_move";
case ScreenEvent::STILL: return "still";
case ScreenEvent::CHOICE_BOX: return "choice_box";
case ScreenEvent::INTERACT_PROMPT: return "interact_prompt";
}
return "unknown";
}
std::string describe(const ScreenEvents& events, std::string_view separator){
constexpr ScreenEvent ALL[] = {
ScreenEvent::FADE_BLACK, ScreenEvent::FADE_WHITE, ScreenEvent::TEXT_BOX,
ScreenEvent::BATTLE, ScreenEvent::CAN_MOVE, ScreenEvent::STILL,
ScreenEvent::CHOICE_BOX, ScreenEvent::INTERACT_PROMPT,
};
std::string text;
for (ScreenEvent event : ALL){
if (events.has(event)){
text += text.empty() ? "" : separator;
text += screen_event_name(event);
}
}
return text.empty() ? std::string("nothing") : text;
}
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;
}
const ScreenState* match_screen_state(const std::vector<ScreenState>& states, const ScreenObservation& observation){
for (const ScreenState& state : states){
if (state.matches_observation && state.matches_observation(observation)){
return &state;
}
}
return nullptr;
}
const ScreenState& read_screen_state(
VideoStream& stream, CancellableScope& scope,
const std::vector<ScreenState>& states, StepTrace& trace, VideoSnapshot& frame
){
const ScreenState* last = nullptr;
WallClock video_lost = WallClock::max();
for (size_t c = 0; c < 20; c++){
VideoSnapshot screen = stream.video().snapshot();
if (!screen){
// The app resets a frozen capture by itself, which takes a second or two.
const WallClock now = current_time();
video_lost = std::min(video_lost, now);
if (now - video_lost > std::chrono::seconds(10)){
trace.fail("No video from the capture card for 10s.");
}
c--;
scope.wait_for(500ms);
continue;
}
video_lost = WallClock::max();
const ScreenState* now = classify(states, screen);
frame = std::move(screen);
if (now != nullptr && now == last){
return *now;
}
last = now;
scope.wait_for(150ms);
}
if (last == nullptr){
trace.fail("The screen matches none of the known states.");
}
return *last; // Still changing after 3s: go with the latest read.
}
void ScreenMachineChecks::check(const ScreenState& state, VideoSnapshot frame){
const std::optional<std::string> found = problem(state, std::move(frame), current_time());
if (found){
m_trace.fail(*found);
}
}
std::optional<std::string> ScreenMachineChecks::problem(const ScreenState& state, const ScreenObservation& observation){
return problem(state, observation.frame ? *observation.frame : VideoSnapshot(), observation.when);
}
std::optional<std::string> ScreenMachineChecks::unchanged_problem(const ScreenState& state, WallClock when){
if (state.max_still > Milliseconds::zero()){
if (when - m_changed_at > state.max_still){
return std::format("The screen stayed still on \"{}\" for more than {}s.", state.name, state.max_still.count() / 1000);
}
return std::nullopt;
}
if (++m_unchanged >= MAX_UNCHANGED){
return std::format("The screen stopped changing: {} steps on \"{}\" did nothing.", MAX_UNCHANGED, state.name);
}
return std::nullopt;
}
std::optional<std::string> ScreenMachineChecks::problem(const ScreenState& state, VideoSnapshot frame, WallClock when){
if (same_screen(m_last_frame, frame)){
std::optional<std::string> stalled = unchanged_problem(state, when);
if (stalled){
return stalled;
}
}else{
m_unchanged = 0;
m_changed_at = when;
}
m_last_frame = std::move(frame);
if (m_previous != nullptr && m_previous != &state && !m_previous->next.empty() &&
std::find(m_previous->next.begin(), m_previous->next.end(), state.name) == m_previous->next.end()
){
return std::format("Unexpected screen after \"{}\": \"{}\".", m_previous->name, state.name);
}
m_in_a_row = m_previous == &state ? m_in_a_row + 1 : 1;
if (m_in_a_row > state.max_in_a_row){
return std::format("Screen \"{}\" came up {} times in a row: the step for it is not getting anywhere.", state.name, m_in_a_row);
}
if (++m_visits[state.name] > state.max_visits){
return std::format("Screen \"{}\" came up more than {} times: the step for it is not working.", state.name, state.max_visits);
}
m_previous = &state;
return std::nullopt;
}
ScreenMachineResult run_screen_machine_on_observations(
const std::function<ScreenObservation()>& observe, CancellableScope& scope,
const std::vector<ScreenState>& states, Milliseconds timeout, StepTrace& trace
){
// Looks before the run stops even when nothing else ends it (looks with
// no time, states with no visit limit).
constexpr size_t MAX_LOOKS = 10000;
ScreenMachineChecks checks(trace);
std::optional<WallClock> start;
for (size_t looks = 0; looks < MAX_LOOKS; looks++){
scope.throw_if_cancelled();
const ScreenObservation observation = observe();
if (!start){
start = observation.when;
}
if (observation.when - *start > timeout){
return {.terminal = "", .failure = std::format("Did not finish within {}s.", timeout.count() / 1000)};
}
const ScreenState* state = match_screen_state(states, observation);
if (state == nullptr){
return {.terminal = "", .failure = "The screen matches none of the known states."};
}
trace.screen(state->name);
std::optional<std::string> found = checks.problem(*state, observation);
if (found){
return {.terminal = "", .failure = std::move(*found)};
}
if (state->act){
state->act();
}
if (state->terminal){
return {.terminal = state->name, .failure = ""};
}
}
return {.terminal = "", .failure = std::format("Did not finish within {} looks.", MAX_LOOKS)};
}
} // namespace PokemonAutomation