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
224 changes: 73 additions & 151 deletions SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,7 @@ void flee_battle(ConsoleHandle& console, ProControllerContext& context){
}
}

WildBattleExitResult exit_wild_battle(
Logger& logger, WildBattleExitActions& actions,
bool stop_on_move_learn, bool prevent_evolution
){
bool exit_wild_battle(ConsoleHandle& console, ProControllerContext& context, bool stop_on_move_learn, bool prevent_evolution){
// For move learning, there are two dialog selection boxes in a row
// we need to decline the first one and accept the second one, so mashing B won't work
// The first one will occur after an Advance Battle Dialog
Expand All @@ -588,195 +585,120 @@ WildBattleExitResult exit_wild_battle(
bool evolved = false;
while (true){
if (errors > 5 || loops > 5){
return WildBattleExitResult::Failed;
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::SEND_ERROR_REPORT,
"exit_wild_battle(): Failed to exit battle.",
console
);
}

WildBattleExitEvent event = first_attempt
? actions.mash_b_until_battle_exited()
: actions.press_until_dialog(rejected_first_box ? BUTTON_A : BUTTON_B);
BlackScreenWatcher battle_exited(COLOR_RED);
AdvanceBattleDialogWatcher advance_dialog(COLOR_RED);
BattleLearnDialogWatcher move_learn_select(COLOR_RED);

switch (event){
case WildBattleExitEvent::BattleExited:
context.wait_for_all_requests();
WallClock deadline = current_time() + 30s;
int ret;
if (first_attempt){
ret = run_until<ProControllerContext>(
console, context,
[](ProControllerContext& context){
pbf_mash_button(context, BUTTON_B, 20000ms);
},
{ battle_exited }
);
}else{
ret = run_until<ProControllerContext>(
console, context,
[deadline, rejected_first_box](ProControllerContext& context){
pbf_wait(context, 1000ms); // give the watchers a chance to detect something
while (current_time() < deadline){
pbf_press_button(context, rejected_first_box ? BUTTON_A : BUTTON_B, 200ms, 1800ms);
// Presses are queued, so without this the loop queues a minute of them
// before the deadline and keeps pressing long after it.
context.wait_for_all_requests();
}
},
{ battle_exited, advance_dialog, move_learn_select }
);
}

BattleDialogWatcher evolution_started(COLOR_RED);
int ret2;

switch (ret){
case 0:
// check for the evolution screen
if (actions.evolution_started()){
logger.log("Evolution detected.");
context.wait_for_all_requests();
ret2 = run_until<ProControllerContext>(
console, context,
[](ProControllerContext& context){
pbf_wait(context, 2000ms);
},
{ evolution_started }
);

if (ret2 == 0){
console.log("Evolution detected.");
evolved = true;
if (!prevent_evolution){
// make sure B isn't pressed too soon, which would cancel the evolution
actions.wait_for_evolution();
pbf_wait(context, 20000ms);
}
rejected_first_box = false;
continue; // press B as in other cases, and handle any move learning loops that might come up
}
logger.log("Battle exited.");
return move_learned ? WildBattleExitResult::ExitedAfterMoveLearn : WildBattleExitResult::Exited;
case WildBattleExitEvent::AdvanceDialog:
logger.log("Battle Advance arrow detected.");
actions.advance_dialog();
console.log("Battle exited.");
return move_learned;
case 1:
console.log("Battle Advance arrow detected.");
pbf_press_button(context, BUTTON_B, 200ms, 800ms);
rejected_first_box = false;
continue;
case WildBattleExitEvent::MoveLearnPrompt:
case 2:
if (stop_on_move_learn){
logger.log("Move learn detected.");
return WildBattleExitResult::StoppedOnMoveLearn;
console.log("Move learn detected.");
return true;
}else if (rejected_first_box){
loops++;
logger.log("Declined to learn new move.");
actions.answer_move_prompt(BUTTON_A);
console.log("Declined to learn new move.");
pbf_press_button(context, BUTTON_A, 200ms, 0ms);
}else{
actions.answer_move_prompt(BUTTON_B);
pbf_press_button(context, BUTTON_B, 200ms, 0ms);
rejected_first_box = true;
move_learned = true;
}
continue;
case WildBattleExitEvent::None:
default:
// An evolution ends on the overworld without another black screen, so nothing above
// can see that it is over. Check for the overworld now, instead of pressing B for
// another 30 seconds first.
if (evolved){
evolved = false;
first_attempt = false;
if (actions.return_to_overworld()){
logger.log("Battle exited.");
return move_learned ? WildBattleExitResult::ExitedAfterMoveLearn : WildBattleExitResult::Exited;
if (in_overworld(console, context)){
console.log("Battle exited.");
return move_learned;
}
continue;
}
if (first_attempt){
logger.log("Loop detected.");
console.log("Loop detected.");
first_attempt = false;
continue;
}
logger.log("Failed to detect expected battle dialogs.");
console.log("Failed to detect expected battle dialogs.");
errors++;
if (actions.return_to_overworld()){
logger.log("Battle exited.");
return move_learned ? WildBattleExitResult::ExitedAfterMoveLearn : WildBattleExitResult::Exited;
if (in_overworld(console, context)){
console.log("Battle exited.");
return move_learned;
}
rejected_first_box = false;
continue;
}
}
}

namespace{

class ConsoleWildBattleExitActions : public WildBattleExitActions{
public:
ConsoleWildBattleExitActions(ConsoleHandle& console, ProControllerContext& context)
: m_console(console), m_context(context)
{}

virtual WildBattleExitEvent mash_b_until_battle_exited() override{
BlackScreenWatcher battle_exited(COLOR_RED);
m_context.wait_for_all_requests();
int ret = run_until<ProControllerContext>(
m_console, m_context,
[](ProControllerContext& context){
pbf_mash_button(context, BUTTON_B, 20000ms);
},
{ battle_exited }
);
return ret == 0 ? WildBattleExitEvent::BattleExited : WildBattleExitEvent::None;
}
virtual WildBattleExitEvent press_until_dialog(Button button) override{
BlackScreenWatcher battle_exited(COLOR_RED);
AdvanceBattleDialogWatcher advance_dialog(COLOR_RED);
BattleLearnDialogWatcher move_learn_select(COLOR_RED);
m_context.wait_for_all_requests();
WallClock deadline = current_time() + 30s;
int ret = run_until<ProControllerContext>(
m_console, m_context,
[deadline, button](ProControllerContext& context){
pbf_wait(context, 1000ms); // give the watchers a chance to detect something
while (current_time() < deadline){
pbf_press_button(context, button, 200ms, 1800ms);
// Presses are queued, so without this the loop queues a minute of them
// before the deadline and keeps pressing long after it.
context.wait_for_all_requests();
}
},
{ battle_exited, advance_dialog, move_learn_select }
);
switch (ret){
case 0:
return WildBattleExitEvent::BattleExited;
case 1:
return WildBattleExitEvent::AdvanceDialog;
case 2:
return WildBattleExitEvent::MoveLearnPrompt;
default:
return WildBattleExitEvent::None;
}
}
virtual bool evolution_started() override{
BattleDialogWatcher evolution_started(COLOR_RED);
m_context.wait_for_all_requests();
int ret = run_until<ProControllerContext>(
m_console, m_context,
[](ProControllerContext& context){
pbf_wait(context, 2000ms);
},
{ evolution_started }
);
return ret == 0;
}
virtual void wait_for_evolution() override{
pbf_wait(m_context, 20000ms);
}
virtual void advance_dialog() override{
pbf_press_button(m_context, BUTTON_B, 200ms, 800ms);
}
virtual void answer_move_prompt(Button button) override{
pbf_press_button(m_context, button, 200ms, 0ms);
}
virtual bool return_to_overworld() override{
StartMenuWatcher start_menu_open(COLOR_RED);
// attempt to exit any screen that might be open (party, bag, etc)
pbf_mash_button(m_context, BUTTON_B, 500ms);
// overworld detection: look for the start menu
m_context.wait_for_all_requests();
int ret = run_until<ProControllerContext>(
m_console, m_context,
[](ProControllerContext& context){
pbf_press_button(context, BUTTON_PLUS, 200ms, 800ms);
pbf_press_button(context, BUTTON_PLUS, 200ms, 800ms);
pbf_press_button(context, BUTTON_PLUS, 200ms, 800ms);
},
{ start_menu_open }
);
if (ret == 0){
pbf_mash_button(m_context, BUTTON_B, 500ms);
}
m_context.wait_for_all_requests();
return ret == 0;
}

private:
ConsoleHandle& m_console;
ProControllerContext& m_context;
};

}

bool exit_wild_battle(ConsoleHandle& console, ProControllerContext& context, bool stop_on_move_learn, bool prevent_evolution){
ConsoleWildBattleExitActions actions(console, context);
switch (exit_wild_battle(console.logger(), actions, stop_on_move_learn, prevent_evolution)){
case WildBattleExitResult::Exited:
return false;
case WildBattleExitResult::ExitedAfterMoveLearn:
case WildBattleExitResult::StoppedOnMoveLearn:
return true;
case WildBattleExitResult::Failed:
break;
}
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::SEND_ERROR_REPORT,
"exit_wild_battle(): Failed to exit battle.",
console
);
}

void open_party_menu_from_overworld(ConsoleHandle& console, ProControllerContext& context, StartMenuContext menu_context){
uint16_t errors = 0;
bool start_menu_is_open = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#ifndef PokemonAutomation_PokemonFRLG_Navigation_H
#define PokemonAutomation_PokemonFRLG_Navigation_H

#include "Common/Cpp/Logging/AbstractLogger.h"
#include "CommonFramework/Language.h"
#include "CommonFramework/Tools/VideoStream.h"
#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h"
Expand Down Expand Up @@ -75,57 +74,6 @@ void flee_battle(ConsoleHandle& console, ProControllerContext& context);
// Returns true if a move was learned (even if it was rejected) and false otherwise.
bool exit_wild_battle(ConsoleHandle& console, ProControllerContext& context, bool stop_on_move_learn, bool prevent_evolution);

// What a step of exit_wild_battle() saw.
enum class WildBattleExitEvent{
BattleExited, // The screen went black as the battle ended.
AdvanceDialog, // A battle dialog is waiting for a button.
MoveLearnPrompt, // A Pokemon is trying to learn a move.
None, // Nothing was seen before the step timed out.
};

// The game steps that exit_wild_battle() takes. They are injected so that its decisions (what to
// press after each dialog, when to give up) run in tests without a Switch.
class WildBattleExitActions{
public:
virtual ~WildBattleExitActions() = default;

// Mash B for up to 20 seconds, until the battle ends.
virtual WildBattleExitEvent mash_b_until_battle_exited() = 0;

// Press `button` every 2 seconds for up to 30 seconds, until the battle ends, a battle dialog
// waits for a button, or a move-learn prompt opens.
virtual WildBattleExitEvent press_until_dialog(Button button) = 0;

// Right after the battle ends, check for a dialog, which means a Pokemon is evolving.
virtual bool evolution_started() = 0;

// Wait for an evolution to finish without pressing B, which would cancel it.
virtual void wait_for_evolution() = 0;

// Close a battle dialog that is waiting for a button.
virtual void advance_dialog() = 0;

// Answer a move-learn prompt with `button`.
virtual void answer_move_prompt(Button button) = 0;

// Close any open screen, then check for the overworld by opening the start menu. Returns true
// if it opened; it is closed again.
virtual bool return_to_overworld() = 0;
};

enum class WildBattleExitResult{
Exited,
ExitedAfterMoveLearn, // A move-learn prompt came up and the move was declined.
StoppedOnMoveLearn, // Stopped at a move-learn prompt, as stop_on_move_learn asks.
Failed,
};

// The decisions of exit_wild_battle(), taking its game steps from `actions`.
WildBattleExitResult exit_wild_battle(
Logger& logger, WildBattleExitActions& actions,
bool stop_on_move_learn, bool prevent_evolution
);

// Starting from the start menu, a sub-screen of the start menu, or the overworld, navigate to the party screen
enum class StartMenuContext {
STANDARD,
Expand Down
Loading
Loading