Skip to content

Commit 4cd876f

Browse files
committed
add inference stop condition to LZA Turbomacro
1 parent 09a55f5 commit 4cd876f

4 files changed

Lines changed: 180 additions & 0 deletions

File tree

SerialPrograms/Source/PokemonLZA/PokemonLZA_Panels.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "Programs/PokemonLZA_ClothingBuyer.h"
1616
#include "Programs/PokemonLZA_StallBuyer.h"
1717
#include "Programs/PokemonLZA_PostKillCatcher.h"
18+
#include "Programs/PokemonLZA_TurboMacro.h"
1819

1920
// Trading
2021
#include "Programs/Trading/PokemonLZA_SelfBoxTrade.h"
@@ -79,6 +80,7 @@ std::vector<PanelEntry> PanelListFactory::make_panels() const{
7980
ret.emplace_back(make_single_switch_program<HyperspaceRewardReset_Descriptor, HyperspaceRewardReset>());
8081
ret.emplace_back(make_single_switch_program<DonutMaker_Descriptor, DonutMaker>());
8182
if (IS_BETA_VERSION){
83+
ret.emplace_back(make_single_switch_program<LZA_TurboMacro_Descriptor, LZA_TurboMacro>());
8284
}
8385

8486
ret.emplace_back("---- Farming ----");
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/* Turbo Macro
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
8+
#include "CommonTools/Async/InferenceRoutines.h"
9+
#include "PokemonLA/Inference/Sounds/PokemonLA_ShinySoundDetector.h"
10+
#include "PokemonLZA/Options/PokemonLZA_ShinyDetectedAction.h"
11+
#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h"
12+
#include "PokemonLZA_TurboMacro.h"
13+
14+
//#include <iostream>
15+
//using std::cout;
16+
//using std::endl;
17+
18+
namespace PokemonAutomation{
19+
namespace NintendoSwitch{
20+
namespace PokemonLZA{
21+
22+
23+
LZA_TurboMacro_Descriptor::LZA_TurboMacro_Descriptor()
24+
: SingleSwitchProgramDescriptor(
25+
"NintendoSwitch:TurboMacro",
26+
"Nintendo Switch", "Turbo Macro",
27+
"Programs/NintendoSwitch/TurboMacro.html",
28+
"Create macros",
29+
ProgramControllerClass::StandardController_NoRestrictions,
30+
FeedbackType::NONE,
31+
AllowCommandsWhenRunning::DISABLE_COMMANDS
32+
)
33+
{}
34+
35+
LZA_TurboMacro::LZA_TurboMacro()
36+
: LOOP(
37+
"<b>Number of times to loop:</b>",
38+
LockMode::UNLOCK_WHILE_RUNNING,
39+
100, 0
40+
)
41+
, TABLE(
42+
"Command Schedule:",
43+
{
44+
ControllerClass::NintendoSwitch_ProController,
45+
ControllerClass::NintendoSwitch_LeftJoycon,
46+
ControllerClass::NintendoSwitch_RightJoycon,
47+
}
48+
)
49+
, GO_HOME_WHEN_DONE(true)
50+
, RUN_UNTIL_CALLBACK(
51+
"Trigger to stop program:",
52+
{
53+
{RunUntilCallback::NONE, "none", "None (stop when done all loops)"},
54+
{RunUntilCallback::SHINY_SOUND, "shiny-sound", "Shiny Sound Detected"},
55+
},
56+
LockMode::LOCK_WHILE_RUNNING,
57+
RunUntilCallback::NONE
58+
)
59+
{
60+
PA_ADD_OPTION(LOOP);
61+
PA_ADD_OPTION(GO_HOME_WHEN_DONE);
62+
PA_ADD_OPTION(RUN_UNTIL_CALLBACK);
63+
PA_ADD_OPTION(TABLE);
64+
}
65+
66+
67+
void LZA_TurboMacro::program(SingleSwitchProgramEnvironment& env, CancellableScope& scope){
68+
ProControllerContext context(scope, env.console.controller<ProController>());
69+
70+
switch (RUN_UNTIL_CALLBACK){
71+
case RunUntilCallback::NONE:
72+
run_table(env, scope);
73+
break;
74+
case RunUntilCallback::SHINY_SOUND:
75+
run_table_stop_when_shiny_sound(env, scope);
76+
break;
77+
default:
78+
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "TurboMacro::program(): Unknown RunUntilCallback");
79+
80+
}
81+
82+
if (GO_HOME_WHEN_DONE){
83+
go_home(env.console, context);
84+
}
85+
}
86+
87+
88+
void LZA_TurboMacro::run_table(SingleSwitchProgramEnvironment& env, CancellableScope& scope){
89+
for (uint32_t c = 0; c < LOOP; c++){
90+
TABLE.run(scope, env.console.controller());
91+
}
92+
}
93+
94+
void LZA_TurboMacro::run_table_stop_when_shiny_sound(SingleSwitchProgramEnvironment& env, CancellableScope& scope){
95+
ShinySoundDetectedActionOption shiny_detected_option("Shiny Detected", "", "1000 ms", ShinySoundDetectedAction::NOTIFY_ON_FIRST_ONLY);
96+
ShinySoundHandler shiny_sound_handler(shiny_detected_option);
97+
PokemonLA::ShinySoundDetector shiny_detector(env.console, [&](float error_coefficient) -> bool {
98+
// Warning: This callback will be run from a different thread than this function.
99+
// env.console.overlay().add_log("Shiny Sound Detected!", COLOR_YELLOW);
100+
return shiny_sound_handler.on_shiny_sound(
101+
env, env.console,
102+
0,
103+
error_coefficient
104+
);
105+
});
106+
107+
int ret = run_until(
108+
env.console, scope,
109+
[&](CancellableScope& scope){
110+
run_table(env, scope);
111+
},
112+
{shiny_detector}
113+
);
114+
115+
if (ret == 0){
116+
return;
117+
}
118+
}
119+
120+
}
121+
}
122+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Turbo Macro
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_PokemonLZA_TurboMacro_H
8+
#define PokemonAutomation_PokemonLZA_TurboMacro_H
9+
10+
#include "Common/Cpp/Options/SimpleIntegerOption.h"
11+
#include "Controllers/ControllerStateTable.h"
12+
#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h"
13+
#include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h"
14+
15+
namespace PokemonAutomation{
16+
namespace NintendoSwitch{
17+
namespace PokemonLZA{
18+
19+
20+
class LZA_TurboMacro_Descriptor : public SingleSwitchProgramDescriptor{
21+
public:
22+
LZA_TurboMacro_Descriptor();
23+
};
24+
25+
26+
class LZA_TurboMacro : public SingleSwitchProgramInstance{
27+
public:
28+
LZA_TurboMacro();
29+
30+
virtual void program(SingleSwitchProgramEnvironment& env, CancellableScope& scope) override;
31+
32+
void run_table(SingleSwitchProgramEnvironment& env, CancellableScope& scope);
33+
34+
void run_table_stop_when_shiny_sound(SingleSwitchProgramEnvironment& env, CancellableScope& scope);
35+
36+
37+
private:
38+
SimpleIntegerOption<uint32_t> LOOP;
39+
ControllerCommandTables TABLE;
40+
GoHomeWhenDoneOption GO_HOME_WHEN_DONE;
41+
42+
enum class RunUntilCallback{
43+
NONE,
44+
SHINY_SOUND,
45+
};
46+
EnumDropdownOption<RunUntilCallback> RUN_UNTIL_CALLBACK;
47+
};
48+
49+
50+
51+
}
52+
}
53+
}
54+
#endif // PokemonAutomation_PokemonLZA_TurboMacro_H

SerialPrograms/cmake/SourceFiles.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,8 @@ file(GLOB LIBRARY_SOURCES
17531753
Source/PokemonLZA/Programs/PokemonLZA_StallBuyer.h
17541754
Source/PokemonLZA/Programs/PokemonLZA_TrainerBattle.cpp
17551755
Source/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h
1756+
Source/PokemonLZA/Programs/PokemonLZA_TurboMacro.cpp
1757+
Source/PokemonLZA/Programs/PokemonLZA_TurboMacro.h
17561758
Source/PokemonLZA/Programs/ShinyHunting/PokemonLZA_AutoFossil.cpp
17571759
Source/PokemonLZA/Programs/ShinyHunting/PokemonLZA_AutoFossil.h
17581760
Source/PokemonLZA/Programs/ShinyHunting/PokemonLZA_BeldumHunter.cpp

0 commit comments

Comments
 (0)