Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "Inference/PokemonLZA_PartyBarDetector.h"
#include "Inference/PokemonLZA_SelectionArrowDetector.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryFailures.h"
#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h"
Expand All @@ -34,7 +35,7 @@



void add_tests(UnitTestDatabase& database){

Check warning on line 38 in SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonLZA::add_tests( UnitTestDatabase & database): function_lines=26 (advisory above 25, limit 50)
add_tests_AlertEyeDetector(database);
add_tests_BoxCellInfoDetector(database);
add_tests_ButtonDetector(database);
Expand All @@ -57,6 +58,7 @@
add_tests_AutoStoryDialogChoiceBox(database);
add_tests_AutoStoryObjectiveMarkerReader(database);
add_tests_AutoStoryFailures(database);
add_tests_AutoStoryDialogs(database);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/* PokemonLZA AutoStory - Actions
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <utility>
#include "Common/Cpp/Color.h"
#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h"
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "CommonTools/Async/InferenceRoutines.h"
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
#include "VideoGames/PokemonLZA/Programs/PokemonLZA_MenuNavigation.h"
#include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h"
#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h"
#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h"
#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h"
#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h"
#include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h"
#include "PokemonLZA_AutoStoryActions.h"
#include "PokemonLZA_AutoStoryDialogs.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{

using namespace std::chrono_literals;


void fight_battle(

Check warning on line 32 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonLZA::fight_battle( ProgramEnvironment & env , ConsoleHandle & console , ProControllerContext & context , BattleAIOption & battle_ai): function_lines=38 (advisory above 25, limit 50)
ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context,
BattleAIOption& battle_ai
){
// The battle's text first: the overworld only ends it once it has
// begun, and it must begin within a minute.
RunFromBattleWatcher battle_menu(COLOR_GREEN, &console.overlay(), 10ms);
context.wait_for_all_requests();
const int started = run_until<ProControllerContext>(
console, context,
[](ProControllerContext& context){
pbf_mash_button(context, BUTTON_B, 60s);
},
{battle_menu}
);
if (started < 0){
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::SEND_ERROR_REPORT,
"fight_battle(): no battle began within a minute.",
console
);
}
TrainerBattleState battle_state(battle_ai);
OverworldPartySelectionWatcher overworld(COLOR_WHITE, &console.overlay(), 100ms);
const int ended = run_battle_until(
env, console, context, battle_state, overworld, 120s,
[](ProControllerContext& context){
pbf_press_button(context, BUTTON_UP, 500ms, 1000ms);
}
);
if (ended < 0){
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::SEND_ERROR_REPORT,
"fight_battle(): the battle's menu was gone for two minutes.",
console
);
}
}



void fight_battle_in_progress(

Check warning on line 73 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonLZA::fight_battle_in_progress( ProgramEnvironment & env , ConsoleHandle & console , ProControllerContext & context , BattleAIOption & battle_ai , std :: chrono :: seconds timeout): function_lines=35 (advisory above 25, limit 50)
ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context,
BattleAIOption& battle_ai, std::chrono::seconds timeout
){
TrainerBattleState battle_state(battle_ai);
StoryDialogDetector any_dialog;
const WallClock deadline = current_time() + timeout;
bool attacked = false;
while (true){
context.wait_for_all_requests();
const VideoSnapshot screen = console.video().snapshot();
if (!screen){
context.wait_for(100ms);
continue;
}
const bool dialog = any_dialog.detect(screen);
if (dialog && attacked){
return;
}
if (current_time() > deadline){
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::SEND_ERROR_REPORT,
"fight_battle_in_progress(): no dialog after the battle within the timeout.",
console
);
}
if (dialog || !battle_state.attempt_one_attack(env, console, context)){
if (!attacked){
pbf_press_button(context, BUTTON_A, 160ms, 400ms);
}
continue;
}
attacked = true;
}
}



namespace{

// Whether the report page's Save button (its cursor arrow, where
// save_game_to_menu() looks for it) shows within `timeout`.
bool save_button_shows(ConsoleHandle& console, ProControllerContext& context, Milliseconds timeout){
// TODO: unify with save_game_to_menu() (PokemonLZA_BasicNavigation.cpp),
// which keeps this box inline - naming it there adds a symbol to a
// header at the code-health symbol limit.
SelectionArrowWatcher save_button(
COLOR_GREEN, &console.overlay(), SelectionArrowType::RIGHT, {0.287118, 0.638835, 0.064410, 0.108738}
);
return wait_until(console, context, timeout, {save_button}) == 0;
}

// From the overworld: X opens the menu, R its report page. The first menu
// of a playthrough opens under a save tutorial card that A closes, leaving
// the menu with the cursor on the Bag; R does
// nothing over the card. So when R does not bring up the Save button, A
// once, then R again. A is never pressed on the plain menu, where it
// would open the Bag.
void open_save_report(ConsoleHandle& console, ProControllerContext& context){
pbf_press_button(context, BUTTON_X, 160ms, 2340ms);
pbf_press_button(context, BUTTON_R, 160ms, 240ms);
if (save_button_shows(console, context, 5s)){
return;
}
console.log("save_story_game(): no Save button after R: the menu's tutorial card; pressing A, then R.");
pbf_press_button(context, BUTTON_A, 160ms, 1340ms);
pbf_press_button(context, BUTTON_R, 160ms, 240ms);
if (!save_button_shows(console, context, 5s)){
console.log("save_story_game(): still no Save button; save_game_to_menu() takes over.", COLOR_RED);
}
}

// save_game_to_menu(), its failures as false.
bool try_save(ConsoleHandle& console, ProControllerContext& context){
try{
return save_game_to_menu(console, context);
}catch (OperationFailedException& e){
console.log(std::format("save_story_game(): {}", e.message()), COLOR_RED);
return false;
}
}

} // namespace

// Opens the report page (open_save_report(), for the first menu's
// tutorial), then save_game_to_menu() presses Save once its button shows
// and waits for the "Saved!" text, so the save is confirmed. If that
// fails, B back to the overworld (B is harmless while writing) and one
// more try.
void save_story_game(ConsoleHandle& console, ProControllerContext& context){
open_save_report(console, context);
if (!try_save(console, context)){
mash_to_overworld(console, context, BUTTON_B, 10s);
if (!save_game_to_menu(console, context)){
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::SEND_ERROR_REPORT,
"save_story_game(): the game did not save.",
console
);
}
}
mash_to_overworld(console, context, BUTTON_B, 10s);
}

void claim_mable_research_reward(ConsoleHandle& console, ProControllerContext& context){

Check warning on line 177 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonLZA::claim_mable_research_reward( ConsoleHandle & console , ProControllerContext & context): function_lines=31 (advisory above 25, limit 50)
overworld_to_main_menu(console, context);
for (int i = 0; i < 3; i++){
pbf_press_dpad(context, DPAD_DOWN, 160ms, 400ms);
}
pbf_press_button(context, BUTTON_A, 160ms, 2840ms);

// The page opens on Rewards under Mable's lines on the level 2 reward,
// TM004 Rock Smash: A while a dialog shows,
// until none has for 3 s, then B back to the overworld, through her
// talk after the menu closes (over 20 s).
// No line at all: the page did not open on her reward (03.10,
// not in her office, the claim passed on X alone).
const ScreensPassed passed = press_a_until_quiet(console, context, {.item_receive = true}, 3s, 60s);
if (passed.timed_out){
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::SEND_ERROR_REPORT,
"claim_mable_research_reward(): Mable's lines did not end within 60 s.",
console
);
}
if (!passed.text){
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::SEND_ERROR_REPORT,
"claim_mable_research_reward(): no dialog after opening Mable's Research: no reward was claimed.",
console
);
}
console.log("claim_mable_research_reward(): Mable's lines on the reward passed.");
mash_to_overworld(console, context, BUTTON_B, 60s);
}



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* PokemonLZA AutoStory - Actions
*
* From: https://github.com/PokemonAutomation/
*
* The battle and save actions of the story game (PokemonLZA_AutoStoryGame.h),
* with the dialogue ones in PokemonLZA_AutoStoryDialogs.h, the walks in
* PokemonLZA_AutoStoryMoves.h and the scenes in PokemonLZA_AutoStoryScenes.h.
* Each is a short function over the LZA battle and menu code that already
* exists (TrainerBattle, BasicNavigation, the dialog and selection-arrow
* detectors).
*
*/

#ifndef PokemonAutomation_PokemonLZA_AutoStoryActions_H
#define PokemonAutomation_PokemonLZA_AutoStoryActions_H

#include <chrono>
#include "CommonFramework/Tools/ProgramEnvironment.h"
#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h"
#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h"
#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


// From a trainer's or a wild Pokemon's battle starting, fights it with
// TrainerBattleState (`battle_ai`'s moves), pressing B through the text
// around it, until the overworld is back. Throws OperationFailedException
// if neither shows for two minutes.
void fight_battle(
ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context,
BattleAIOption& battle_ai
);

// A story battle, from the talk before it: attacks with TrainerBattleState
// until a dialog box shows (the speech after a win or a loss). Until the
// first attack lands, a dialog box or a failed lock-on gets A instead: the
// trainer's lines, the challenge cutscene's subtitle (no text box) and
// tutorial cards (Taunie's battle in 03.02). For
// story battles, which show no Run prompt for fight_battle() to start on.
// Throws OperationFailedException after `timeout`.
void fight_battle_in_progress(
ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context,
BattleAIOption& battle_ai, std::chrono::seconds timeout
);

// From the overworld, saves the game (save_game_to_menu()) and returns to
// the overworld. The first menu of a playthrough shows a save tutorial
// card, which it closes with A. If the save fails, B back to the
// overworld and one more try; throws OperationFailedException if that
// fails too.
void save_story_game(ConsoleHandle& console, ProControllerContext& context);

// From the overworld: the main menu, DOWN x3 to "Mable's Research" (as
// recorded), A; through Mable's lines on the reward, back
// to the overworld. Throws OperationFailedException when no line shows
// (no reward claimed) or the lines do not end within 60 s.
void claim_mable_research_reward(ConsoleHandle& console, ProControllerContext& context);



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Loading
Loading