-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathPokemonSwSh_MaxLair_Run_Battle.cpp
More file actions
391 lines (324 loc) · 13.5 KB
/
PokemonSwSh_MaxLair_Run_Battle.cpp
File metadata and controls
391 lines (324 loc) · 13.5 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* Max Lair Battle
*
* From: https://github.com/PokemonAutomation/
*
*/
#include "Common/Cpp/PrettyPrint.h"
#include "CommonFramework/Exceptions/OperationFailedException.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "CommonFramework/Tools/ErrorDumper.h"
#include "CommonTools/Async/InferenceRoutines.h"
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
#include "PokemonSwSh/Resources/PokemonSwSh_MaxLairDatabase.h"
#include "PokemonSwSh/Inference/PokemonSwSh_SelectionArrowFinder.h"
#include "PokemonSwSh/Programs/PokemonSwSh_BasicCatcher.h"
#include "PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_BattleMenu.h"
#include "PokemonSwSh/MaxLair/AI/PokemonSwSh_MaxLair_AI.h"
//#include "PokemonSwSh_MaxLair_Run_CaughtScreen.h"
#include "PokemonSwSh_MaxLair_Run_Battle.h"
namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSwSh{
namespace MaxLairInternal{
bool read_battle_menu(
ProgramEnvironment& env,
VideoStream& stream, ProControllerContext& context, size_t player_index,
OcrFailureWatchdog& ocr_watchdog,
GlobalState& state,
const ConsoleSpecificOptions& settings,
bool currently_dmaxed, bool cheer_only
){
PlayerState& player = state.players[player_index];
VideoOverlaySet boxes(stream.overlay());
BattleMenuReader reader(stream.overlay(), settings.language, ocr_watchdog);
BattleMoveArrowFinder arrow_finder(stream.overlay());
arrow_finder.make_overlays(boxes);
// Read raid mon.
do{
std::set<std::string> mon = reader.read_opponent(stream.logger(), context, stream.video());
if (mon.size() == 1){
state.opponent = std::move(mon);
break;
}
if (mon.size() > 1){
stream.log("Ambiguous Read Result: " + set_to_str(mon), COLOR_PURPLE);
if (state.opponent.size() == 1 && mon.find(*state.opponent.begin()) != mon.end()){
stream.log("Using previous known value to disambiguate: " + set_to_str(mon), COLOR_PURPLE);
break;
}
}
if (state.opponent.size() == 1){
stream.log("Failed to read opponent from battle. Using previously known value: " + set_to_str(state.opponent), COLOR_ORANGE);
break;
}
stream.log("Unable to read opponent from battle. Attempting to read from summary.", COLOR_ORANGE);
pbf_press_button(context, BUTTON_Y, 80ms, 1000ms);
pbf_press_dpad(context, DPAD_UP, 80ms, 400ms);
pbf_press_button(context, BUTTON_A, 80ms, 2000ms);
context.wait_for_all_requests();
mon = reader.read_opponent_in_summary(stream.logger(), stream.video().snapshot());
pbf_mash_button(context, BUTTON_B, 3000ms);
state.opponent = std::move(mon);
if (state.wins == 3 && !state.boss.empty()){
if (!state.opponent.empty() && *state.opponent.begin() != state.boss){
stream.log("Inconsistent Boss: Expected " + state.boss + ", Read: " + *state.opponent.begin(), COLOR_RED);
}
state.opponent = {state.boss};
break;
}
}while (false);
context.wait_for_all_requests();
const std::string& opponent = state.opponent.empty()
? ""
: *state.opponent.begin();
if (state.wins != 3 && is_boss(opponent)){
stream.log("Boss found before 3 wins. Something is seriously out-of-sync.", COLOR_RED);
dump_image(stream.logger(), MODULE_NAME, stream.video(), "BossBeforeEnd");
// send_program_telemetry(
// env.logger(), true, COLOR_RED, MODULE_NAME,
// "Error",
// {{"Message:", "Boss found before 3 wins."}},
// ""
// );
return false;
}
// Infer the boss if we don't know it.
if (state.boss.empty() && state.wins == 3){
state.boss = opponent;
}
// Read misc.
VideoSnapshot screen = stream.video().snapshot();
state.opponent_hp = reader.read_opponent_hp(stream.logger(), screen);
if (cheer_only){
player.dmax_turns_left = 0;
player.health = Health{0, 1};
player.can_dmax = false;
pbf_press_button(context, BUTTON_A, 80ms, 1000ms);
context.wait_for_all_requests();
return true;
}
if (currently_dmaxed){
player.dmax_turns_left--;
if (player.dmax_turns_left <= 0){
stream.log("State Inconsistency: dmax_turns_left <= 0 && currently_dmaxed == true", COLOR_RED);
player.dmax_turns_left = 1;
}
}else{
std::string name = reader.read_own_mon(stream.logger(), screen);
if (!name.empty()){
state.players[player_index].pokemon = std::move(name);
}
if (player.dmax_turns_left > 1){
stream.log("State Inconsistency: dmax_turns_left > 0 && currently_dmaxed == false", COLOR_RED);
state.move_slot = 0;
}
if (player.dmax_turns_left == 1){
stream.log("End of Dmax.");
state.move_slot = 0;
}
player.dmax_turns_left = 0;
}
Health health[4];
reader.read_hp(stream.logger(), screen, health, player_index);
if (health[0].hp >= 0) state.players[0].health = health[0];
if (health[1].hp >= 0) state.players[1].health = health[1];
if (health[2].hp >= 0) state.players[2].health = health[2];
if (health[3].hp >= 0) state.players[3].health = health[3];
for (size_t attempts = 0; attempts < 5; attempts++){
// Enter move selection to read PP.
pbf_press_button(context, BUTTON_A, 80ms, 1000ms);
context.wait_for_all_requests();
// Clear move blocked status.
player.move_blocked[0] = false;
player.move_blocked[1] = false;
player.move_blocked[2] = false;
player.move_blocked[3] = false;
screen = stream.video().snapshot();
int8_t pp[4] = {-1, -1, -1, -1};
reader.read_own_pp(stream.logger(), screen, pp);
player.pp[0] = pp[0];
player.pp[1] = pp[1];
player.pp[2] = pp[2];
player.pp[3] = pp[3];
player.can_dmax = reader.can_dmax(screen);
// Read move slot.
// int8_t move_slot = arrow_finder.get_slot();
int8_t move_slot = arrow_finder.detect(screen);
if (move_slot < 0){
stream.log("Unable to detect move slot.", COLOR_RED);
// dump_image(stream.logger(), MODULE_NAME, "MoveSlot", screen);
// pbf_press_button(context, BUTTON_A, 80ms, 1000ms);
// pbf_press_dpad(context, DPAD_RIGHT, 2000ms, 0ms);
// pbf_press_dpad(context, DPAD_UP, 2000ms, 0ms);
// move_slot = 0;
pbf_mash_button(context, BUTTON_B, 1000ms);
continue;
}else{
stream.log("Current Move Slot: " + std::to_string(move_slot), COLOR_BLUE);
}
if (move_slot != state.move_slot){
stream.log(
"Move Slot Mismatch: Expected = " + std::to_string(state.move_slot) + ", Actual = " + std::to_string(move_slot),
COLOR_RED
);
}
state.move_slot = move_slot;
return true;
}
dump_image(stream.logger(), MODULE_NAME, "MoveSlot", screen);
return true;
}
StateMachineAction run_move_select(
ProgramEnvironment& env, size_t console_index,
VideoStream& stream, ProControllerContext& context,
OcrFailureWatchdog& ocr_watchdog,
GlobalStateTracker& state_tracker,
const ConsoleSpecificOptions& settings,
bool currently_dmaxed, bool cheer_only,
const EndBattleDecider& decider
){
GlobalState& state = state_tracker[console_index];
size_t player_index = state.find_player_index(console_index);
PlayerState& player = state.players[player_index];
if (!read_battle_menu(
env, stream, context, player_index,
ocr_watchdog,
state, settings,
currently_dmaxed, cheer_only
)){
return StateMachineAction::RESET_RECOVER;
}
if (state.wins < 4) {
const std::string& opponent = state.opponent.empty() ? "" : *state.opponent.begin();
if (!opponent.empty() && decider.stop_for_non_boss(opponent)) {
stream.log("Stopping program as " + opponent + " was encountered.", COLOR_PURPLE);
return StateMachineAction::STOP_PROGRAM;
}
}
GlobalState inferred = state_tracker.synchronize(stream.logger(), console_index);
bool all_moves_blocked = false;
while (true){
stream.log("Selecting move...");
if (cheer_only){
stream.log("Choosing move Cheer. (you are dead)", COLOR_PURPLE);
// pbf_mash_button(context, BUTTON_A, 2000ms);
// context.wait_for_all_requests();
break;
}
std::pair<uint8_t, bool> move = select_move(
stream.logger(),
inferred,
player_index
);
stream.log("Choosing move " + std::to_string((int)move.first) + (move.second ? " (dmax)." : "."), COLOR_PURPLE);
if (player.can_dmax && move.second){
pbf_press_dpad(context, DPAD_LEFT, 80ms, 400ms);
pbf_press_button(context, BUTTON_A, 80ms, 1000ms);
player.dmax_turns_left = 3;
}
while (state.move_slot != move.first){
pbf_press_dpad(context, DPAD_DOWN, 80ms, 400ms);
state.move_slot++;
state.move_slot %= 4;
}
// Enter the move.
if (all_moves_blocked){
// If we had trouble selecting a move, then we're probably stuck in a self-target loop.
// Force target the opponent.
stream.log("Force targeting opponent due to inability to select a move after multiple attempts...", COLOR_RED);
pbf_press_button(context, BUTTON_A, 160ms, 2000ms);
pbf_press_dpad(context, DPAD_UP, 2000ms, 0ms);
}
pbf_mash_button(context, BUTTON_A, 2000ms);
context.wait_for_all_requests();
// inference.stop();
// Back out and look for battle menu. This indicates that the move wasn't selectable.
BattleMenuDetector detector;
int result = run_until<ProControllerContext>(
stream, context,
[](ProControllerContext& context){
pbf_mash_button(context, BUTTON_B, 5000ms);
},
{{detector}},
INFERENCE_RATE
);
// No battle menu detected, we're good.
if (result < 0){
player.move_blocked[state.move_slot] = false;
break;
}
// Battle menu detected. It means the move wasn't selectable.
stream.log("Move not selectable.", COLOR_RED);
player.move_blocked[state.move_slot] = true;
bool no_moves = true;
for (size_t c = 0; c < 4; c++){
no_moves &= player.move_blocked[c];
}
if (no_moves){
stream.log("All moves reported as blocked. This is impossible. Clearing state.", COLOR_RED);
for (size_t c = 0; c < 4; c++){
player.move_blocked[c] = false;
}
all_moves_blocked = true;
}
state_tracker.push_update(console_index);
// Reset position.
pbf_mash_button(context, BUTTON_B, 2000ms);
pbf_press_button(context, BUTTON_A, 160ms, 1000ms);
pbf_press_dpad(context, DPAD_RIGHT, 2000ms, 0ms);
pbf_press_dpad(context, DPAD_UP, 2000ms, 0ms);
state.move_slot = 0;
inferred = state_tracker.infer_actual_state(console_index);
// inferred.players[player_index].move_blocked[state.move_slot] = true;
}
// inference.stop();
return StateMachineAction::KEEP_GOING;
}
StateMachineAction throw_balls(
AdventureRuntime& runtime,
ProgramEnvironment& env, size_t console_index,
VideoStream& stream, ProControllerContext& context,
Language language,
OcrFailureWatchdog& ocr_watchdog,
GlobalStateTracker& state_tracker,
const EndBattleDecider& decider
){
GlobalState& state = state_tracker[console_index];
state.clear_battle_state();
state.wins++;
state.players[0].health.value.dead = 0;
state.players[1].health.value.dead = 0;
state.players[2].health.value.dead = 0;
state.players[3].health.value.dead = 0;
GlobalState inferred = state_tracker.synchronize(stream.logger(), console_index);
std::string ball;
bool boss = inferred.wins == 4;
if (boss){
ball = decider.boss_ball(console_index, inferred.boss);
}else{
ball = decider.normal_ball(console_index);
}
BattleBallReader reader(stream, language);
pbf_press_button(context, BUTTON_A, 400ms, 600ms);
context.wait_for_all_requests();
int16_t balls = move_to_ball(reader, stream, context, ball);
if (balls != 0){
pbf_mash_button(context, BUTTON_A, 1000ms);
}else{
OperationFailedException::fire(
ErrorReport::NO_ERROR_REPORT,
"Unable to find appropriate ball. Did you run out?",
stream
);
}
ReadableQuantity999& stat = boss
? runtime.consoles[console_index].boss_balls
: runtime.consoles[console_index].normal_balls;
stat.update_with_ocr(balls, boss ? -1 : 1);
stat.quantity = (uint16_t)std::max((int)stat.quantity - 1, 0);
return StateMachineAction::KEEP_GOING;
}
}
}
}
}