Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions SerialPrograms/Source/PokemonLZA/PokemonLZA_Panels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Programs/PokemonLZA_ClothingBuyer.h"
#include "Programs/PokemonLZA_StallBuyer.h"
#include "Programs/PokemonLZA_PostKillCatcher.h"
#include "Programs/PokemonLZA_TurboMacro.h"

// Trading
#include "Programs/Trading/PokemonLZA_SelfBoxTrade.h"
Expand Down Expand Up @@ -79,6 +80,7 @@ std::vector<PanelEntry> PanelListFactory::make_panels() const{
ret.emplace_back(make_single_switch_program<HyperspaceRewardReset_Descriptor, HyperspaceRewardReset>());
ret.emplace_back(make_single_switch_program<DonutMaker_Descriptor, DonutMaker>());
if (IS_BETA_VERSION){
ret.emplace_back(make_single_switch_program<LZA_TurboMacro_Descriptor, LZA_TurboMacro>());
}

ret.emplace_back("---- Farming ----");
Expand Down
155 changes: 155 additions & 0 deletions SerialPrograms/Source/PokemonLZA/Programs/PokemonLZA_TurboMacro.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/* Turbo Macro
*
* From: https://github.com/PokemonAutomation/
*
*/


#include "CommonTools/Async/InferenceRoutines.h"
#include "PokemonLA/Inference/Sounds/PokemonLA_ShinySoundDetector.h"
#include "PokemonLZA/Options/PokemonLZA_ShinyDetectedAction.h"
#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h"
#include "Pokemon/Pokemon_Strings.h"
#include "CommonFramework/ProgramStats/StatsTracking.h"
#include "CommonFramework/Notifications/ProgramNotifications.h"
#include "PokemonLZA_TurboMacro.h"

//#include <iostream>
//using std::cout;
//using std::endl;

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{

using namespace Pokemon;

LZA_TurboMacro_Descriptor::LZA_TurboMacro_Descriptor()
: SingleSwitchProgramDescriptor(
"PokemonLZA:TurboMacro", STRING_POKEMON + " LZA",
"Turbo Macro",
"Programs/NintendoSwitch/TurboMacro.html",
"Create macros. Stops with specific trigger (e.g. shiny sound).",
ProgramControllerClass::StandardController_NoRestrictions,
FeedbackType::NONE,
AllowCommandsWhenRunning::DISABLE_COMMANDS
)
{}

class LZA_TurboMacro_Descriptor::Stats : public StatsTracker{
public:
Stats()
: loops(m_stats["Loops"])
, shinies(m_stats["Shiny Sounds"])
, errors(m_stats["Errors"])
{
m_display_order.emplace_back("Loops");
m_display_order.emplace_back("Shiny Sounds");
m_display_order.emplace_back("Errors", HIDDEN_IF_ZERO);
}

std::atomic<uint64_t>& loops;
std::atomic<uint64_t>& shinies;
std::atomic<uint64_t>& errors;
};
std::unique_ptr<StatsTracker> LZA_TurboMacro_Descriptor::make_stats() const{
return std::unique_ptr<StatsTracker>(new Stats());
}

LZA_TurboMacro::LZA_TurboMacro()
: LOOP(
"<b>Number of times to loop:</b>",
LockMode::UNLOCK_WHILE_RUNNING,
100, 0
)
, TABLE(
"Command Schedule:",
{
ControllerClass::NintendoSwitch_ProController,
ControllerClass::NintendoSwitch_LeftJoycon,
ControllerClass::NintendoSwitch_RightJoycon,
}
)
, GO_HOME_WHEN_DONE(true)
, RUN_UNTIL_CALLBACK(
"Trigger to stop program:",
{
{RunUntilCallback::NONE, "none", "None (stop when done all loops)"},
{RunUntilCallback::SHINY_SOUND, "shiny-sound", "Shiny Sound Detected"},
},
LockMode::LOCK_WHILE_RUNNING,
RunUntilCallback::NONE
)
{
PA_ADD_OPTION(LOOP);
PA_ADD_OPTION(GO_HOME_WHEN_DONE);
PA_ADD_OPTION(RUN_UNTIL_CALLBACK);
PA_ADD_OPTION(TABLE);
}


void LZA_TurboMacro::program(SingleSwitchProgramEnvironment& env, CancellableScope& scope){
ProControllerContext context(scope, env.console.controller<ProController>());

switch (RUN_UNTIL_CALLBACK){
case RunUntilCallback::NONE:
run_table(env, scope);
break;
case RunUntilCallback::SHINY_SOUND:
run_table_stop_when_shiny_sound(env, scope);
break;
default:
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "TurboMacro::program(): Unknown RunUntilCallback");

}

if (GO_HOME_WHEN_DONE){
go_home(env.console, context);
}
send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH);
}


void LZA_TurboMacro::run_table(SingleSwitchProgramEnvironment& env, CancellableScope& scope){
LZA_TurboMacro_Descriptor::Stats& stats =
env.current_stats<LZA_TurboMacro_Descriptor::Stats>();
for (uint32_t c = 0; c < LOOP; c++){
TABLE.run(scope, env.console.controller());
stats.loops++;
env.update_stats();
}
}

void LZA_TurboMacro::run_table_stop_when_shiny_sound(SingleSwitchProgramEnvironment& env, CancellableScope& scope){
LZA_TurboMacro_Descriptor::Stats& stats =
env.current_stats<LZA_TurboMacro_Descriptor::Stats>();
ShinySoundDetectedActionOption shiny_detected_option("Shiny Detected", "", "1000 ms", ShinySoundDetectedAction::NOTIFY_ON_FIRST_ONLY);
ShinySoundHandler shiny_sound_handler(shiny_detected_option);
PokemonLA::ShinySoundDetector shiny_detector(env.console, [&](float error_coefficient) -> bool {
// Warning: This callback will be run from a different thread than this function.
// env.console.overlay().add_log("Shiny Sound Detected!", COLOR_YELLOW);
return shiny_sound_handler.on_shiny_sound(
env, env.console,
0,
error_coefficient
);
});

int ret = run_until(
env.console, scope,
[&](CancellableScope& scope){
run_table(env, scope);
},
{shiny_detector}
);

if (ret == 0){
stats.shinies++;
env.update_stats();
return;
}
}

}
}
}
57 changes: 57 additions & 0 deletions SerialPrograms/Source/PokemonLZA/Programs/PokemonLZA_TurboMacro.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Turbo Macro
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_PokemonLZA_TurboMacro_H
#define PokemonAutomation_PokemonLZA_TurboMacro_H

#include "Common/Cpp/Options/SimpleIntegerOption.h"
#include "Controllers/ControllerStateTable.h"
#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h"
#include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


class LZA_TurboMacro_Descriptor : public SingleSwitchProgramDescriptor{
public:
LZA_TurboMacro_Descriptor();

class Stats;
virtual std::unique_ptr<StatsTracker> make_stats() const override;
};


class LZA_TurboMacro : public SingleSwitchProgramInstance{
public:
LZA_TurboMacro();

virtual void program(SingleSwitchProgramEnvironment& env, CancellableScope& scope) override;

void run_table(SingleSwitchProgramEnvironment& env, CancellableScope& scope);

void run_table_stop_when_shiny_sound(SingleSwitchProgramEnvironment& env, CancellableScope& scope);


private:
SimpleIntegerOption<uint32_t> LOOP;
ControllerCommandTables TABLE;
GoHomeWhenDoneOption GO_HOME_WHEN_DONE;

enum class RunUntilCallback{
NONE,
SHINY_SOUND,
};
EnumDropdownOption<RunUntilCallback> RUN_UNTIL_CALLBACK;
};



}
}
}
#endif // PokemonAutomation_PokemonLZA_TurboMacro_H
2 changes: 2 additions & 0 deletions SerialPrograms/cmake/SourceFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,8 @@ file(GLOB LIBRARY_SOURCES
Source/PokemonLZA/Programs/PokemonLZA_StallBuyer.h
Source/PokemonLZA/Programs/PokemonLZA_TrainerBattle.cpp
Source/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h
Source/PokemonLZA/Programs/PokemonLZA_TurboMacro.cpp
Source/PokemonLZA/Programs/PokemonLZA_TurboMacro.h
Source/PokemonLZA/Programs/ShinyHunting/PokemonLZA_AutoFossil.cpp
Source/PokemonLZA/Programs/ShinyHunting/PokemonLZA_AutoFossil.h
Source/PokemonLZA/Programs/ShinyHunting/PokemonLZA_BeldumHunter.cpp
Expand Down