Skip to content

Commit

Permalink
Merge pull request #390 from NympheaR/flyingtrailfarmer
Browse files Browse the repository at this point in the history
Flying Trial Farmer: Add autosaving option after every N trials
  • Loading branch information
Mysticial authored Dec 25, 2023
2 parents 4d5d5f5 + af14b33 commit a4e891f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "PokemonSV/Inference/Dialogs/PokemonSV_DialogDetector.h"
#include "PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h"
#include "PokemonSV/Inference/PokemonSV_WhiteButtonDetector.h"
#include "PokemonSV/Programs/PokemonSV_SaveGame.h"
#include "PokemonSV_FlyingTrialFarmer.h"

namespace PokemonAutomation{
Expand All @@ -40,14 +41,17 @@ struct FlyingTrialFarmer_Descriptor::Stats : public StatsTracker{
: m_trials(m_stats["Trials"])
, m_success(m_stats["Success"])
, m_fail(m_stats["Fail"])
, m_saves(m_stats["Game Saves"])
{
m_display_order.emplace_back("Trials");
m_display_order.emplace_back("Success");
m_display_order.emplace_back("Fail");
m_display_order.emplace_back("Game Saves");
}
std::atomic<uint64_t>& m_trials;
std::atomic<uint64_t>& m_success;
std::atomic<uint64_t>& m_fail;
std::atomic<uint64_t>& m_saves;
};
std::unique_ptr<StatsTracker> FlyingTrialFarmer_Descriptor::make_stats() const{
return std::unique_ptr<StatsTracker>(new Stats());
Expand All @@ -62,13 +66,19 @@ FlyingTrialFarmer::FlyingTrialFarmer()
LockMode::UNLOCK_WHILE_RUNNING,
1000
)
, SAVE_NUM_ROUNDS(
"<b>Save after attempting this many trials:</b><br>0 disables saving.",
LockMode::UNLOCK_WHILE_RUNNING,
50
)
, NOTIFICATIONS({
&NOTIFICATION_PROGRAM_FINISH,
&NOTIFICATION_ERROR_FATAL,
})
{
PA_ADD_OPTION(GO_HOME_WHEN_DONE);
PA_ADD_OPTION(NUM_TRIALS);
PA_ADD_OPTION(SAVE_NUM_ROUNDS);
PA_ADD_OPTION(NOTIFICATIONS);
}

Expand All @@ -79,7 +89,6 @@ bool FlyingTrialFarmer::run_rewards(SingleSwitchProgramEnvironment& env, BotBase
while (true){
DialogBoxWatcher dialog(COLOR_GREEN, true, std::chrono::milliseconds(250), DialogType::DIALOG_BLACK);
OverworldWatcher overworld(COLOR_CYAN);

context.wait_for_all_requests();

int ret_finish = run_until(
Expand All @@ -89,6 +98,7 @@ bool FlyingTrialFarmer::run_rewards(SingleSwitchProgramEnvironment& env, BotBase
},
{ dialog, overworld }
);
context.wait_for_all_requests();

switch (ret_finish){
case 0: // dialog
Expand Down Expand Up @@ -127,18 +137,20 @@ void FlyingTrialFarmer::program(SingleSwitchProgramEnvironment& env, BotBaseCont
);
context.wait_for_all_requests();
if (ret_entry == 0) {
env.log("Black screen detected. Trial starting.");
env.log("Black screen detected. Trial starting...");
}

WhiteButtonWatcher whitebutton(COLOR_GREEN, WhiteButton::ButtonY, {0.40, 0.85, 0.20, 0.14});
context.wait_for_all_requests();

int ret_trial_start = wait_until(
env.console, context,
std::chrono::seconds(120),
{whitebutton}
);
context.wait_for_all_requests();
if (ret_trial_start == 0) {
env.log("Countdown is over. Start navigation sequence.");
env.log("Countdown is over. Starting navigation sequence...");
pbf_wait(context, 3 * TICKS_PER_SECOND);
pbf_move_left_joystick(context, 180, 20, 1 * TICKS_PER_SECOND, 0); // go through the 2nd ring for additional time
pbf_wait(context, 2 * TICKS_PER_SECOND);
Expand All @@ -157,6 +169,12 @@ void FlyingTrialFarmer::program(SingleSwitchProgramEnvironment& env, BotBaseCont
}

stats.m_trials++;

if (SAVE_NUM_ROUNDS != 0 && stats.m_trials % SAVE_NUM_ROUNDS == 0){
save_game_from_overworld(env.program_info(), env.console, context);
stats.m_saves++;
}

env.update_stats();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class FlyingTrialFarmer : public SingleSwitchProgramInstance{
private:
GoHomeWhenDoneOption GO_HOME_WHEN_DONE;
SimpleIntegerOption<uint16_t> NUM_TRIALS;
SimpleIntegerOption<uint16_t> SAVE_NUM_ROUNDS;
EventNotificationsOption NOTIFICATIONS;

bool run_rewards(SingleSwitchProgramEnvironment& env, BotBaseContext& context);
Expand Down

0 comments on commit a4e891f

Please sign in to comment.