Skip to content

Commit 2c17453

Browse files
committed
Generalize "run_until()" beyond just "BotBaseContext".
1 parent fa09a30 commit 2c17453

File tree

99 files changed

+227
-204
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+227
-204
lines changed

ClientSource/Connection/BotBase.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ BotBaseContext::BotBaseContext(CancellableScope& parent, BotBase& botbase)
2222
{
2323
attach(parent);
2424
}
25+
BotBaseContext::BotBaseContext(CancellableScope& parent, BotBaseContext& context)
26+
: m_botbase(context.botbase())
27+
{
28+
attach(parent);
29+
}
2530
BotBaseContext::~BotBaseContext(){
31+
m_lifetime_sanitizer.check_usage();
32+
try{
33+
m_botbase.wait_for_all_requests(this);
34+
}catch (...){}
2635
detach();
2736
}
2837

ClientSource/Connection/BotBase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class BotBaseContext final : public CancellableScope{
7575
public:
7676
BotBaseContext(BotBase& botbase);
7777
BotBaseContext(CancellableScope& parent, BotBase& botbase);
78+
BotBaseContext(CancellableScope& parent, BotBaseContext& context);
7879
virtual ~BotBaseContext();
7980

8081

SerialPrograms/Source/CommonFramework/InferenceInfra/InferenceRoutines.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
#include "Common/Cpp/Exceptions.h"
8-
#include "ClientSource/Connection/BotBase.h"
8+
#include "Common/Cpp/CancellableScope.h"
99
#include "InferenceSession.h"
1010
#include "InferenceRoutines.h"
1111

@@ -69,6 +69,7 @@ int run_until(
6969

7070
return session.triggered_index();
7171
}
72+
#if 0
7273
int run_until(
7374
VideoStream& stream, BotBaseContext& context,
7475
std::function<void(BotBaseContext& context)>&& command,
@@ -95,6 +96,7 @@ int run_until(
9596

9697
return session.triggered_index();
9798
}
99+
#endif
98100

99101

100102

SerialPrograms/Source/CommonFramework/InferenceInfra/InferenceRoutines.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
namespace PokemonAutomation{
1818

19-
class BotBaseContext;
2019
class ProgramEnvironment;
2120

2221

@@ -67,13 +66,25 @@ int run_until(
6766
std::chrono::milliseconds default_video_period = std::chrono::milliseconds(50),
6867
std::chrono::milliseconds default_audio_period = std::chrono::milliseconds(20)
6968
);
69+
template <typename ContextType>
7070
int run_until(
71-
VideoStream& stream, BotBaseContext& context,
72-
std::function<void(BotBaseContext& context)>&& command,
71+
VideoStream& stream, ContextType& context,
72+
std::function<void(ContextType& context)>&& command,
7373
const std::vector<PeriodicInferenceCallback>& callbacks,
7474
std::chrono::milliseconds default_video_period = std::chrono::milliseconds(50),
7575
std::chrono::milliseconds default_audio_period = std::chrono::milliseconds(20)
76-
);
76+
){
77+
return run_until(
78+
stream, context,
79+
[&](CancellableScope& scope){
80+
ContextType subcontext(scope, context);
81+
command(subcontext);
82+
},
83+
callbacks,
84+
default_video_period,
85+
default_audio_period
86+
);
87+
}
7788

7889

7990
#if 0

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
10581058
NormalBattleMenuWatcher battle_menu(COLOR_RED);
10591059
AreaZeroSkyTracker sky_tracker(overlay);
10601060
context.wait_for_all_requests();
1061-
int ret = run_until(
1061+
int ret = run_until<BotBaseContext>(
10621062
console, context,
10631063
[&](BotBaseContext& context){
10641064
while (true){

SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_GameEntry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void start_game_from_home_with_inference(
100100
context.wait_for_all_requests();
101101
{
102102
HomeWatcher detector;
103-
int ret = run_until(
103+
int ret = run_until<BotBaseContext>(
104104
console, context,
105105
[](BotBaseContext& context){
106106
pbf_mash_button(context, BUTTON_B, 10 * TICKS_PER_SECOND);

SerialPrograms/Source/PokemonBDSP/Programs/Eggs/PokemonBDSP_EggAutonomousState.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ void EggAutonomousState::fetch_egg(){
310310
m_console.log("Attempting to fetch an egg.");
311311
{
312312
ShortDialogWatcher dialog;
313-
int ret = run_until(
313+
int ret = run_until<BotBaseContext>(
314314
m_console, m_context,
315315
[](BotBaseContext& context){
316316
pbf_move_left_joystick(context, 0, 255, 125, 0);
@@ -333,7 +333,7 @@ void EggAutonomousState::fetch_egg(){
333333
m_console.log("Going to daycare man.");
334334
{
335335
ShortDialogWatcher dialog;
336-
int ret = run_until(
336+
int ret = run_until<BotBaseContext>(
337337
m_console, m_context,
338338
[](BotBaseContext& context){
339339
pbf_move_left_joystick(context, 0, 255, 30, 0);
@@ -350,7 +350,7 @@ void EggAutonomousState::fetch_egg(){
350350
// Talk to daycare man.
351351
{
352352
ShortDialogWatcher dialog;
353-
int ret = run_until(
353+
int ret = run_until<BotBaseContext>(
354354
m_console, m_context,
355355
[](BotBaseContext& context){
356356
pbf_press_button(context, BUTTON_ZL, 20, 230);
@@ -365,7 +365,7 @@ void EggAutonomousState::fetch_egg(){
365365

366366
{
367367
EggReceivedDetector received;
368-
run_until(
368+
run_until<BotBaseContext>(
369369
m_console, m_context,
370370
[](BotBaseContext& context){
371371
pbf_mash_button(context, BUTTON_ZL, 500);
@@ -454,7 +454,7 @@ void EggAutonomousState::hatch_rest_of_party(){
454454
dump();
455455
ShortDialogWatcher dialog;
456456
FrozenImageDetector frozen(COLOR_CYAN, {0, 0, 1, 0.5}, std::chrono::seconds(60), 20);
457-
int ret = run_until(
457+
int ret = run_until<BotBaseContext>(
458458
m_console, m_context,
459459
[&](BotBaseContext& context){
460460
egg_spin(context, 480 * TICKS_PER_SECOND);
@@ -483,7 +483,7 @@ void EggAutonomousState::spin_until_fetch_or_hatch(){
483483
m_context.wait_for_all_requests();
484484
m_console.log("Looking for more eggs...");
485485
ShortDialogWatcher dialog;
486-
int ret = run_until(
486+
int ret = run_until<BotBaseContext>(
487487
m_console, m_context,
488488
[&](BotBaseContext& context){
489489
egg_spin(context, m_travel_time_per_fetch);

SerialPrograms/Source/PokemonBDSP/Programs/Eggs/PokemonBDSP_EggFeedback.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void hatch_egg(ConsoleHandle& console, BotBaseContext& context){
3333
break;
3434
}
3535

36-
int ret = run_until(
36+
int ret = run_until<BotBaseContext>(
3737
console, context,
3838
[](BotBaseContext& context){
3939
egg_spin(context, 480 * TICKS_PER_SECOND);

SerialPrograms/Source/PokemonBDSP/Programs/Farming/PokemonBDSP_DoublesLeveling.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ bool DoublesLeveling::battle(SingleSwitchProgramEnvironment& env, BotBaseContext
104104
EndBattleWatcher end_battle;
105105
SelectionArrowFinder learn_move(env.console, {0.50, 0.62, 0.40, 0.18}, COLOR_YELLOW);
106106
FrozenImageDetector overworld(std::chrono::seconds(5), 10);
107-
int ret = run_until(
107+
int ret = run_until<BotBaseContext>(
108108
env.console, context,
109109
[](BotBaseContext& context){
110110
pbf_mash_button(context, BUTTON_B, 120 * TICKS_PER_SECOND);

SerialPrograms/Source/PokemonBDSP/Programs/Farming/PokemonBDSP_MoneyFarmerRoute210.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ bool MoneyFarmerRoute210::battle(SingleSwitchProgramEnvironment& env, BotBaseCon
117117

118118
{
119119
StartBattleDetector detector(env.console);
120-
int ret = run_until(
120+
int ret = run_until<BotBaseContext>(
121121
env.console, context,
122122
[](BotBaseContext& context){
123123
pbf_press_button(context, BUTTON_ZL, 10, 10);
@@ -149,7 +149,7 @@ bool MoneyFarmerRoute210::battle(SingleSwitchProgramEnvironment& env, BotBaseCon
149149
BattleMenuWatcher battle_menu(BattleType::TRAINER);
150150
EndBattleWatcher end_battle;
151151
SelectionArrowFinder learn_move(env.console, {0.50, 0.62, 0.40, 0.18}, COLOR_YELLOW);
152-
int ret = run_until(
152+
int ret = run_until<BotBaseContext>(
153153
env.console, context,
154154
[](BotBaseContext& context){
155155
pbf_mash_button(context, BUTTON_B, 120 * TICKS_PER_SECOND);
@@ -380,7 +380,7 @@ void MoneyFarmerRoute210::program(SingleSwitchProgramEnvironment& env, BotBaseCo
380380
std::vector<ImagePixelBox> bubbles;
381381
{
382382
VSSeekerReactionTracker tracker(env.console, {0.20, 0.20, 0.60, 0.60});
383-
run_until(
383+
run_until<BotBaseContext>(
384384
env.console, context,
385385
[this](BotBaseContext& context){
386386
SHORTCUT.run(context, TICKS_PER_SECOND);

0 commit comments

Comments
 (0)