-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathPokemonSwSh_MaxLair_StateMachine.cpp
More file actions
218 lines (202 loc) · 7.91 KB
/
PokemonSwSh_MaxLair_StateMachine.cpp
File metadata and controls
218 lines (202 loc) · 7.91 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
/* Max Lair State Machine
*
* From: https://github.com/PokemonAutomation/
*
*/
#include "Common/Cpp/Containers/FixedLimitVector.tpp"
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
#include "CommonFramework/Tools/ErrorDumper.h"
#include "CommonFramework/Notifications/ProgramInfo.h"
#include "CommonTools/Async/InterruptableCommands.h"
#include "CommonTools/Async/InferenceRoutines.h"
#include "CommonTools/VisualDetectors/FrozenImageDetector.h"
#include "Pokemon/Pokemon_Strings.h"
#include "PokemonSwSh/Inference/Dens/PokemonSwSh_RaidCatchDetector.h"
#include "PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_PokemonSelectMenu.h"
#include "PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_PathSelect.h"
#include "PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_ItemSelectMenu.h"
#include "PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_ProfessorSwap.h"
#include "PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_BattleMenu.h"
#include "PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_PokemonSwapMenu.h"
#include "PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_EndBattle.h"
#include "PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_Entrance.h"
#include "PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_PokemonSelect.h"
#include "PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_PathSelect.h"
#include "PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_ItemSelect.h"
#include "PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_ProfessorSwap.h"
#include "PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_Battle.h"
#include "PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_PokemonSwap.h"
#include "PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_CaughtScreen.h"
#include "PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_Entrance.h"
#include "PokemonSwSh_MaxLair_StateMachine.h"
namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonSwSh{
namespace MaxLairInternal{
AdventureRuntime::~AdventureRuntime() = default;
AdventureRuntime::AdventureRuntime(
FixedLimitVector<ConsoleHandle>& consoles,
const size_t p_host_index,
const Consoles& p_console_settings,
const EndBattleDecider& p_actions,
const bool p_go_home_when_done,
HostingSettings& p_hosting_settings,
EventNotificationOption& p_notification_status,
EventNotificationOption& p_notification_shiny,
Stats& p_session_stats
)
: host_index(p_host_index)
, console_settings(p_console_settings)
, actions(p_actions)
, go_home_when_done(p_go_home_when_done)
, hosting_settings(p_hosting_settings)
, notification_status(p_notification_status)
, notification_shiny(p_notification_shiny)
, ocr_watchdog(p_console_settings.active_consoles())
, session_stats(p_session_stats)
{
for (size_t c = 0; c < p_console_settings.active_consoles(); c++){
ocr_watchdog.emplace_back(consoles[c].logger(), console_settings.PLAYERS[c]->language);
}
}
StateMachineAction run_state_iteration(
AdventureRuntime& runtime, size_t console_index,
ProgramEnvironment& env,
ConsoleHandle& console, ProControllerContext& context,
bool save_path,
GlobalStateTracker& global_state,
const EndBattleDecider& decider,
const ImageViewRGB32& entrance
){
GlobalState state = global_state.infer_actual_state(console_index);
// uint8_t wins = state.wins;
bool starting = true;
for (size_t c = 0; c < 4; c++){
if (state.players[c].console_id == (int8_t)console_index){
starting = false;
break;
}
}
PokemonSelectMenuDetector pokemon_select(false);
PokemonSwapMenuDetector pokemon_swap(false);
PathSelectDetector path_select;
ItemSelectDetector item_menu(false);
ProfessorSwapDetector professor_swap(console.overlay(), !starting);
BattleMenuDetector battle_menu;
RaidCatchWatcher catch_select(console.overlay());
PokemonCaughtMenuWatcher caught_menu;
EntranceDetector entrance_detector(entrance);
FrozenImageDetector frozen_screen(COLOR_CYAN, {0, 0, 1, 0.5}, std::chrono::seconds(30), 10);
int result = wait_until(
console, context,
std::chrono::seconds(300),
{
{starting
? (VisualInferenceCallback&)pokemon_select
: (VisualInferenceCallback&)pokemon_swap
},
{path_select},
{item_menu},
{professor_swap},
{battle_menu},
{catch_select},
{caught_menu},
{entrance_detector},
{frozen_screen},
},
std::chrono::milliseconds(200)
);
switch (result){
case 0:
if (starting){
console.log("Current State: " + STRING_POKEMON + " Select");
run_select_pokemon(
console_index,
console, context,
global_state,
runtime.ocr_watchdog[console_index],
runtime.console_settings[console_index]
);
return StateMachineAction::KEEP_GOING;
}else{
console.log("Current State: " + STRING_POKEMON + " Swap");
run_swap_pokemon(
console_index,
runtime,
console, context,
global_state,
runtime.console_settings[console_index]
);
return StateMachineAction::KEEP_GOING;
}
case 1:
console.log("Current State: Path Select");
run_path_select(env, console_index, console, context, global_state);
return StateMachineAction::KEEP_GOING;
case 2:
console.log("Current State: Item Select");
run_item_select(console_index, console, context, global_state);
return StateMachineAction::KEEP_GOING;
case 3:
console.log("Current State: Professor Swap");
run_professor_swap(console_index, runtime, console, context, global_state);
return StateMachineAction::KEEP_GOING;
case 4:
console.log("Current State: Move Select");
return run_move_select(
env, console_index,
console, context,
runtime.ocr_watchdog[console_index],
global_state,
runtime.console_settings[console_index],
battle_menu.dmaxed(),
battle_menu.cheer(),
runtime.actions
);
case 5:
console.log("Current State: Catch Select");
return throw_balls(
runtime,
env, console_index,
console, context,
runtime.console_settings[console_index].language,
runtime.ocr_watchdog[console_index],
global_state,
decider
);
case 6:
console.log("Current State: Caught Menu");
return run_caught_screen(
runtime,
env, console_index,
console, context,
global_state, decider,
entrance
);
case 7:
console.log("Current State: Entrance");
run_entrance(
runtime,
env, console_index,
console, context,
save_path,
global_state
);
return StateMachineAction::DONE_WITH_ADVENTURE;
case 8:
console.log("Current State: Frozen Screen", COLOR_RED);
// pbf_mash_button(context, BUTTON_B, 1000ms);
// context.wait_for_all_requests();
// return StateMachineAction::KEEP_GOING;
return StateMachineAction::RESET_RECOVER;
default:
console.log("Program hang. No state detected after 5 minutes.", COLOR_RED);
dump_image(console.logger(), MODULE_NAME, console.video(), "ProgramHang");
global_state.mark_as_dead(console_index);
return StateMachineAction::RESET_RECOVER;
}
}
}
}
}
}