From ea8483a070f951a3ede9ad0bb3decf3121c32476 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 23:01:49 -0800 Subject: [PATCH 1/5] BDSP AutoStory 01.04: Roark only after both gym trainers; Trainer Card badge check The walk up the Oreburgh Gym was blocked by the second trainer, an A press opened his talk and it was fought as Roark (lab-B 2026-11-12 22:50): 01.04 passed and 01.05 saved with no badge. The opponent is now the next one in line (GymTrainers: Roark only once both trainers are beaten). When CHECKS is thorough or testing, verify_badge() opens the Trainer Card after the leader's battle and fails on a missing badge. The shared verdict is Pokemon_BadgeCheck.h, for FRLG to reuse. The BDSP reader has no fixture yet and reads UNREADABLE (passes, with the card's screenshot kept in Screenshots/AutoStory/cards/). Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Types/Pokemon_BadgeCheck.cpp | 31 +++++++ .../AutoStory/Types/Pokemon_BadgeCheck.h | 45 ++++++++++ .../PokemonBDSP_AutoStory_Actions_Tests.cpp | 61 ++++++++++++++ .../PokemonBDSP_AutoStory_BadgeCheck.cpp | 64 +++++++++++++++ .../PokemonBDSP_AutoStory_BadgeCheck.h | 39 +++++++++ .../PokemonBDSP_AutoStory_Events_Oreburgh.cpp | 82 +++++++++++++++---- .../PokemonBDSP_AutoStory_GymOpponent.h | 62 ++++++++++++++ ...okemonBDSP_AutoStory_TrainerCardReader.cpp | 23 ++++++ .../PokemonBDSP_AutoStory_TrainerCardReader.h | 36 ++++++++ .../AutoStory/PokemonBDSP_AutoStory.cpp | 2 +- .../PokemonBDSP_AutoStory_StoryExecutor.h | 3 + SerialPrograms/cmake/SourceFiles.cmake | 7 ++ 12 files changed, 439 insertions(+), 16 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.cpp new file mode 100644 index 0000000000..ab7af5b1c1 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.cpp @@ -0,0 +1,31 @@ +/* Badge Check + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_BadgeCheck.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +bool badge_check_wanted(StoryCheckLevel level){ + return level == StoryCheckLevel::TESTING || level == StoryCheckLevel::THOROUGH; +} + +std::string badge_check_failure(BadgeReading reading, std::string_view badge){ + if (reading != BadgeReading::MISSING){ + return ""; + } + return std::format( + "The Trainer Card has no {}: the battle that ended the step was not the gym leader's.", + badge + ); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.h new file mode 100644 index 0000000000..2030bd47e8 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.h @@ -0,0 +1,45 @@ +/* Badge Check + * + * From: https://github.com/PokemonAutomation/ + * + * After a gym leader's battle, a thorough run opens the Trainer Card and + * checks the badge is on it: a battle can end with a win that was not the + * leader's (BDSP 01.04, lab-B 2026-11-12: the second gym trainer was taken + * for Roark and 01.05 saved with no badge). Each game reads its own card + * and says what it saw; the verdict and the wording are shared. + * + */ + +#ifndef PokemonAutomation_Pokemon_BadgeCheck_H +#define PokemonAutomation_Pokemon_BadgeCheck_H + +#include +#include +#include "Pokemon_StoryCheckLevel.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// What a game's Trainer Card reader saw for one badge. +enum class BadgeReading{ + PRESENT, + MISSING, + // The reader has no reference for the card yet, or the card was not on screen. + UNREADABLE, +}; + +// Whether a run at `level` opens the Trainer Card after a leader's battle: +// the levels that check everything for the user. +bool badge_check_wanted(StoryCheckLevel level); + +// The failure text when the badge is missing, "" when `reading` is fine. +// An UNREADABLE card does not fail the step: the reader has nothing to say +// yet, and the caller logs it and keeps the card's screenshot. +std::string badge_check_failure(BadgeReading reading, std::string_view badge); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp index b328d00fe2..d271f1c9c4 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp @@ -17,6 +17,7 @@ #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/Filesystem/Filesystem.h" +#include "Pokemon/AutoStory/Types/Pokemon_BadgeCheck.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/GlobalAutoPaths.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" @@ -24,12 +25,14 @@ #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.h" #include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryTrainers.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" #include "PokemonBDSP_AutoStory_BagScreen.h" #include "PokemonBDSP_AutoStory_BattleMoves.h" #include "PokemonBDSP_AutoStory_Center.h" #include "PokemonBDSP_AutoStory_Events.h" +#include "PokemonBDSP_AutoStory_GymOpponent.h" #include "PokemonBDSP_AutoStory_IntroProbe.h" #include "PokemonBDSP_AutoStory_IntroSteps.h" #include "PokemonBDSP_AutoStory_LitDoorway.h" @@ -493,6 +496,56 @@ UnitTestResult test_asks_someone_else(Logger&){ return true; } +// 01.04 (lab-B, 2026-11-12 22:50): the walk up the gym was blocked by the +// second trainer, an A press opened his talk, and it was taken for Roark's. +// A talk before every trainer is beaten is a trainer's, however it began. +UnitTestResult test_gym_opponent_is_roark_only_after_the_trainers(Logger&){ + GymTrainers gym(oreburgh_gym_trainer_sights().size()); + if (gym.leader_next() || gym.next_label() != "Gym trainer 1 of 2"){ + return "At the door the opponent must be the first trainer, not Roark."; + } + gym.record_win(); + // The failing case: one trainer beaten, a blocked walk, a talk. + if (gym.leader_next() || gym.next_label() != "Gym trainer 2 of 2"){ + return "After one trainer a talk must be the second trainer's, not Roark's."; + } + gym.record_win(); + if (!gym.leader_next() || gym.next_label() != "Roark"){ + return "After both trainers the opponent must be Roark."; + } + gym.record_win(); + if (!gym.leader_next()){ + return "A win past the last trainer must not unset Roark."; + } + return true; +} + +// The Trainer Card check runs for the levels that check everything, fails +// on a missing badge, and lets a card the reader cannot read yet pass. +UnitTestResult test_badge_check(Logger&){ + using Pokemon::BadgeReading; + using Pokemon::StoryCheckLevel; + for (StoryCheckLevel level : {StoryCheckLevel::TESTING, StoryCheckLevel::THOROUGH}){ + if (!Pokemon::badge_check_wanted(level)){ + return "A thorough run must check the badge."; + } + } + for (StoryCheckLevel level : {StoryCheckLevel::DEFAULT, StoryCheckLevel::FAST, StoryCheckLevel::SPEEDRUN}){ + if (Pokemon::badge_check_wanted(level)){ + return "A run below thorough must not open the Trainer Card."; + } + } + if (Pokemon::badge_check_failure(BadgeReading::MISSING, "Coal Badge").find("Coal Badge") == std::string::npos){ + return "A missing badge must fail and name the badge."; + } + if (!Pokemon::badge_check_failure(BadgeReading::PRESENT, "Coal Badge").empty() + || !Pokemon::badge_check_failure(BadgeReading::UNREADABLE, "Coal Badge").empty() + ){ + return "A present or unreadable badge must not fail."; + } + return true; +} + UnitTestResult test_choose_move(Logger&){ struct Case{ std::string_view name; std::vector slots; size_t best; }; const Case cases[] = { @@ -675,6 +728,14 @@ const AutoStoryActionsCase AUTO_STORY_ACTIONS_CASES[] = { .input = {"wanderer talk: no ask from a tile that faces a sign (00.05b, the Poketch Company's sign first)", test_asks_someone_else}, .expectation = {}, }, + { + .input = {"01.04 gym: a talk before both trainers are beaten is not Roark's (lab-B 2026-11-12)", test_gym_opponent_is_roark_only_after_the_trainers}, + .expectation = {}, + }, + { + .input = {"badge check: thorough levels open the Trainer Card and a missing badge fails", test_badge_check}, + .expectation = {}, + }, { .input = {"battle moves: move menu screenshots in each language", test_move_menu_screenshots}, .expectation = {}, diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp new file mode 100644 index 0000000000..d009d28e65 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp @@ -0,0 +1,64 @@ +/* BDSP AutoStory - Badge Check + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.h" +#include "PokemonBDSP_AutoStory_Actions.h" +#include "PokemonBDSP_AutoStory_BadgeCheck.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +void verify_badge( + VideoStream& stream, ProControllerContext& context, + Pokemon::StoryCheckLevel level, size_t index, std::string_view badge, int menu_icons +){ + if (!Pokemon::badge_check_wanted(level)){ + return; + } + open_menu(stream, context, MenuCursorPosition::CARD, menu_icons); + // The card slides in. + context.wait_for(2000ms); + context.wait_for_all_requests(); + const VideoSnapshot card = stream.video().snapshot(); + if (!card){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Badge check: no frame of the Trainer Card.", stream); + } + // Kept for the reader's fixtures (Pokemon::BadgeReading). + const std::string folder = SCREENSHOTS_PATH() + "AutoStory/cards/"; + Filesystem::create_directories(Filesystem::Path(folder)); + const std::string path = folder + now_to_filestring() + ".png"; + card.frame->save(path); + stream.log("Trainer Card screenshot: " + path); + + const Pokemon::BadgeReading reading = read_trainer_card_badge(*card.frame, index); + const std::string failure = Pokemon::badge_check_failure(reading, badge); + if (!failure.empty()){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, failure, stream); + } + pbf_mash_button(context, BUTTON_B, 3000ms); + context.wait_for_all_requests(); + stream.log(reading == Pokemon::BadgeReading::PRESENT + ? std::string("Badge check: the ") + std::string(badge) + " is on the Trainer Card." + : std::string("Badge check: the Trainer Card reader has no reference yet; not checked (screenshot kept).")); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.h new file mode 100644 index 0000000000..62655e250c --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.h @@ -0,0 +1,39 @@ +/* BDSP AutoStory - Badge Check + * + * From: https://github.com/PokemonAutomation/ + * + * Opens the Trainer Card and checks a badge is on it. The shared verdict is + * Pokemon_BadgeCheck.h's. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_BadgeCheck_H +#define PokemonAutomation_PokemonBDSP_AutoStory_BadgeCheck_H + +#include +#include +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// When `level` asks for it, opens the Trainer Card from the overworld, saves +// its screenshot, reads badge `index` (menu with `menu_icons` icons, see +// open_menu()) and backs out to the overworld. Fails +// the step with a screenshot when the badge is missing; a card the reader +// cannot read yet is logged and passes. +void verify_badge( + VideoStream& stream, ProControllerContext& context, + Pokemon::StoryCheckLevel level, size_t index, std::string_view badge, int menu_icons +); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp index 6ce669a041..592b5ef583 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp @@ -12,10 +12,13 @@ #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryTrainers.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP_AutoStory_BadgeCheck.h" +#include "PokemonBDSP_AutoStory_GymOpponent.h" #include "PokemonBDSP_AutoStory_Talks.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h" #include "PokemonBDSP_AutoStory_WildBattle.h" @@ -66,17 +69,50 @@ void oreburgh_street_to_gym(VideoStream& stream, ProControllerContext& context){ fail_step(stream, "Oreburgh: no column near the gym's door went in."); } +// How a meeting with someone in the gym ended. +enum class GymMeeting{ + TALKED, // No battle: someone beaten, or a line only. + LOST, // A whiteout; we are in the Pokemon Center. + TRAINER_WON, + LEADER_WON, +}; + +// A talk has opened: the opponent is whoever `trainers` says is next, never +// "whoever the walk met" (lab-B, 2026-11-12 22:50: the second trainer, +// met by a blocked walk and an A press, was fought as Roark, and 01.05 +// saved with no badge). +GymMeeting meet_gym_opponent(VideoStream& stream, ProControllerContext& context, GymTrainers& trainers){ + const bool leader = trainers.leader_next(); + const std::string label = trainers.next_label(); + // His lines pause for more than 4 s before the battle (r210d: "only + // talked"), so Roark's battle is expected; his badge speech is over + // when talk_or_fight() returns (confirm_battle_overworld()). + const bool fought = talk_or_fight(stream, context, label, leader ? TalkExpect::BATTLE : TalkExpect::TALK_OR_BATTLE); + if (!fought){ + return GymMeeting::TALKED; + } + if (in_pokemon_center(stream)){ + return GymMeeting::LOST; + } + if (leader){ + return GymMeeting::LEADER_WON; + } + trainers.record_win(); + return GymMeeting::TRAINER_WON; +} + // Inside the Oreburgh Gym: straight up the middle, fighting the trainers // who stop us, to Roark on the top tier. Returns whether Roark was beaten; // false after a whiteout, which leaves us in the Pokemon Center. // Walking straight up meets both gym trainers (one at the entrance, one on // the second tier); their experience helps an under-levelled team, and -// after a whiteout they stay beaten. Butters' route goes round them. -bool oreburgh_gym_battles(VideoStream& stream, ProControllerContext& context){ +// after a whiteout they stay beaten (`trainers` keeps the count). Butters' +// route goes round them. +bool oreburgh_gym_battles(VideoStream& stream, ProControllerContext& context, GymTrainers& trainers){ // The entry camera pans for about 7.5 s (Butters' reference video). context.wait_for(8000ms); if (dialogue_opens(stream, context, 3000ms)){ - if (talk_or_fight(stream, context, "Gym trainer at the entrance") && in_pokemon_center(stream)){ + if (meet_gym_opponent(stream, context, trainers) == GymMeeting::LOST){ return false; } } @@ -84,8 +120,13 @@ bool oreburgh_gym_battles(VideoStream& stream, ProControllerContext& context){ for (size_t leg = 0; leg < 10; leg++){ const WalkResult up = walk_until_no_wild(stream, context, {0, 1}, 8000ms, "north up the gym"); if (up.stop == WalkStop::DIALOG){ - if (talk_or_fight(stream, context, "Gym trainer") && in_pokemon_center(stream)){ + switch (meet_gym_opponent(stream, context, trainers)){ + case GymMeeting::LOST: return false; + case GymMeeting::LEADER_WON: + return true; + default: + break; } continue; } @@ -93,16 +134,21 @@ bool oreburgh_gym_battles(VideoStream& stream, ProControllerContext& context){ continue; } // Roark stands at the top facing us; a wall or a stair edge says - // nothing to A. Only Roark starts a battle from our A here. The walk - // left us facing north: a turn tap that way would be a step. + // nothing to A. Whoever answers is the next opponent in line, which + // is a gym trainer until both are beaten. The walk left us facing + // north: a turn tap that way would be a step. pbf_press_button(context, BUTTON_A, 80ms, 200ms); if (dialogue_opens(stream, context, 3000ms)){ - // His lines pause for more than 4 s before the battle (r210d: "only - // talked"), so the battle is expected. - // His badge speech is over when talk_or_fight() returns: the - // battle ends with its speeches (confirm_battle_overworld()). - if (talk_or_fight(stream, context, "Roark", TalkExpect::BATTLE)){ - return !in_pokemon_center(stream); + if (!trainers.leader_next()){ + stream.log("[01.04] Blocked with a gym trainer still to beat: the talk is not Roark's."); + } + switch (meet_gym_opponent(stream, context, trainers)){ + case GymMeeting::LOST: + return false; + case GymMeeting::LEADER_WON: + return true; + default: + break; } continue; } @@ -114,14 +160,15 @@ bool oreburgh_gym_battles(VideoStream& stream, ProControllerContext& context){ } // 01.04: west along the row into the gym and up it to Roark, again from the Center after a loss. -void gym_until_roark_beaten(VideoStream& stream, ProControllerContext& context){ +void gym_until_roark_beaten(VideoStream& stream, ProControllerContext& context, Pokemon::StoryCheckLevel checks){ // Losing costs some money and keeps the experience, so each try comes // back stronger: Roark won twice by hand against Piplup Lv 10 and 14 and // lost the third time. + GymTrainers trainers(oreburgh_gym_trainer_sights().size()); bool won = false; for (size_t attempt = 0; attempt < 5 && !won; attempt++){ oreburgh_street_to_gym(stream, context); - won = oreburgh_gym_battles(stream, context); + won = oreburgh_gym_battles(stream, context, trainers); if (!won){ stream.log("[01.04] Lost in the gym (try " + std::to_string(attempt + 1) + "); back from the Center.", COLOR_ORANGE); walk_out_of_center(stream, context, "Oreburgh"); @@ -130,13 +177,18 @@ void gym_until_roark_beaten(VideoStream& stream, ProControllerContext& context){ if (!won){ fail_step(stream, "Oreburgh Gym: lost to Roark five times."); } + // The Coal Badge is the gym's first (menu with 5 icons: no Map yet). + verify_badge(stream, context, checks, 0, "Coal Badge", 5); } } // namespace void add_oreburgh_events(StoryEvents& events){ - events.emplace("gym_until_roark_beaten", console_event(gym_until_roark_beaten)); + events.emplace("gym_until_roark_beaten", [](StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep&){ + gym_until_roark_beaten(executor.stream(), executor.context(), executor.env().checks); + return Pokemon::outcome_ok(); + }); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h new file mode 100644 index 0000000000..16348c5e43 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h @@ -0,0 +1,62 @@ +/* BDSP AutoStory - Gym opponent + * + * From: https://github.com/PokemonAutomation/ + * + * Who the person in front of us is on the way up the Oreburgh Gym. 01.04 + * walks straight up and is stopped by the gym trainers; when the walk was + * blocked and an A press opened a talk, the talk was taken for Roark's, and + * the second trainer's fight ended the step with no badge (lab-B, + * 2026-11-12 22:50). Roark is next only once every trainer in the gym is + * beaten; a talk before that is a trainer's. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_GymOpponent_H +#define PokemonAutomation_PokemonBDSP_AutoStory_GymOpponent_H + +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The gym trainers beaten so far, kept across the tries of a step: a trainer +// beaten stays beaten after a whiteout. +class GymTrainers{ +public: + // `total`: the trainers between the door and the leader. + explicit GymTrainers(size_t total) + : m_total(total) + {} + + // A trainer's battle was won. + void record_win(){ + if (m_beaten < m_total){ + m_beaten++; + } + } + // Whether the next opponent is the leader. + bool leader_next() const{ + return m_beaten >= m_total; + } + // The next opponent's name, for the log and the battle. + std::string next_label() const{ + if (leader_next()){ + return "Roark"; + } + return "Gym trainer " + std::to_string(m_beaten + 1) + " of " + std::to_string(m_total); + } + +private: + size_t m_total; + size_t m_beaten = 0; +}; + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.cpp new file mode 100644 index 0000000000..3e6160a09f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.cpp @@ -0,0 +1,23 @@ +/* BDSP AutoStory - Trainer Card Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonBDSP_AutoStory_TrainerCardReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +Pokemon::BadgeReading read_trainer_card_badge(const ImageViewRGB32&, size_t){ + // See the header: the badge boxes wait for a fixture. + return Pokemon::BadgeReading::UNREADABLE; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.h new file mode 100644 index 0000000000..a226c769e9 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.h @@ -0,0 +1,36 @@ +/* BDSP AutoStory - Trainer Card Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Reads the badges on the Trainer Card (menu icon CARD). + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_TrainerCardReader_H +#define PokemonAutomation_PokemonBDSP_AutoStory_TrainerCardReader_H + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "Pokemon/AutoStory/Types/Pokemon_BadgeCheck.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Whether badge `index` (0 = Coal Badge, in the order the gyms are beaten) +// is on the card shown on `screen`. +// +// TODO: no Trainer Card screenshot exists yet (CommandLineTests has none and +// the lab has only save-prompt shots), so the badge boxes are not known and +// this reads UNREADABLE. verify_badge() saves the card it opened to +// Screenshots/AutoStory/cards/; a card with 0 badges and one with 1 badge +// from console B are the fixtures to draw the boxes and thresholds from. +Pokemon::BadgeReading read_trainer_card_badge(const ImageViewRGB32& screen, size_t index); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp index 9e10c1ac1f..7fa21d38ea 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp @@ -246,7 +246,7 @@ void BDSPAutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerCo // The reads that use text (Location.h) take the game's language from here. set_story_language(RUN.language()); - StoryEnv story{env, context, STARTER, PLAYER_NAME}; + StoryEnv story{env, context, STARTER, PLAYER_NAME, RUN.check_level()}; StoryExecutor executor(story); executor.set_stop_rows(rows_that_stop(checkpoints)); // A start with a save always loads it first, so the run never walks diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h index 2170a0e0ad..077b5c0b8c 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h @@ -42,6 +42,7 @@ #include "Pokemon/AutoStory/Pokemon_StorySegments.h" #include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" #include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h" @@ -88,6 +89,8 @@ struct StoryEnv{ ProControllerContext& context; StarterChoice starter = StarterChoice::CHIMCHAR; std::string player_name; + // How much the steps confirm on screen (StoryRunOptions::check_level()). + Pokemon::StoryCheckLevel checks = Pokemon::StoryCheckLevel::DEFAULT; }; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 25156664aa..22b754194d 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1654,6 +1654,8 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.h Source/Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.cpp Source/Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h + Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.cpp + Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.h Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.cpp Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.h Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h @@ -1723,6 +1725,8 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.h Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.h Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.cpp Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.h Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.cpp @@ -1741,6 +1745,7 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Route204.cpp Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Sandgem.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroProbe.h Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroSteps.h Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_LitDoorway.h @@ -1788,6 +1793,8 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.cpp Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.h + Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.cpp + Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.cpp From 68d2e900eb1e7f8ea49c5fa68fdcc6b22aea8753 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 00:11:51 -0800 Subject: [PATCH 2/5] BDSP AutoStory 01.04: Roark told by the battle's intro text; an unreadable Trainer Card fails the badge check Roark was taken for a gym trainer after a reload (lab-B 2026-11-12 23:49), and the replacement, GymColumn (tiles from time walked), is dead reckoning too. Now BattleIntroWatcher reads "You are challenged by Gym Leader Roark!" against "... by !" in the battle's text box while it shows (English only; UNREAD otherwise), fight_story_battle() returns it, and the gym step fails with a screenshot when the intro was not read instead of guessing. GymColumn stays as a log hint and for how long to wait for Roark's battle. The Trainer Card check now fails on an unreadable card (no fixture exists yet, so the reader is still a stub). PotionLedger from 333dc8701: Potions stop after one that gained no HP. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Types/Pokemon_BadgeCheck.cpp | 8 +- .../AutoStory/Types/Pokemon_BadgeCheck.h | 10 +- .../PokemonBDSP_AutoStory_Actions_Tests.cpp | 163 +++++++++++++++--- .../PokemonBDSP_AutoStory_BadgeCheck.cpp | 4 +- .../PokemonBDSP_AutoStory_BadgeCheck.h | 4 +- .../Actions/PokemonBDSP_AutoStory_Battle.cpp | 66 +++++-- .../Actions/PokemonBDSP_AutoStory_Battle.h | 62 ++++++- .../PokemonBDSP_AutoStory_Events_Oreburgh.cpp | 74 +++++--- .../PokemonBDSP_AutoStory_GymOpponent.h | 69 +++++--- .../Actions/PokemonBDSP_AutoStory_TalkFight.h | 37 ++++ .../Actions/PokemonBDSP_AutoStory_Talks.cpp | 11 +- .../PokemonBDSP_AutoStory_BattleIntro.cpp | 158 +++++++++++++++++ .../PokemonBDSP_AutoStory_BattleIntro.h | 92 ++++++++++ .../PokemonBDSP_AutoStory_TrainerCardReader.h | 3 +- SerialPrograms/cmake/SourceFiles.cmake | 3 + 15 files changed, 657 insertions(+), 107 deletions(-) create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkFight.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.cpp index ab7af5b1c1..027f91a463 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.cpp @@ -16,9 +16,15 @@ bool badge_check_wanted(StoryCheckLevel level){ } std::string badge_check_failure(BadgeReading reading, std::string_view badge){ - if (reading != BadgeReading::MISSING){ + if (reading == BadgeReading::PRESENT){ return ""; } + if (reading == BadgeReading::UNREADABLE){ + return std::format( + "The Trainer Card could not be read, so the {} is not confirmed (the card's screenshot is kept).", + badge + ); + } return std::format( "The Trainer Card has no {}: the battle that ended the step was not the gym leader's.", badge diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.h index 2030bd47e8..b2f4c76dcf 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.h @@ -25,7 +25,8 @@ namespace Pokemon{ enum class BadgeReading{ PRESENT, MISSING, - // The reader has no reference for the card yet, or the card was not on screen. + // The reader has no reference for the card yet, or the card was not on + // screen. A badge not seen is not a badge confirmed: this fails too. UNREADABLE, }; @@ -33,9 +34,10 @@ enum class BadgeReading{ // the levels that check everything for the user. bool badge_check_wanted(StoryCheckLevel level); -// The failure text when the badge is missing, "" when `reading` is fine. -// An UNREADABLE card does not fail the step: the reader has nothing to say -// yet, and the caller logs it and keeps the card's screenshot. +// The failure text when the badge is missing or the card was not read, "" +// when the badge was seen. The check runs only at the levels that check +// everything (badge_check_wanted()), and there an unreadable card must not +// pass as a badge confirmed; the caller keeps the card's screenshot. std::string badge_check_failure(BadgeReading reading, std::string_view badge); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp index d271f1c9c4..bc97d706cd 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp @@ -9,6 +9,7 @@ * */ +#include #include #include #include @@ -29,6 +30,7 @@ #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" #include "PokemonBDSP_AutoStory_BagScreen.h" +#include "PokemonBDSP_AutoStory_Battle.h" #include "PokemonBDSP_AutoStory_BattleMoves.h" #include "PokemonBDSP_AutoStory_Center.h" #include "PokemonBDSP_AutoStory_Events.h" @@ -496,32 +498,132 @@ UnitTestResult test_asks_someone_else(Logger&){ return true; } -// 01.04 (lab-B, 2026-11-12 22:50): the walk up the gym was blocked by the -// second trainer, an A press opened his talk, and it was taken for Roark's. -// A talk before every trainer is beaten is a trainer's, however it began. -UnitTestResult test_gym_opponent_is_roark_only_after_the_trainers(Logger&){ - GymTrainers gym(oreburgh_gym_trainer_sights().size()); - if (gym.leader_next() || gym.next_label() != "Gym trainer 1 of 2"){ - return "At the door the opponent must be the first trainer, not Roark."; +// 01.04 (lab-B, 2026-11-12 22:50 and 23:49): the opponent is read from the +// battle's intro text, not told by a count that a reload loses (Roark was +// "Gym trainer 1 of 2" after a reload) or by the time walked. +UnitTestResult test_battle_intro_tells_the_opponent(Logger&){ + struct Case{ std::string_view text; BattleOpponent opponent; }; + const Case cases[] = { + {"You are challenged by Gym Leader Roark!", BattleOpponent::LEADER}, + {"You are challenged by Gym Leader", BattleOpponent::LEADER}, + {"You are challenged by Gym Leacler Roark!", BattleOpponent::LEADER}, + {"You are challenged by Youngster Tristan!", BattleOpponent::TRAINER}, + {"You are challenged by Hiker Mark!\n", BattleOpponent::TRAINER}, + // Still being typed out: no verdict from "by Y". + {"You are challenged by Y", BattleOpponent::UNREAD}, + {"You are challenged by", BattleOpponent::UNREAD}, + {"You are chal", BattleOpponent::UNREAD}, + {"Go! Turtwig!", BattleOpponent::UNREAD}, + {"", BattleOpponent::UNREAD}, + {" \n ", BattleOpponent::UNREAD}, + {"!!!", BattleOpponent::UNREAD}, + }; + for (const Case& c : cases){ + if (classify_battle_intro(c.text) != c.opponent){ + return std::format("\"{}\" read as {}, not {}.", c.text, + battle_opponent_name(classify_battle_intro(c.text)), battle_opponent_name(c.opponent)); + } } - gym.record_win(); - // The failing case: one trainer beaten, a blocked walk, a talk. - if (gym.leader_next() || gym.next_label() != "Gym trainer 2 of 2"){ - return "After one trainer a talk must be the second trainer's, not Roark's."; + return true; +} + +// A trainer's line is final when read the same twice; a half-typed one is +// not, and nothing read stays unread. The leader's settles at once. +UnitTestResult test_battle_intro_reading_settles(Logger&){ + BattleIntroReading trainer; + if (trainer.add("You are challenged by Youngster Tr") != BattleOpponent::UNREAD){ + return "A line read once is not final."; } - gym.record_win(); - if (!gym.leader_next() || gym.next_label() != "Roark"){ - return "After both trainers the opponent must be Roark."; + if (trainer.add("You are challenged by Youngster Tristan!") != BattleOpponent::UNREAD){ + return "A line that changed since the last read is still being typed."; } - gym.record_win(); - if (!gym.leader_next()){ - return "A win past the last trainer must not unset Roark."; + if (trainer.add("") != BattleOpponent::UNREAD || trainer.add("You are challenged by Youngster Tristan!") != BattleOpponent::UNREAD){ + return "An unread frame between two reads breaks the pair."; + } + if (trainer.add("You are challenged by Youngster Tristan!") != BattleOpponent::TRAINER){ + return "The same line twice in a row is a trainer's."; + } + // The leader's line settles at once, and a later read does not undo it. + BattleIntroReading leader; + if (leader.add("You are challenged by Gym Leader Roark!") != BattleOpponent::LEADER + || leader.add("You are challenged by Youngster Tristan!") != BattleOpponent::LEADER + ){ + return "Gym Leader in the intro is Roark's, at once and for good."; + } + BattleIntroReading none; + if (none.add("") != BattleOpponent::UNREAD || none.verdict() != BattleOpponent::UNREAD){ + return "Nothing read must stay unread."; } return true; } -// The Trainer Card check runs for the levels that check everything, fails -// on a missing badge, and lets a card the reader cannot read yet pass. +// The column's time estimate is a hint only now: it must still count and +// restart, and a negative walk adds nothing. +UnitTestResult test_gym_column_is_a_hint(Logger&){ + GymColumn column(6, 9, 240ms); + if (column.leader_here() || column.tiles() != 0){ + return "At the entrance the hint is a trainer's tile."; + } + column.walked_north(240ms * 8); + if (!column.leader_here()){ + return "Past the halfway point to Roark the hint says his tile."; + } + column.walked_north(-100ms); + if (column.tiles() != 8){ + return "A negative walk must not move the hint."; + } + column.restart(); + if (column.leader_here()){ + return "After a restart the hint is a trainer's tile."; + } + return true; +} + +// Roark, lab-B 2026-11-12 23:49: four Potions at 26%, 26%, 29%, 29%. A Potion +// that gained nothing ends the Potions of the battle. +UnitTestResult test_potion_ledger_stops_without_gain(Logger&){ + PotionLedger potions; + potions.observe(0.26); + if (!potions.may_use()){ + return "The first Potion must be allowed."; + } + potions.used(0.26); + if (!potions.pending()){ + return "A Potion waits for its HP read."; + } + potions.observe(-1); + if (!potions.pending() || potions.stalled()){ + return "An unread bar must not settle a Potion."; + } + potions.observe(0.26); + if (!potions.stalled() || potions.may_use()){ + return "No gain (26% then 26%) must stop the Potions."; + } + // The gain case: a Potion that raised HP allows the next while it is low. + PotionLedger gaining; + gaining.used(0.20); + gaining.observe(0.45); + if (gaining.stalled() || !gaining.may_use()){ + return "A Potion that gained HP must not stop the Potions."; + } + // At most the cap, and none after the Bag runs out. + for (size_t n = 1; n < PotionLedger::MAX_POTIONS; n++){ + gaining.used(0.20); + gaining.observe(0.20 + 0.01 * (double)n); + } + if (gaining.may_use()){ + return "No more than the cap of Potions in a battle."; + } + PotionLedger empty; + empty.out_of_potions(); + if (empty.may_use()){ + return "No Potion once the Bag has none."; + } + return true; +} + +// The Trainer Card check runs for the levels that check everything and fails +// on a missing badge and on a card the reader cannot read. UnitTestResult test_badge_check(Logger&){ using Pokemon::BadgeReading; using Pokemon::StoryCheckLevel; @@ -538,10 +640,11 @@ UnitTestResult test_badge_check(Logger&){ if (Pokemon::badge_check_failure(BadgeReading::MISSING, "Coal Badge").find("Coal Badge") == std::string::npos){ return "A missing badge must fail and name the badge."; } - if (!Pokemon::badge_check_failure(BadgeReading::PRESENT, "Coal Badge").empty() - || !Pokemon::badge_check_failure(BadgeReading::UNREADABLE, "Coal Badge").empty() - ){ - return "A present or unreadable badge must not fail."; + if (!Pokemon::badge_check_failure(BadgeReading::PRESENT, "Coal Badge").empty()){ + return "A present badge must not fail."; + } + if (Pokemon::badge_check_failure(BadgeReading::UNREADABLE, "Coal Badge").find("Coal Badge") == std::string::npos){ + return "An unreadable card must fail and name the badge."; } return true; } @@ -729,7 +832,19 @@ const AutoStoryActionsCase AUTO_STORY_ACTIONS_CASES[] = { .expectation = {}, }, { - .input = {"01.04 gym: a talk before both trainers are beaten is not Roark's (lab-B 2026-11-12)", test_gym_opponent_is_roark_only_after_the_trainers}, + .input = {"01.04 gym: the opponent is read from the battle's intro text, not a count or the tiles walked (lab-B 2026-11-12)", test_battle_intro_tells_the_opponent}, + .expectation = {}, + }, + { + .input = {"01.04 gym: the intro reading settles on a repeated trainer line or a leader's line", test_battle_intro_reading_settles}, + .expectation = {}, + }, + { + .input = {"01.04 gym: the column's tile estimate is a hint only", test_gym_column_is_a_hint}, + .expectation = {}, + }, + { + .input = {"Battle: a Potion that gained no HP ends the Potions of the battle (Roark, lab-B 2026-11-12 23:49)", test_potion_ledger_stops_without_gain}, .expectation = {}, }, { diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp index d009d28e65..f0a3686db2 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp @@ -52,9 +52,7 @@ void verify_badge( } pbf_mash_button(context, BUTTON_B, 3000ms); context.wait_for_all_requests(); - stream.log(reading == Pokemon::BadgeReading::PRESENT - ? std::string("Badge check: the ") + std::string(badge) + " is on the Trainer Card." - : std::string("Badge check: the Trainer Card reader has no reference yet; not checked (screenshot kept).")); + stream.log(std::string("Badge check: the ") + std::string(badge) + " is on the Trainer Card."); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.h index 62655e250c..0371d14752 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.h @@ -24,8 +24,8 @@ namespace PokemonBDSP{ // When `level` asks for it, opens the Trainer Card from the overworld, saves // its screenshot, reads badge `index` (menu with `menu_icons` icons, see // open_menu()) and backs out to the overworld. Fails -// the step with a screenshot when the badge is missing; a card the reader -// cannot read yet is logged and passes. +// the step with a screenshot when the badge is missing or the card cannot be +// read: nothing is passed unseen. void verify_badge( VideoStream& stream, ProControllerContext& context, Pokemon::StoryCheckLevel level, size_t index, std::string_view badge, int menu_icons diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp index e177f711a6..d68e4a87c6 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp @@ -10,8 +10,10 @@ #include #include #include +#include #include "Common/Cpp/Filesystem/Filesystem.h" #include "Common/Cpp/PrettyPrint.h" +#include "Common/Cpp/Time.h" #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/GlobalAutoPaths.h" #include "CommonFramework/ImageTools/ImageBoxes.h" @@ -24,9 +26,11 @@ #include "PokemonBDSP/Inference/Battles/PokemonBDSP_EndBattleDetector.h" #include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" #include "PokemonBDSP/Inference/PokemonBDSP_SelectionArrow.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h" #include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.h" #include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" #include "PokemonBDSP_AutoStory_Bag.h" #include "PokemonBDSP_AutoStory_BattleMoves.h" #include "PokemonBDSP_AutoStory_TalkOver.h" @@ -216,10 +220,21 @@ enum class BattleHeal{ NOT_NEEDED, USED, NONE_LEFT }; // from the Bag on the lead, which takes the turn. Otherwise the // battle menu is left up. BattleHeal battle_heal_if_low(VideoStream& stream, ProControllerContext& context, - BattleType battle_type, double heal_below, const std::string& label){ + BattleType battle_type, double heal_below, const std::string& label, PotionLedger& potions){ + // The menu comes up after the turn's animations; the bar settles a + // moment later, so read it after a short wait when a Potion is settling. + if (potions.pending()){ + pbf_wait(context, 600ms); + } const double hp = read_battle_hp(stream, context); stream.log("[" + label + "] Our HP bar reads " + std::to_string((int)(hp * 100)) + "%."); - if (hp < 0 || hp >= heal_below){ + const size_t before_count = potions.count(); + potions.observe(hp); + if (potions.stalled() && before_count > 0){ + stream.log("[" + label + "] The last Potion gained no HP (read " + std::to_string((int)(hp * 100)) + + "%): the foe's hits take it back, or it was not used. No more Potions this battle; fighting on.", COLOR_ORANGE); + } + if (hp < 0 || hp >= heal_below || !potions.may_use()){ return BattleHeal::NOT_NEEDED; } if (!cursor_to_bag(stream, context, battle_type)){ @@ -240,6 +255,7 @@ BattleHeal battle_heal_if_low(VideoStream& stream, ProControllerContext& context pbf_press_button(context, BUTTON_A, 80ms, 1500ms); pbf_press_button(context, BUTTON_A, 80ms, 2500ms); context.wait_for_all_requests(); + potions.used(hp); return BattleHeal::USED; } @@ -255,8 +271,7 @@ struct BestMoveBattle{ // What a battle has done so far, across turns. struct BattleProgress{ bool battle_menu_seen = false; - bool out_of_potions = false; - size_t potions_used = 0; + PotionLedger potions; size_t turns = 0; size_t faint_switches = 0; }; @@ -276,13 +291,14 @@ void take_turn( } stream.log("[AutoStory] Battle menu detected.", COLOR_BLUE); // A trainer battle we may lose: a Potion when HP is low. - if (battle.battle_type == BattleType::TRAINER && !progress.out_of_potions && progress.potions_used < 4){ - const BattleHeal heal = battle_heal_if_low(stream, context, battle.battle_type, 0.35, label); + if (battle.battle_type == BattleType::TRAINER && progress.potions.may_use()){ + const BattleHeal heal = battle_heal_if_low(stream, context, battle.battle_type, 0.35, label, progress.potions); if (heal == BattleHeal::USED){ - progress.potions_used++; return; } - progress.out_of_potions = heal == BattleHeal::NONE_LEFT; + if (heal == BattleHeal::NONE_LEFT){ + progress.potions.out_of_potions(); + } } stream.log("[" + label + "] Move list position 1: use move 0."); use_best_move(stream, context, battle.battle_type, label); @@ -340,11 +356,33 @@ int wait_for_battle_screen( ); } +// The battle's opening transition, 5 s as it always was. A trainer's battle +// starts with "You are challenged by ...": it is read while it shows, before +// anything presses through it. +BattleOpponent wait_out_battle_start(VideoStream& stream, ProControllerContext& context, const BestMoveBattle& battle, const std::string& label){ + constexpr Milliseconds TRANSITION = 5000ms; + if (battle.battle_type != BattleType::TRAINER || !story_reads_text()){ + pbf_wait(context, TRANSITION); + return BattleOpponent::UNREAD; + } + context.wait_for_all_requests(); + BattleIntroWatcher intro; + const WallClock start = current_time(); + wait_until(stream, context, TRANSITION, {{intro}}); + const Milliseconds elapsed = std::chrono::duration_cast(current_time() - start); + if (elapsed < TRANSITION){ + pbf_wait(context, TRANSITION - elapsed); + } + const BattleOpponent opponent = intro.verdict(); + stream.log(std::format("[{}] The battle's intro text read \"{}\": the opponent is {}.", + label, intro.last_text(), battle_opponent_name(opponent))); + return opponent; +} + // Fights the battle on screen to its end and the overworld after it. -void fight_with_best_move(VideoStream& stream, ProControllerContext& context, const BestMoveBattle& battle, const std::string& label){ +BattleOpponent fight_with_best_move(VideoStream& stream, ProControllerContext& context, const BestMoveBattle& battle, const std::string& label){ stream.log("[AutoStory] Starting " + label + " battle...", COLOR_BLUE); - // Preserve the existing early-game transition delay. - pbf_wait(context, 5000ms); + const BattleOpponent opponent = wait_out_battle_start(stream, context, battle, label); BattleProgress progress; EndBattleWatcher end_battle; while (true){ @@ -353,7 +391,7 @@ void fight_with_best_move(VideoStream& stream, ProControllerContext& context, co take_turn(stream, context, battle, progress, label); }else if (ret == 1){ finish_battle(stream, context, label); - return; + return opponent; }else if (ret == 2){ stream.log("[" + label + "] Declining move learning; preserving scripted move positions."); pbf_move_right_joystick(context, {0, -1}, 160ms, 840ms); @@ -381,9 +419,9 @@ void confirm_battle_overworld(VideoStream& stream, ProControllerContext& context talk_over(stream, context, label); } -void fight_story_battle(VideoStream& stream, ProControllerContext& context, const std::string& label, BattleFormat format){ +BattleOpponent fight_story_battle(VideoStream& stream, ProControllerContext& context, const std::string& label, BattleFormat format){ // The best move each turn; the next Pokemon after a faint; no new moves. - fight_with_best_move(stream, context, {.battle_type = BattleType::TRAINER, .format = format, .switch_on_faint = true}, label); + return fight_with_best_move(stream, context, {.battle_type = BattleType::TRAINER, .format = format, .switch_on_faint = true}, label); } void fight_trainer(VideoStream& stream, ProControllerContext& context, const std::string& label){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h index 371ab3877e..e4dbe9df04 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h @@ -17,6 +17,7 @@ #include "CommonTools/VisualDetector.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -35,6 +36,60 @@ enum class BattleFormat{ DOUBLES, }; +// Whether Potions are working in a battle. A Potion takes the turn; when our +// HP at the next battle menu is no higher than before it, the heal is lost to +// the foe's hits (or the Bag flow did not use it), and more Potions would only +// hand over turns (Roark, lab-B 2026-11-12 23:49: 26%, 26%, 29%, 29% over four +// Potions). FRLG's battle policy skips a futile heal the same way, from the +// foe's mean hit; BDSP has no foe model, so the HP read after the turn is the +// net of the heal and the hit. After a Potion that gained nothing, none is +// used again this battle: the fight goes on with moves. +class PotionLedger{ +public: + // At a battle menu with our HP read (-1 unread). Settles the last + // Potion, if any: it gained when `hp` is above the HP it was used at. + void observe(double hp){ + if (m_before < 0 || hp < 0){ + // Nothing to settle, or an unread bar: no verdict either way. + return; + } + m_stalled = hp <= m_before; + m_before = -1; + } + // Whether a Potion may be used now. + bool may_use() const{ + return !m_stalled && !m_out && m_used < MAX_POTIONS; + } + // A Potion was used while our HP read `hp_before` (-1 unread). + void used(double hp_before){ + m_used++; + m_before = hp_before; + } + // The Bag has no Potion left. + void out_of_potions(){ + m_out = true; + } + // Whether the last Potion gained nothing (log text for the caller). + bool stalled() const{ + return m_stalled; + } + size_t count() const{ + return m_used; + } + // Whether a Potion is waiting for the HP read that settles it. + bool pending() const{ + return m_before >= 0; + } + + static constexpr size_t MAX_POTIONS = 4; + +private: + size_t m_used = 0; + double m_before = -1; + bool m_stalled = false; + bool m_out = false; +}; + // The speeches after a battle, to their end: talk_over(), B through the // lines until the overworld is quiet. Roark's badge lines pause for the // fanfare, and someone may walk off before the next line, so a moment @@ -45,8 +100,11 @@ void confirm_battle_overworld(VideoStream& stream, ProControllerContext& context // under 35 %, the next Pokemon when one faints, and "Keep old moves" // on level-up prompts. `doubles` confirms the default target each // turn in DOUBLES. Returns once the battle has ended, -// won or lost; after a loss we stand in the last Pokemon Center. -void fight_story_battle(VideoStream& stream, ProControllerContext& context, const std::string& label, BattleFormat format); +// won or lost; after a loss we stand in the last Pokemon Center. Returns who +// the battle's intro text ("You are challenged by ...") said the opponent +// was: UNREAD when it was not read (no English text reader, or the line was +// gone before the first read), never a guess. +BattleOpponent fight_story_battle(VideoStream& stream, ProControllerContext& context, const std::string& label, BattleFormat format); // Any trainer battle, whatever the starter: the best move on the menu each // turn until the battle ends, then the text after it. diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp index 592b5ef583..c8b4714ee9 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp @@ -9,16 +9,20 @@ * */ +#include +#include #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryTrainers.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteTiming.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" #include "PokemonBDSP_AutoStory_BadgeCheck.h" #include "PokemonBDSP_AutoStory_GymOpponent.h" +#include "PokemonBDSP_AutoStory_TalkFight.h" #include "PokemonBDSP_AutoStory_Talks.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h" #include "PokemonBDSP_AutoStory_WildBattle.h" @@ -69,6 +73,18 @@ void oreburgh_street_to_gym(VideoStream& stream, ProControllerContext& context){ fail_step(stream, "Oreburgh: no column near the gym's door went in."); } +// The gym's column as tiles north of the entrance: where the farthest gym +// trainer stops us, and the tile in front of Roark. +GymColumn oreburgh_gym_column(){ + const Pokemon::TilePos entrance = map_point("OreburghGym", "entrance"); + const Pokemon::TilePos roark = map_point("OreburghGym", "roark"); + int last_trainer = 0; + for (const TrainerSight& trainer : oreburgh_gym_trainer_sights()){ + last_trainer = std::max(last_trainer, entrance.y - trainer.pos.y); + } + return GymColumn(last_trainer, entrance.y - roark.y - 1, ROUTE_MS_PER_TILE_UP_DOWN); +} + // How a meeting with someone in the gym ended. enum class GymMeeting{ TALKED, // No battle: someone beaten, or a line only. @@ -77,28 +93,37 @@ enum class GymMeeting{ LEADER_WON, }; -// A talk has opened: the opponent is whoever `trainers` says is next, never -// "whoever the walk met" (lab-B, 2026-11-12 22:50: the second trainer, -// met by a blocked walk and an A press, was fought as Roark, and 01.05 -// saved with no badge). -GymMeeting meet_gym_opponent(VideoStream& stream, ProControllerContext& context, GymTrainers& trainers){ - const bool leader = trainers.leader_next(); - const std::string label = trainers.next_label(); - // His lines pause for more than 4 s before the battle (r210d: "only - // talked"), so Roark's battle is expected; his badge speech is over - // when talk_or_fight() returns (confirm_battle_overworld()). - const bool fought = talk_or_fight(stream, context, label, leader ? TalkExpect::BATTLE : TalkExpect::TALK_OR_BATTLE); - if (!fought){ +// A talk has opened. Who it is, is what the battle's intro text says ("You +// are challenged by Gym Leader Roark!"), read on the screen by +// fight_story_battle(): never the count of trainers beaten or the distance +// walked (lab-B, 2026-11-12 22:50: the second trainer, met by a blocked walk +// and an A press, was fought as Roark, and 01.05 saved with no badge; 23:49: +// after a reload Roark was "Gym trainer 1"). The column only hints, and says +// how to wait: Roark's lines pause for more than 4 s before the battle +// (r210d: "only talked"), so up at his tile a battle is expected. An intro +// that was not read fails the step: the next one would be a guess. +GymMeeting meet_gym_opponent(VideoStream& stream, ProControllerContext& context, const GymColumn& column){ + const bool leader_hint = column.leader_here(); + stream.log(std::format("[01.04] About {:.1f} tiles up the gym (a hint, not the opponent: {}).", + column.tiles(), leader_hint ? "Roark's tile" : "a trainer's")); + // His badge speech is over when talk_or_fight_who() returns (confirm_battle_overworld()). + const TalkFight met = talk_or_fight_who(stream, context, "Gym opponent", leader_hint ? TalkExpect::BATTLE : TalkExpect::TALK_OR_BATTLE); + if (!met.fought){ return GymMeeting::TALKED; } if (in_pokemon_center(stream)){ return GymMeeting::LOST; } - if (leader){ + stream.log(std::format("[01.04] The intro text says the opponent was the {}.", battle_opponent_name(met.opponent))); + switch (met.opponent){ + case BattleOpponent::LEADER: return GymMeeting::LEADER_WON; + case BattleOpponent::TRAINER: + return GymMeeting::TRAINER_WON; + case BattleOpponent::UNREAD: + break; } - trainers.record_win(); - return GymMeeting::TRAINER_WON; + fail_step(stream, "Oreburgh Gym: the battle's intro text was not read (English text only), so the opponent is not known to be a trainer or Roark."); } // Inside the Oreburgh Gym: straight up the middle, fighting the trainers @@ -106,21 +131,23 @@ GymMeeting meet_gym_opponent(VideoStream& stream, ProControllerContext& context, // false after a whiteout, which leaves us in the Pokemon Center. // Walking straight up meets both gym trainers (one at the entrance, one on // the second tier); their experience helps an under-levelled team, and -// after a whiteout they stay beaten (`trainers` keeps the count). Butters' +// after a whiteout they stay beaten. Butters' // route goes round them. -bool oreburgh_gym_battles(VideoStream& stream, ProControllerContext& context, GymTrainers& trainers){ +bool oreburgh_gym_battles(VideoStream& stream, ProControllerContext& context, GymColumn& column){ + column.restart(); // The entry camera pans for about 7.5 s (Butters' reference video). context.wait_for(8000ms); if (dialogue_opens(stream, context, 3000ms)){ - if (meet_gym_opponent(stream, context, trainers) == GymMeeting::LOST){ + if (meet_gym_opponent(stream, context, column) == GymMeeting::LOST){ return false; } } size_t stuck = 0; for (size_t leg = 0; leg < 10; leg++){ const WalkResult up = walk_until_no_wild(stream, context, {0, 1}, 8000ms, "north up the gym"); + column.walked_north(up.walked); if (up.stop == WalkStop::DIALOG){ - switch (meet_gym_opponent(stream, context, trainers)){ + switch (meet_gym_opponent(stream, context, column)){ case GymMeeting::LOST: return false; case GymMeeting::LEADER_WON: @@ -139,10 +166,7 @@ bool oreburgh_gym_battles(VideoStream& stream, ProControllerContext& context, Gy // north: a turn tap that way would be a step. pbf_press_button(context, BUTTON_A, 80ms, 200ms); if (dialogue_opens(stream, context, 3000ms)){ - if (!trainers.leader_next()){ - stream.log("[01.04] Blocked with a gym trainer still to beat: the talk is not Roark's."); - } - switch (meet_gym_opponent(stream, context, trainers)){ + switch (meet_gym_opponent(stream, context, column)){ case GymMeeting::LOST: return false; case GymMeeting::LEADER_WON: @@ -164,11 +188,11 @@ void gym_until_roark_beaten(VideoStream& stream, ProControllerContext& context, // Losing costs some money and keeps the experience, so each try comes // back stronger: Roark won twice by hand against Piplup Lv 10 and 14 and // lost the third time. - GymTrainers trainers(oreburgh_gym_trainer_sights().size()); + GymColumn column = oreburgh_gym_column(); bool won = false; for (size_t attempt = 0; attempt < 5 && !won; attempt++){ oreburgh_street_to_gym(stream, context); - won = oreburgh_gym_battles(stream, context, trainers); + won = oreburgh_gym_battles(stream, context, column); if (!won){ stream.log("[01.04] Lost in the gym (try " + std::to_string(attempt + 1) + "); back from the Center.", COLOR_ORANGE); walk_out_of_center(stream, context, "Oreburgh"); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h index 16348c5e43..d015392c96 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h @@ -3,55 +3,68 @@ * From: https://github.com/PokemonAutomation/ * * Who the person in front of us is on the way up the Oreburgh Gym. 01.04 - * walks straight up and is stopped by the gym trainers; when the walk was - * blocked and an A press opened a talk, the talk was taken for Roark's, and - * the second trainer's fight ended the step with no badge (lab-B, - * 2026-11-12 22:50). Roark is next only once every trainer in the gym is - * beaten; a talk before that is a trainer's. + * walks straight up the middle column and is stopped by the gym trainers; + * when a blocked walk and an A press opened a talk, the talk was taken for + * Roark's, and the second trainer's fight ended the step with no badge + * (lab-B, 2026-11-12 22:50). A count of trainers beaten fixed that, but the + * count is run state: a reload, or a save made mid-gym, restarts it at zero + * and Roark was called "Gym trainer 1 of 2" (lab-B, 23:49). + * + * Position is not identity either: it is dead reckoning from the time held, + * so it is only a hint (a log line, and whether to expect Roark's long + * pause). Who the opponent is, is read from the battle's intro text + * (BattleIntro.h). * */ #ifndef PokemonAutomation_PokemonBDSP_AutoStory_GymOpponent_H #define PokemonAutomation_PokemonBDSP_AutoStory_GymOpponent_H -#include -#include +#include namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonBDSP{ -// The gym trainers beaten so far, kept across the tries of a step: a trainer -// beaten stays beaten after a whiteout. -class GymTrainers{ +// Where we stand on the way up the gym's column, in tiles north of the +// entrance, estimated from the time walked north: a hint, not who is there. +class GymColumn{ public: - // `total`: the trainers between the door and the leader. - explicit GymTrainers(size_t total) - : m_total(total) + // `last_trainer_tiles`: how far north of the entrance the farthest gym + // trainer stops us. `leader_tiles`: the same for the tile in front of + // the leader. `ms_per_tile`: the pace walking north. + GymColumn(double last_trainer_tiles, double leader_tiles, std::chrono::milliseconds ms_per_tile) + : m_last_trainer(last_trainer_tiles) + , m_leader(leader_tiles) + , m_ms_per_tile(ms_per_tile.count() > 0 ? ms_per_tile.count() : 1) {} - // A trainer's battle was won. - void record_win(){ - if (m_beaten < m_total){ - m_beaten++; + // A try starts again at the entrance (the first, or after a whiteout). + void restart(){ + m_walked_ms = 0; + } + // A walk north held for `walked`. + void walked_north(std::chrono::milliseconds walked){ + if (walked.count() > 0){ + m_walked_ms += walked.count(); } } - // Whether the next opponent is the leader. - bool leader_next() const{ - return m_beaten >= m_total; + // Tiles north of the entrance, estimated. + double tiles() const{ + return (double)m_walked_ms / (double)m_ms_per_tile; } - // The next opponent's name, for the log and the battle. - std::string next_label() const{ - if (leader_next()){ - return "Roark"; - } - return "Gym trainer " + std::to_string(m_beaten + 1) + " of " + std::to_string(m_total); + // Whether the person in front of us is the leader: past the halfway + // point between the last trainer's stop and the leader's. + bool leader_here() const{ + return tiles() >= (m_last_trainer + m_leader) / 2; } private: - size_t m_total; - size_t m_beaten = 0; + double m_last_trainer; + double m_leader; + long long m_ms_per_tile; + long long m_walked_ms = 0; }; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkFight.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkFight.h new file mode 100644 index 0000000000..dd1b1082d3 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkFight.h @@ -0,0 +1,37 @@ +/* BDSP AutoStory - Talk Fight + * + * From: https://github.com/PokemonAutomation/ + * + * A talk that may end in a trainer's battle, with who the battle's intro + * text said the opponent was. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_TalkFight_H +#define PokemonAutomation_PokemonBDSP_AutoStory_TalkFight_H + +#include "PokemonBDSP_AutoStory_Talks.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// How a talk ended, and who the battle's intro text said it was against. +struct TalkFight{ + bool fought = false; + // UNREAD when there was no battle, or its intro was not read. + BattleOpponent opponent = BattleOpponent::UNREAD; +}; +// talk_or_fight() that also says who the opponent was. +TalkFight talk_or_fight_who( + VideoStream& stream, ProControllerContext& context, const std::string& label, + TalkExpect expect = TalkExpect::TALK_OR_BATTLE, BattleFormat format = BattleFormat::SINGLES +); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp index e8eb07f10d..101eedd09e 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp @@ -33,6 +33,7 @@ #include "PokemonBDSP_AutoStory_TalkOver.h" #include "PokemonBDSP_AutoStory_WildBattle.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h" +#include "PokemonBDSP_AutoStory_TalkFight.h" #include "PokemonBDSP_AutoStory_Talks.h" namespace PokemonAutomation{ @@ -56,22 +57,26 @@ bool dialogue_opens(VideoStream& stream, ProControllerContext& context, Millisec } bool talk_or_fight(VideoStream& stream, ProControllerContext& context, const std::string& label, TalkExpect expect, BattleFormat format){ + return talk_or_fight_who(stream, context, label, expect, format).fought; +} + +TalkFight talk_or_fight_who(VideoStream& stream, ProControllerContext& context, const std::string& label, TalkExpect expect, BattleFormat format){ const TalkOverEnd end = talk_over(stream, context, label, { .rule = {.end_on_quiet = expect != TalkExpect::BATTLE}, .watch = TalkOverWatch::TRAINER, }); if (end == TalkOverEnd::QUIET){ route_status(stream, label + " only talked."); - return false; + return {}; } route_status(stream, "Fighting " + label + "."); // The battle ends with its speeches over (confirm_battle_overworld()): // Roark's badge lines keep the Potion's menu from opening until then. - fight_story_battle(stream, context, label, format); + const BattleOpponent opponent = fight_story_battle(stream, context, label, format); if (!in_pokemon_center(stream)){ use_potion_first_pokemon(stream, context, 0.8); } - return true; + return {.fought = true, .opponent = opponent}; } // A rough check that we stand in a Pokemon Center: its floor fills the lower diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp new file mode 100644 index 0000000000..a642c48fd6 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp @@ -0,0 +1,158 @@ +/* BDSP AutoStory - Battle Intro + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlay.h" +#include "CommonTools/OCR/OCR_Routines.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "PokemonBDSP_AutoStory_BattleIntro.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +namespace{ + +// Lower case letters and single spaces only: OCR adds punctuation and +// stray marks, and the match is on words. +std::string words_of(std::string_view text){ + std::string out; + for (char ch : text){ + const unsigned char c = (unsigned char)ch; + if (std::isalpha(c)){ + out += (char)std::tolower(c); + }else if (!out.empty() && out.back() != ' '){ + out += ' '; + } + } + while (!out.empty() && out.back() == ' '){ + out.pop_back(); + } + return out; +} + +// After "challenged by", at least this many letters of the class and name +// must be there for the line to be a trainer's, not the start of a typed one. +constexpr size_t TRAINER_TEXT_MIN = 5; + +// The battle dialog's text, inside its margins (its frame: BattleDialogDetector). +const ImageFloatBox INTRO_TEXT(0.10, 0.86, 0.80, 0.08); +// Reads of the text box are this far apart: OCR is slow and the line types out over seconds. +constexpr std::chrono::milliseconds INTRO_READ_PERIOD = 300ms; + +} // namespace + + +std::string_view battle_opponent_name(BattleOpponent opponent){ + switch (opponent){ + case BattleOpponent::LEADER: + return "gym leader"; + case BattleOpponent::TRAINER: + return "trainer"; + case BattleOpponent::UNREAD: + break; + } + return "not read"; +} + +BattleOpponent classify_battle_intro(std::string_view text){ + const std::string words = words_of(text); + if (words.empty()){ + return BattleOpponent::UNREAD; + } + // No trainer's class or name is "leader", and Roark's name is on his line. + if (words.find("leader") != std::string::npos || words.find("roark") != std::string::npos){ + return BattleOpponent::LEADER; + } + const size_t by = words.find("challenged by "); + if (by == std::string::npos){ + return BattleOpponent::UNREAD; + } + const std::string rest = words.substr(by + std::string_view("challenged by ").size()); + size_t letters = 0; + for (char ch : rest){ + letters += ch != ' '; + } + return letters >= TRAINER_TEXT_MIN ? BattleOpponent::TRAINER : BattleOpponent::UNREAD; +} + +BattleOpponent BattleIntroReading::add(std::string_view text){ + if (m_verdict == BattleOpponent::LEADER){ + return m_verdict; + } + const BattleOpponent read = classify_battle_intro(text); + if (read == BattleOpponent::LEADER){ + m_verdict = BattleOpponent::LEADER; + }else if (read == BattleOpponent::TRAINER){ + const std::string words = words_of(text); + if (words == m_last_trainer_text){ + m_verdict = BattleOpponent::TRAINER; + } + m_last_trainer_text = words; + }else{ + m_last_trainer_text.clear(); + } + return m_verdict; +} + + +BattleIntroWatcher::BattleIntroWatcher(Color color) + : VisualInferenceCallback("BattleIntroWatcher") + , m_color(color) + , m_dialog(color) + , m_text(INTRO_TEXT) +{} + +void BattleIntroWatcher::make_overlays(VideoOverlaySet& items) const{ + m_dialog.make_overlays(items); + items.add(m_color, m_text); +} + +bool BattleIntroWatcher::process_frame(const ImageViewRGB32& frame, WallClock timestamp){ + if (frame.width() == 0 || frame.height() == 0){ + return false; + } + { + LockGuard lg(m_lock); + if (m_reading.verdict() != BattleOpponent::UNREAD){ + return true; + } + if (m_last_read != WallClock::min() && timestamp - m_last_read < INTRO_READ_PERIOD){ + return false; + } + m_last_read = timestamp; + } + if (!m_dialog.detect(frame) || !story_reads_text()){ + return false; + } + const std::string text = OCR::ocr_read(story_language(), extract_box_reference(frame, m_text), OCR::PageSegMode::SINGLE_BLOCK); + LockGuard lg(m_lock); + m_last_text = text; + return m_reading.add(text) != BattleOpponent::UNREAD; +} + +BattleOpponent BattleIntroWatcher::verdict() const{ + LockGuard lg(m_lock); + return m_reading.verdict(); +} + +std::string BattleIntroWatcher::last_text() const{ + LockGuard lg(m_lock); + return m_last_text; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h new file mode 100644 index 0000000000..a5e62a3a19 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h @@ -0,0 +1,92 @@ +/* BDSP AutoStory - Battle Intro + * + * From: https://github.com/PokemonAutomation/ + * + * Who a trainer battle is against, read from the text it opens with: "You + * are challenged by Gym Leader Roark!" against "... by Youngster !" + * for a gym trainer. Position and time walked only guess who stopped us + * (01.04 took Roark for a trainer after a reload, lab-B 2026-11-12 23:49); + * the text on the screen says it. + * + * English only: other languages have no text reader (story_reads_text()), + * so the opponent stays UNREAD there. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_BattleIntro_H +#define PokemonAutomation_PokemonBDSP_AutoStory_BattleIntro_H + +#include +#include +#include "Common/Cpp/Concurrency/Mutex.h" +#include "Common/Cpp/Time.h" +#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// What the intro text said of the opponent. +enum class BattleOpponent{ + UNREAD, // No intro text was read (nothing, a garble, or another language). + LEADER, // "Gym Leader ". + TRAINER, // "challenged by ", with no Leader in it. +}; + +// For the log: "gym leader", "trainer" or "not read". +std::string_view battle_opponent_name(BattleOpponent opponent); + +// One OCR read of the intro's text box. Empty or unrelated text is UNREAD. +// A read may be a line still being typed out, so TRAINER here is not final: +// BattleIntroReading decides that. +BattleOpponent classify_battle_intro(std::string_view text); + +// The reads of one battle's intro, to a verdict. A leader's name settles it +// at once (the line's words "Gym Leader" come before the name, and no +// trainer's read has them); a trainer is settled by the same text twice in a +// row, which says the line is fully typed. +class BattleIntroReading{ +public: + // Takes one OCR read; returns the verdict so far. + BattleOpponent add(std::string_view text); + BattleOpponent verdict() const{ + return m_verdict; + } + +private: + BattleOpponent m_verdict = BattleOpponent::UNREAD; + std::string m_last_trainer_text; +}; + + +// Reads the intro's text box while the battle's dialog shows, a few times a +// second, until a verdict. `process_frame()` is true once there is one. +class BattleIntroWatcher : public VisualInferenceCallback{ +public: + BattleIntroWatcher(Color color = COLOR_YELLOW); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override; + + BattleOpponent verdict() const; + // The last text read, for the log. + std::string last_text() const; + +private: + Color m_color; + BattleDialogDetector m_dialog; + ImageFloatBox m_text; + mutable Mutex m_lock; + BattleIntroReading m_reading; + std::string m_last_text; + WallClock m_last_read = WallClock::min(); +}; + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.h index a226c769e9..356581510b 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.h @@ -23,7 +23,8 @@ namespace PokemonBDSP{ // // TODO: no Trainer Card screenshot exists yet (CommandLineTests has none and // the lab has only save-prompt shots), so the badge boxes are not known and -// this reads UNREADABLE. verify_badge() saves the card it opened to +// this reads UNREADABLE, which fails the thorough check (nothing is passed +// unseen): until it is built 01.04 cannot pass at those levels. verify_badge() saves the card it opened to // Screenshots/AutoStory/cards/; a card with 0 badges and one with 1 badge // from console B are the fixtures to draw the boxes and thresholds from. Pokemon::BadgeReading read_trainer_card_badge(const ImageViewRGB32& screen, size_t index); diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 22b754194d..6686c40f91 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1755,6 +1755,7 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.h Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkFight.h Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.h Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.cpp @@ -1785,6 +1786,8 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WalkRules.h Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.cpp Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.h + Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp + Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.cpp Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.h Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_MenuCursorDetector.cpp From ce4520bcd60813c5866b4f9acec157a9121ed9b5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 01:23:31 -0800 Subject: [PATCH 3/5] BDSP AutoStory 01.04: read the battle intro in French; refuse other languages at the start The French run (lab-B, 2026-11-13 01:17) never read the intro: the reader was English-only, so two gym battles were fought blind and the Roark check failed after them. The intro classifier now takes per-language word tables (English, French; accents folded), 01.04 refuses at its start in a language with no table, an unread intro dumps its frames to DebugDumps, and the log line reads "opponent: not read". Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonBDSP_AutoStory_Actions_Tests.cpp | 57 +++++++ .../Actions/PokemonBDSP_AutoStory_Battle.cpp | 25 ++- .../Actions/PokemonBDSP_AutoStory_Battle.h | 3 +- .../PokemonBDSP_AutoStory_Events_Oreburgh.cpp | 11 +- .../PokemonBDSP_AutoStory_GymOpponent.h | 20 +++ .../PokemonBDSP_AutoStory_BattleIntro.cpp | 153 ++++++++++++++---- .../PokemonBDSP_AutoStory_BattleIntro.h | 36 ++++- 7 files changed, 263 insertions(+), 42 deletions(-) diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp index bc97d706cd..3085da3cba 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp @@ -527,6 +527,55 @@ UnitTestResult test_battle_intro_tells_the_opponent(Logger&){ return true; } +// 01.04 in French (lab-B, 2026-11-13 01:17): the intro was never read, two +// gym battles were fought blind and the step failed after them. Roark is +// "Pierrick", his title "Champion d'Arene" (pokepedia.fr); OCR may drop the +// accents, and an English table must not read a French line. +UnitTestResult test_battle_intro_french(Logger&){ + struct Case{ std::string_view text; Language language; BattleOpponent opponent; }; + const Case cases[] = { + {"Vous êtes défié par Champion d'Arène Pierrick !", Language::French, BattleOpponent::LEADER}, + {"Champion d'Arene Pierrick", Language::French, BattleOpponent::LEADER}, + {"CHAMPION D'ARÈNE", Language::French, BattleOpponent::LEADER}, + {"Pierrick", Language::French, BattleOpponent::LEADER}, + // Not the leader's: a trainer's line, once it is long enough. + {"Vous êtes défié par Montagnard Marc !", Language::French, BattleOpponent::TRAINER}, + {"Vous êtes", Language::French, BattleOpponent::UNREAD}, + {"", Language::French, BattleOpponent::UNREAD}, + // Each table reads its own language only. + {"Vous êtes défié par Champion d'Arène Pierrick !", Language::English, BattleOpponent::UNREAD}, + {"You are challenged by Gym Leader Roark!", Language::English, BattleOpponent::LEADER}, + {"You are challenged by Gym Leader Roark!", Language::German, BattleOpponent::UNREAD}, + }; + for (const Case& c : cases){ + const BattleOpponent read = classify_battle_intro(c.text, c.language); + if (read != c.opponent){ + return std::format("\"{}\" ({}) read as {}, not {}.", c.text, + language_data(c.language).name, battle_opponent_name(read), battle_opponent_name(c.opponent)); + } + } + BattleIntroReading trainer(Language::French); + trainer.add("Vous êtes défié par Montagnard Marc !"); + if (trainer.add("Vous etes defie par Montagnard Marc !") != BattleOpponent::TRAINER){ + return "The same French line twice, accents or not, is a trainer's."; + } + return true; +} + +// 01.04 refuses at its start in a language the intro is not read in, +// before any gym battle; English and French run. +UnitTestResult test_gym_refuses_unread_language(Logger&){ + if (gym_language_refusal(Language::English) || gym_language_refusal(Language::French)){ + return "English and French have intro word tables: 01.04 runs."; + } + for (Language language : {Language::German, Language::Japanese, Language::Spanish}){ + if (!gym_language_refusal(language)){ + return std::format("01.04 in {} must refuse before the gym.", language_data(language).name); + } + } + return true; +} + // A trainer's line is final when read the same twice; a half-typed one is // not, and nothing read stays unread. The leader's settles at once. UnitTestResult test_battle_intro_reading_settles(Logger&){ @@ -839,6 +888,14 @@ const AutoStoryActionsCase AUTO_STORY_ACTIONS_CASES[] = { .input = {"01.04 gym: the intro reading settles on a repeated trainer line or a leader's line", test_battle_intro_reading_settles}, .expectation = {}, }, + { + .input = {"01.04 gym: the intro is read in French, accents or not, and each table reads its own language (lab-B 2026-11-13)", test_battle_intro_french}, + .expectation = {}, + }, + { + .input = {"01.04 gym: refuses at its start in a language the intro is not read in", test_gym_refuses_unread_language}, + .expectation = {}, + }, { .input = {"01.04 gym: the column's tile estimate is a hint only", test_gym_column_is_a_hint}, .expectation = {}, diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp index d68e4a87c6..36e8a0bdd5 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp @@ -17,6 +17,7 @@ #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/GlobalAutoPaths.h" #include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/Tools/DebugDumper.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonTools/Async/InferenceRoutines.h" @@ -356,17 +357,32 @@ int wait_for_battle_screen( ); } +// An intro that told nobody: the frames it read (none when the battle's +// dialog never showed) and the screen now, for the next fix. +void dump_unread_intro(VideoStream& stream, const BattleIntroWatcher& intro, const std::string& label){ + const std::vector frames = intro.frames_read(); + for (size_t i = 0; i < frames.size(); i++){ + dump_debug_image(stream.logger(), "PokemonBDSP/AutoStory/BattleIntro", std::format("unread-read{}", i), frames[i]); + } + const VideoSnapshot now = stream.video().snapshot(); + if (now){ + dump_debug_image(stream.logger(), "PokemonBDSP/AutoStory/BattleIntro", "unread-now", *now.frame); + } + stream.log(std::format("[{}] The intro was not read: {} read frame(s) and the screen now are in DebugDumps.", + label, frames.size()), COLOR_ORANGE); +} + // The battle's opening transition, 5 s as it always was. A trainer's battle // starts with "You are challenged by ...": it is read while it shows, before // anything presses through it. BattleOpponent wait_out_battle_start(VideoStream& stream, ProControllerContext& context, const BestMoveBattle& battle, const std::string& label){ constexpr Milliseconds TRANSITION = 5000ms; - if (battle.battle_type != BattleType::TRAINER || !story_reads_text()){ + if (battle.battle_type != BattleType::TRAINER || !battle_intro_reads(story_language())){ pbf_wait(context, TRANSITION); return BattleOpponent::UNREAD; } context.wait_for_all_requests(); - BattleIntroWatcher intro; + BattleIntroWatcher intro(story_language()); const WallClock start = current_time(); wait_until(stream, context, TRANSITION, {{intro}}); const Milliseconds elapsed = std::chrono::duration_cast(current_time() - start); @@ -374,8 +390,11 @@ BattleOpponent wait_out_battle_start(VideoStream& stream, ProControllerContext& pbf_wait(context, TRANSITION - elapsed); } const BattleOpponent opponent = intro.verdict(); - stream.log(std::format("[{}] The battle's intro text read \"{}\": the opponent is {}.", + stream.log(std::format("[{}] The battle's intro text read \"{}\"; opponent: {}.", label, intro.last_text(), battle_opponent_name(opponent))); + if (opponent == BattleOpponent::UNREAD){ + dump_unread_intro(stream, intro, label); + } return opponent; } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h index e4dbe9df04..9ec9bf76a4 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h @@ -102,7 +102,8 @@ void confirm_battle_overworld(VideoStream& stream, ProControllerContext& context // turn in DOUBLES. Returns once the battle has ended, // won or lost; after a loss we stand in the last Pokemon Center. Returns who // the battle's intro text ("You are challenged by ...") said the opponent -// was: UNREAD when it was not read (no English text reader, or the line was +// was: UNREAD when it was not read (no word table for the game's language, +// battle_intro_reads(), or the line was // gone before the first read), never a guess. BattleOpponent fight_story_battle(VideoStream& stream, ProControllerContext& context, const std::string& label, BattleFormat format); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp index c8b4714ee9..5b51d2ca89 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp @@ -11,6 +11,8 @@ #include #include +#include +#include #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" @@ -18,6 +20,7 @@ #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteTiming.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" #include "PokemonBDSP_AutoStory_BadgeCheck.h" @@ -114,7 +117,7 @@ GymMeeting meet_gym_opponent(VideoStream& stream, ProControllerContext& context, if (in_pokemon_center(stream)){ return GymMeeting::LOST; } - stream.log(std::format("[01.04] The intro text says the opponent was the {}.", battle_opponent_name(met.opponent))); + stream.log(std::format("[01.04] Opponent by the battle's intro text: {}.", battle_opponent_name(met.opponent))); switch (met.opponent){ case BattleOpponent::LEADER: return GymMeeting::LEADER_WON; @@ -123,7 +126,7 @@ GymMeeting meet_gym_opponent(VideoStream& stream, ProControllerContext& context, case BattleOpponent::UNREAD: break; } - fail_step(stream, "Oreburgh Gym: the battle's intro text was not read (English text only), so the opponent is not known to be a trainer or Roark."); + fail_step(stream, "Oreburgh Gym: the battle's intro text was not read, so the opponent is not known to be a trainer or Roark (its frames are in DebugDumps)."); } // Inside the Oreburgh Gym: straight up the middle, fighting the trainers @@ -188,6 +191,10 @@ void gym_until_roark_beaten(VideoStream& stream, ProControllerContext& context, // Losing costs some money and keeps the experience, so each try comes // back stronger: Roark won twice by hand against Piplup Lv 10 and 14 and // lost the third time. + const std::optional refusal = gym_language_refusal(story_language()); + if (refusal){ + fail_step(stream, *refusal); + } GymColumn column = oreburgh_gym_column(); bool won = false; for (size_t attempt = 0; attempt < 5 && !won; attempt++){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h index d015392c96..e0af08049c 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h @@ -21,12 +21,32 @@ #define PokemonAutomation_PokemonBDSP_AutoStory_GymOpponent_H #include +#include +#include +#include +#include "CommonFramework/Language.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonBDSP{ +// Why 01.04 cannot run in `language`, or none when it can. Roark is told +// from a trainer only by the battle's intro text; with no word table for +// the language, every gym battle would be fought blind and the step would +// fail after them (lab-B, 2026-11-13 01:17, French), so it refuses first. +inline std::optional gym_language_refusal(Language language){ + if (battle_intro_reads(language)){ + return std::nullopt; + } + return std::format( + "Oreburgh Gym: the battle's intro text is not read in {}, so Roark could not be told from a trainer. " + "Refusing before the gym's battles.", + language_data(language).name + ); +} + // Where we stand on the way up the gym's column, in tiles north of the // entrance, estimated from the time walked north: a hint, not who is there. class GymColumn{ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp index a642c48fd6..5e88b5a059 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp @@ -6,12 +6,12 @@ #include #include +#include #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" #include "CommonFramework/VideoPipeline/VideoOverlay.h" #include "CommonTools/OCR/OCR_Routines.h" -#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" -#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "Common/Cpp/Strings/Unicode.h" #include "PokemonBDSP_AutoStory_BattleIntro.h" namespace PokemonAutomation{ @@ -23,17 +23,48 @@ using namespace std::chrono_literals; namespace{ -// Lower case letters and single spaces only: OCR adds punctuation and +// A letter with its accent dropped: OCR reads "Arene" as often as "Arène", +// so both sides of a match are folded. Latin-1 letters and the French +// ligature; anything else that is not ASCII is not a letter here. +std::string_view fold_letter(char32_t c){ + static constexpr std::string_view LATIN1 = + "aaaaaaaceeeeiiii" // U+00E0 - U+00EF + "dnooooo/ouuuuyty"; // U+00F0 - U+00FF + if (c < 0x80){ + return {}; + } + if (c >= 0xC0 && c <= 0xDE && c != 0xD7){ + c += 0x20; + } + if (c >= 0xE0 && c <= 0xFF && c != 0xF7){ + return LATIN1.substr(c - 0xE0, 1); + } + if (c == 0x152 || c == 0x153){ + return "oe"; + } + return {}; +} + +// Lower case ASCII letters and single spaces only: OCR adds punctuation and // stray marks, and the match is on words. std::string words_of(std::string_view text){ std::string out; - for (char ch : text){ - const unsigned char c = (unsigned char)ch; - if (std::isalpha(c)){ - out += (char)std::tolower(c); - }else if (!out.empty() && out.back() != ' '){ + const auto space = [&out](){ + if (!out.empty() && out.back() != ' '){ out += ' '; } + }; + for (char32_t c : utf8_to_utf32(std::string(text))){ + if (c < 0x80 && std::isalpha((int)c)){ + out += (char)std::tolower((int)c); + continue; + } + const std::string_view folded = fold_letter(c); + if (folded.empty()){ + space(); + }else{ + out += folded; + } } while (!out.empty() && out.back() == ' '){ out.pop_back(); @@ -41,9 +72,53 @@ std::string words_of(std::string_view text){ return out; } -// After "challenged by", at least this many letters of the class and name +// What the intro says in one language, in folded words (words_of()). +struct IntroWords{ + Language language; + // Any of these in the line makes it the leader's: his name or his title. + // No gym trainer's class or name has them. + std::vector leader; + // The words just before a trainer's class and name. Empty when the + // trainer's line is not known word for word: then any line that is not + // the leader's is a trainer's, once read the same twice (it is the + // battle's first text). + std::string_view trainer; +}; + +// English: the game's text ("You are challenged by Gym Leader Roark!", read +// on lab-B). French: Roark is "Pierrick", the title "Champion d'Arene" +// (pokepedia.fr, "Pierrick" and "Champion d'Arene"); the trainer's line is +// not verified word for word, so it has no words of its own. +const std::vector& intro_word_tables(){ + static const std::vector TABLES{ + {Language::English, {"leader", "roark"}, "challenged by"}, + {Language::French, {"pierrick", "champion", "arene"}, ""}, + }; + return TABLES; +} + +const IntroWords* intro_words(Language language){ + for (const IntroWords& table : intro_word_tables()){ + if (table.language == language){ + return &table; + } + } + return nullptr; +} + +size_t letters_in(std::string_view words){ + size_t letters = 0; + for (char ch : words){ + letters += ch != ' '; + } + return letters; +} + +// After the trainer's words, at least this many letters of the class and name // must be there for the line to be a trainer's, not the start of a typed one. constexpr size_t TRAINER_TEXT_MIN = 5; +// A line with no trainer's words to look for: at least this many letters. +constexpr size_t TRAINER_LINE_MIN = 12; // The battle dialog's text, inside its margins (its frame: BattleDialogDetector). const ImageFloatBox INTRO_TEXT(0.10, 0.86, 0.80, 0.08); @@ -56,41 +131,48 @@ constexpr std::chrono::milliseconds INTRO_READ_PERIOD = 300ms; std::string_view battle_opponent_name(BattleOpponent opponent){ switch (opponent){ case BattleOpponent::LEADER: - return "gym leader"; + return "the gym leader"; case BattleOpponent::TRAINER: - return "trainer"; + return "a trainer"; case BattleOpponent::UNREAD: break; } return "not read"; } -BattleOpponent classify_battle_intro(std::string_view text){ +bool battle_intro_reads(Language language){ + return intro_words(language) != nullptr; +} + +BattleOpponent classify_battle_intro(std::string_view text, Language language){ + const IntroWords* table = intro_words(language); const std::string words = words_of(text); - if (words.empty()){ + if (table == nullptr || words.empty()){ return BattleOpponent::UNREAD; } - // No trainer's class or name is "leader", and Roark's name is on his line. - if (words.find("leader") != std::string::npos || words.find("roark") != std::string::npos){ - return BattleOpponent::LEADER; + for (std::string_view word : table->leader){ + if (words.find(word) != std::string::npos){ + return BattleOpponent::LEADER; + } } - const size_t by = words.find("challenged by "); - if (by == std::string::npos){ - return BattleOpponent::UNREAD; + if (table->trainer.empty()){ + return letters_in(words) >= TRAINER_LINE_MIN ? BattleOpponent::TRAINER : BattleOpponent::UNREAD; } - const std::string rest = words.substr(by + std::string_view("challenged by ").size()); - size_t letters = 0; - for (char ch : rest){ - letters += ch != ' '; + const std::string marker = std::string(table->trainer) + " "; + const size_t at = words.find(marker); + if (at == std::string::npos){ + return BattleOpponent::UNREAD; } - return letters >= TRAINER_TEXT_MIN ? BattleOpponent::TRAINER : BattleOpponent::UNREAD; + return letters_in(std::string_view(words).substr(at + marker.size())) >= TRAINER_TEXT_MIN + ? BattleOpponent::TRAINER + : BattleOpponent::UNREAD; } BattleOpponent BattleIntroReading::add(std::string_view text){ if (m_verdict == BattleOpponent::LEADER){ return m_verdict; } - const BattleOpponent read = classify_battle_intro(text); + const BattleOpponent read = classify_battle_intro(text, m_language); if (read == BattleOpponent::LEADER){ m_verdict = BattleOpponent::LEADER; }else if (read == BattleOpponent::TRAINER){ @@ -106,11 +188,13 @@ BattleOpponent BattleIntroReading::add(std::string_view text){ } -BattleIntroWatcher::BattleIntroWatcher(Color color) +BattleIntroWatcher::BattleIntroWatcher(Language language, Color color) : VisualInferenceCallback("BattleIntroWatcher") + , m_language(language) , m_color(color) , m_dialog(color) , m_text(INTRO_TEXT) + , m_reading(language) {} void BattleIntroWatcher::make_overlays(VideoOverlaySet& items) const{ @@ -132,12 +216,16 @@ bool BattleIntroWatcher::process_frame(const ImageViewRGB32& frame, WallClock ti } m_last_read = timestamp; } - if (!m_dialog.detect(frame) || !story_reads_text()){ + if (!m_dialog.detect(frame) || !battle_intro_reads(m_language)){ return false; } - const std::string text = OCR::ocr_read(story_language(), extract_box_reference(frame, m_text), OCR::PageSegMode::SINGLE_BLOCK); + const std::string text = OCR::ocr_read(m_language, extract_box_reference(frame, m_text), OCR::PageSegMode::SINGLE_BLOCK); LockGuard lg(m_lock); m_last_text = text; + if (m_frames.size() >= INTRO_FRAMES_KEPT){ + m_frames.erase(m_frames.begin()); + } + m_frames.emplace_back(frame.copy()); return m_reading.add(text) != BattleOpponent::UNREAD; } @@ -151,6 +239,15 @@ std::string BattleIntroWatcher::last_text() const{ return m_last_text; } +std::vector BattleIntroWatcher::frames_read() const{ + LockGuard lg(m_lock); + std::vector out; + for (const ImageRGB32& frame : m_frames){ + out.emplace_back(frame.copy()); + } + return out; +} + } // namespace PokemonBDSP diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h index a5e62a3a19..e50d3488d6 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h @@ -8,8 +8,9 @@ * (01.04 took Roark for a trainer after a reload, lab-B 2026-11-12 23:49); * the text on the screen says it. * - * English only: other languages have no text reader (story_reads_text()), - * so the opponent stays UNREAD there. + * Each language the intro is read in has a word table (battle_intro_reads()): + * English and French so far. A run in another language must not reach a + * battle whose opponent matters (01.04 refuses at its start). * */ @@ -18,8 +19,11 @@ #include #include +#include #include "Common/Cpp/Concurrency/Mutex.h" #include "Common/Cpp/Time.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/Language.h" #include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" #include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" @@ -35,13 +39,17 @@ enum class BattleOpponent{ TRAINER, // "challenged by ", with no Leader in it. }; -// For the log: "gym leader", "trainer" or "not read". +// For the log, after "opponent: ": "the gym leader", "a trainer" or "not read". std::string_view battle_opponent_name(BattleOpponent opponent); -// One OCR read of the intro's text box. Empty or unrelated text is UNREAD. -// A read may be a line still being typed out, so TRAINER here is not final: -// BattleIntroReading decides that. -BattleOpponent classify_battle_intro(std::string_view text); +// Whether the intro text is read in `language`: it has a word table. +bool battle_intro_reads(Language language); + +// One OCR read of the intro's text box, in `language`. Empty or unrelated +// text is UNREAD, and so is every read in a language with no word table. +// Accents are ignored (OCR drops them). A read may be a line still being +// typed out, so TRAINER here is not final: BattleIntroReading decides that. +BattleOpponent classify_battle_intro(std::string_view text, Language language = Language::English); // The reads of one battle's intro, to a verdict. A leader's name settles it // at once (the line's words "Gym Leader" come before the name, and no @@ -49,6 +57,10 @@ BattleOpponent classify_battle_intro(std::string_view text); // row, which says the line is fully typed. class BattleIntroReading{ public: + explicit BattleIntroReading(Language language = Language::English) + : m_language(language) + {} + // Takes one OCR read; returns the verdict so far. BattleOpponent add(std::string_view text); BattleOpponent verdict() const{ @@ -56,6 +68,7 @@ class BattleIntroReading{ } private: + Language m_language; BattleOpponent m_verdict = BattleOpponent::UNREAD; std::string m_last_trainer_text; }; @@ -63,9 +76,10 @@ class BattleIntroReading{ // Reads the intro's text box while the battle's dialog shows, a few times a // second, until a verdict. `process_frame()` is true once there is one. +// It keeps the last few frames it read, for a dump when nothing was told. class BattleIntroWatcher : public VisualInferenceCallback{ public: - BattleIntroWatcher(Color color = COLOR_YELLOW); + explicit BattleIntroWatcher(Language language, Color color = COLOR_YELLOW); virtual void make_overlays(VideoOverlaySet& items) const override; virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override; @@ -73,14 +87,20 @@ class BattleIntroWatcher : public VisualInferenceCallback{ BattleOpponent verdict() const; // The last text read, for the log. std::string last_text() const; + // The last frames read (oldest first), at most INTRO_FRAMES_KEPT. + std::vector frames_read() const; + + static constexpr size_t INTRO_FRAMES_KEPT = 4; private: + Language m_language; Color m_color; BattleDialogDetector m_dialog; ImageFloatBox m_text; mutable Mutex m_lock; BattleIntroReading m_reading; std::string m_last_text; + std::vector m_frames; WallClock m_last_read = WallClock::min(); }; From 4c071e3c0a33225835a00ebdc4a3ec4eb814621a Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 01:23:59 -0800 Subject: [PATCH 4/5] BDSP AutoStory BattleIntro: build the trainer marker with std::format Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp index 5e88b5a059..07ea0ab001 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" @@ -158,7 +159,7 @@ BattleOpponent classify_battle_intro(std::string_view text, Language language){ if (table->trainer.empty()){ return letters_in(words) >= TRAINER_LINE_MIN ? BattleOpponent::TRAINER : BattleOpponent::UNREAD; } - const std::string marker = std::string(table->trainer) + " "; + const std::string marker = std::format("{} ", table->trainer); const size_t at = words.find(marker); if (at == std::string::npos){ return BattleOpponent::UNREAD; From 26242b63ca1d827a3cb2fd3af3040e86aabcd9f7 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 01:29:00 -0800 Subject: [PATCH 5/5] BDSP AutoStory 01.04: tell Roark by the talk after the battle, not its intro text The intro text was read by OCR in English and French only; Roark is the gym opponent who talks on the overworld after the battle (badge and TM speech), while a gym trainer's battle ends on a quiet overworld. talk_over now counts the looks with a box (TalkOverTally), the battle returns that count, and gym_opponent_after_battle() decides. BattleIntro is removed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonBDSP_AutoStory_Actions_Tests.cpp | 151 +++-------- .../Actions/PokemonBDSP_AutoStory_Battle.cpp | 64 +---- .../Actions/PokemonBDSP_AutoStory_Battle.h | 14 +- .../PokemonBDSP_AutoStory_Events_Oreburgh.cpp | 39 +-- .../PokemonBDSP_AutoStory_GymOpponent.h | 48 ++-- .../Actions/PokemonBDSP_AutoStory_TalkFight.h | 12 +- .../PokemonBDSP_AutoStory_TalkOver.cpp | 12 +- .../Actions/PokemonBDSP_AutoStory_TalkOver.h | 37 +++ .../Actions/PokemonBDSP_AutoStory_Talks.cpp | 4 +- .../PokemonBDSP_AutoStory_BattleIntro.cpp | 256 ------------------ .../PokemonBDSP_AutoStory_BattleIntro.h | 112 -------- SerialPrograms/cmake/SourceFiles.cmake | 2 - 12 files changed, 152 insertions(+), 599 deletions(-) delete mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp delete mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp index 3085da3cba..f919f1af5b 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp @@ -39,6 +39,7 @@ #include "PokemonBDSP_AutoStory_IntroSteps.h" #include "PokemonBDSP_AutoStory_LitDoorway.h" #include "PokemonBDSP_AutoStory_NewGame.h" +#include "PokemonBDSP_AutoStory_TalkOver.h" #include "PokemonBDSP_AutoStory_Actions_Tests.h" namespace PokemonAutomation{ @@ -498,114 +499,58 @@ UnitTestResult test_asks_someone_else(Logger&){ return true; } -// 01.04 (lab-B, 2026-11-12 22:50 and 23:49): the opponent is read from the -// battle's intro text, not told by a count that a reload loses (Roark was -// "Gym trainer 1 of 2" after a reload) or by the time walked. -UnitTestResult test_battle_intro_tells_the_opponent(Logger&){ - struct Case{ std::string_view text; BattleOpponent opponent; }; - const Case cases[] = { - {"You are challenged by Gym Leader Roark!", BattleOpponent::LEADER}, - {"You are challenged by Gym Leader", BattleOpponent::LEADER}, - {"You are challenged by Gym Leacler Roark!", BattleOpponent::LEADER}, - {"You are challenged by Youngster Tristan!", BattleOpponent::TRAINER}, - {"You are challenged by Hiker Mark!\n", BattleOpponent::TRAINER}, - // Still being typed out: no verdict from "by Y". - {"You are challenged by Y", BattleOpponent::UNREAD}, - {"You are challenged by", BattleOpponent::UNREAD}, - {"You are chal", BattleOpponent::UNREAD}, - {"Go! Turtwig!", BattleOpponent::UNREAD}, - {"", BattleOpponent::UNREAD}, - {" \n ", BattleOpponent::UNREAD}, - {"!!!", BattleOpponent::UNREAD}, - }; - for (const Case& c : cases){ - if (classify_battle_intro(c.text) != c.opponent){ - return std::format("\"{}\" read as {}, not {}.", c.text, - battle_opponent_name(classify_battle_intro(c.text)), battle_opponent_name(c.opponent)); +// The overworld after a won battle, replayed look by look through the +// talk-over's judge as the console runs it; returns the looks with a box. +size_t replay_after_battle(const std::vector& looks){ + using Pokemon::ScreenEvent; + TalkOverTally judge; + WallClock now = WallClock{} + std::chrono::hours(1); + for (const Pokemon::ScreenEvents& look : looks){ + if (judge.look(look, now) == SceneMove::DONE){ + return judge.box_looks(); } + now += TALK_OVER_HOLD + TALK_OVER_RELEASE; } - return true; + const Pokemon::ScreenEvents quiet{ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::SCREEN_STILL}; + for (size_t i = 0; i < 20; i++){ + if (judge.look(quiet, now) == SceneMove::DONE){ + return judge.box_looks(); + } + now += TALK_OVER_HOLD + TALK_OVER_RELEASE; + } + return judge.box_looks(); } -// 01.04 in French (lab-B, 2026-11-13 01:17): the intro was never read, two -// gym battles were fought blind and the step failed after them. Roark is -// "Pierrick", his title "Champion d'Arene" (pokepedia.fr); OCR may drop the -// accents, and an English table must not read a French line. -UnitTestResult test_battle_intro_french(Logger&){ - struct Case{ std::string_view text; Language language; BattleOpponent opponent; }; +// 01.04 (lab-B, 2026-11-13 01:17, French): Roark is told from a gym trainer +// by what happens after the battle, not by reading its intro text. A +// trainer's battle ends straight on a quiet overworld (22:49, the cleanup +// over in 7.8 s); Roark talks there for about 40 s (01:16:43 to 01:17:25), +// with a pause for the badge fanfare. +UnitTestResult test_gym_opponent_from_the_talk_after(Logger&){ + using Pokemon::ScreenEvent; + using Pokemon::ScreenEvents; + const ScreenEvents box{ScreenEvent::DIALOG_BOX}; + const ScreenEvents fanfare{ScreenEvent::PLAYER_CAN_MOVE}; + struct Case{ std::string_view name; std::vector looks; GymOpponent opponent; }; + std::vector roark(20, box); + roark.insert(roark.end(), 3, fanfare); + roark.insert(roark.end(), 12, box); const Case cases[] = { - {"Vous êtes défié par Champion d'Arène Pierrick !", Language::French, BattleOpponent::LEADER}, - {"Champion d'Arene Pierrick", Language::French, BattleOpponent::LEADER}, - {"CHAMPION D'ARÈNE", Language::French, BattleOpponent::LEADER}, - {"Pierrick", Language::French, BattleOpponent::LEADER}, - // Not the leader's: a trainer's line, once it is long enough. - {"Vous êtes défié par Montagnard Marc !", Language::French, BattleOpponent::TRAINER}, - {"Vous êtes", Language::French, BattleOpponent::UNREAD}, - {"", Language::French, BattleOpponent::UNREAD}, - // Each table reads its own language only. - {"Vous êtes défié par Champion d'Arène Pierrick !", Language::English, BattleOpponent::UNREAD}, - {"You are challenged by Gym Leader Roark!", Language::English, BattleOpponent::LEADER}, - {"You are challenged by Gym Leader Roark!", Language::German, BattleOpponent::UNREAD}, + {"a trainer: the overworld comes back quiet", {}, GymOpponent::TRAINER}, + {"a trainer: one stray look with a box", {box}, GymOpponent::TRAINER}, + {"Roark: the badge and TM speech, a fanfare pause between", roark, GymOpponent::LEADER}, }; for (const Case& c : cases){ - const BattleOpponent read = classify_battle_intro(c.text, c.language); - if (read != c.opponent){ - return std::format("\"{}\" ({}) read as {}, not {}.", c.text, - language_data(c.language).name, battle_opponent_name(read), battle_opponent_name(c.opponent)); - } - } - BattleIntroReading trainer(Language::French); - trainer.add("Vous êtes défié par Montagnard Marc !"); - if (trainer.add("Vous etes defie par Montagnard Marc !") != BattleOpponent::TRAINER){ - return "The same French line twice, accents or not, is a trainer's."; - } - return true; -} - -// 01.04 refuses at its start in a language the intro is not read in, -// before any gym battle; English and French run. -UnitTestResult test_gym_refuses_unread_language(Logger&){ - if (gym_language_refusal(Language::English) || gym_language_refusal(Language::French)){ - return "English and French have intro word tables: 01.04 runs."; - } - for (Language language : {Language::German, Language::Japanese, Language::Spanish}){ - if (!gym_language_refusal(language)){ - return std::format("01.04 in {} must refuse before the gym.", language_data(language).name); + const size_t looks = replay_after_battle(c.looks); + const GymOpponent opponent = gym_opponent_after_battle(looks); + if (opponent != c.opponent){ + return std::format("{}: {} look(s) with a box read as {}, not {}.", c.name, looks, + gym_opponent_name(opponent), gym_opponent_name(c.opponent)); } } return true; } -// A trainer's line is final when read the same twice; a half-typed one is -// not, and nothing read stays unread. The leader's settles at once. -UnitTestResult test_battle_intro_reading_settles(Logger&){ - BattleIntroReading trainer; - if (trainer.add("You are challenged by Youngster Tr") != BattleOpponent::UNREAD){ - return "A line read once is not final."; - } - if (trainer.add("You are challenged by Youngster Tristan!") != BattleOpponent::UNREAD){ - return "A line that changed since the last read is still being typed."; - } - if (trainer.add("") != BattleOpponent::UNREAD || trainer.add("You are challenged by Youngster Tristan!") != BattleOpponent::UNREAD){ - return "An unread frame between two reads breaks the pair."; - } - if (trainer.add("You are challenged by Youngster Tristan!") != BattleOpponent::TRAINER){ - return "The same line twice in a row is a trainer's."; - } - // The leader's line settles at once, and a later read does not undo it. - BattleIntroReading leader; - if (leader.add("You are challenged by Gym Leader Roark!") != BattleOpponent::LEADER - || leader.add("You are challenged by Youngster Tristan!") != BattleOpponent::LEADER - ){ - return "Gym Leader in the intro is Roark's, at once and for good."; - } - BattleIntroReading none; - if (none.add("") != BattleOpponent::UNREAD || none.verdict() != BattleOpponent::UNREAD){ - return "Nothing read must stay unread."; - } - return true; -} - // The column's time estimate is a hint only now: it must still count and // restart, and a negative walk adds nothing. UnitTestResult test_gym_column_is_a_hint(Logger&){ @@ -881,19 +826,7 @@ const AutoStoryActionsCase AUTO_STORY_ACTIONS_CASES[] = { .expectation = {}, }, { - .input = {"01.04 gym: the opponent is read from the battle's intro text, not a count or the tiles walked (lab-B 2026-11-12)", test_battle_intro_tells_the_opponent}, - .expectation = {}, - }, - { - .input = {"01.04 gym: the intro reading settles on a repeated trainer line or a leader's line", test_battle_intro_reading_settles}, - .expectation = {}, - }, - { - .input = {"01.04 gym: the intro is read in French, accents or not, and each table reads its own language (lab-B 2026-11-13)", test_battle_intro_french}, - .expectation = {}, - }, - { - .input = {"01.04 gym: refuses at its start in a language the intro is not read in", test_gym_refuses_unread_language}, + .input = {"01.04 gym: Roark is told by the talk on the overworld after the battle, in any language (lab-B 2026-11-13)", test_gym_opponent_from_the_talk_after}, .expectation = {}, }, { diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp index 36e8a0bdd5..88de25eb91 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp @@ -10,14 +10,11 @@ #include #include #include -#include #include "Common/Cpp/Filesystem/Filesystem.h" #include "Common/Cpp/PrettyPrint.h" -#include "Common/Cpp/Time.h" #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/GlobalAutoPaths.h" #include "CommonFramework/ImageTools/ImageBoxes.h" -#include "CommonFramework/Tools/DebugDumper.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonTools/Async/InferenceRoutines.h" @@ -27,11 +24,9 @@ #include "PokemonBDSP/Inference/Battles/PokemonBDSP_EndBattleDetector.h" #include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" #include "PokemonBDSP/Inference/PokemonBDSP_SelectionArrow.h" -#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h" #include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.h" #include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" -#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" #include "PokemonBDSP_AutoStory_Bag.h" #include "PokemonBDSP_AutoStory_BattleMoves.h" #include "PokemonBDSP_AutoStory_TalkOver.h" @@ -313,7 +308,7 @@ void take_turn( // The battle has ended: its text to the overworld, which must stay // dialogue-free for four seconds. -void finish_battle(VideoStream& stream, ProControllerContext& context, const std::string& label){ +size_t finish_battle(VideoStream& stream, ProControllerContext& context, const std::string& label){ stream.log("[AutoStory] " + label + " battle complete.", COLOR_GREEN); OverworldWatcher overworld; const int cleanup_ret = run_until(stream, context, @@ -325,7 +320,7 @@ void finish_battle(VideoStream& stream, ProControllerContext& context, const std label + ": Post-battle cleanup timed out.", stream); } context.wait_for_all_requests(); - confirm_battle_overworld(stream, context, label + " cleanup"); + return confirm_battle_overworld(stream, context, label + " cleanup"); } // B through the battle's text until its menu, its end, or a move-learning @@ -357,51 +352,11 @@ int wait_for_battle_screen( ); } -// An intro that told nobody: the frames it read (none when the battle's -// dialog never showed) and the screen now, for the next fix. -void dump_unread_intro(VideoStream& stream, const BattleIntroWatcher& intro, const std::string& label){ - const std::vector frames = intro.frames_read(); - for (size_t i = 0; i < frames.size(); i++){ - dump_debug_image(stream.logger(), "PokemonBDSP/AutoStory/BattleIntro", std::format("unread-read{}", i), frames[i]); - } - const VideoSnapshot now = stream.video().snapshot(); - if (now){ - dump_debug_image(stream.logger(), "PokemonBDSP/AutoStory/BattleIntro", "unread-now", *now.frame); - } - stream.log(std::format("[{}] The intro was not read: {} read frame(s) and the screen now are in DebugDumps.", - label, frames.size()), COLOR_ORANGE); -} - -// The battle's opening transition, 5 s as it always was. A trainer's battle -// starts with "You are challenged by ...": it is read while it shows, before -// anything presses through it. -BattleOpponent wait_out_battle_start(VideoStream& stream, ProControllerContext& context, const BestMoveBattle& battle, const std::string& label){ - constexpr Milliseconds TRANSITION = 5000ms; - if (battle.battle_type != BattleType::TRAINER || !battle_intro_reads(story_language())){ - pbf_wait(context, TRANSITION); - return BattleOpponent::UNREAD; - } - context.wait_for_all_requests(); - BattleIntroWatcher intro(story_language()); - const WallClock start = current_time(); - wait_until(stream, context, TRANSITION, {{intro}}); - const Milliseconds elapsed = std::chrono::duration_cast(current_time() - start); - if (elapsed < TRANSITION){ - pbf_wait(context, TRANSITION - elapsed); - } - const BattleOpponent opponent = intro.verdict(); - stream.log(std::format("[{}] The battle's intro text read \"{}\"; opponent: {}.", - label, intro.last_text(), battle_opponent_name(opponent))); - if (opponent == BattleOpponent::UNREAD){ - dump_unread_intro(stream, intro, label); - } - return opponent; -} - // Fights the battle on screen to its end and the overworld after it. -BattleOpponent fight_with_best_move(VideoStream& stream, ProControllerContext& context, const BestMoveBattle& battle, const std::string& label){ +size_t fight_with_best_move(VideoStream& stream, ProControllerContext& context, const BestMoveBattle& battle, const std::string& label){ stream.log("[AutoStory] Starting " + label + " battle...", COLOR_BLUE); - const BattleOpponent opponent = wait_out_battle_start(stream, context, battle, label); + // Preserve the existing early-game transition delay. + pbf_wait(context, 5000ms); BattleProgress progress; EndBattleWatcher end_battle; while (true){ @@ -409,8 +364,7 @@ BattleOpponent fight_with_best_move(VideoStream& stream, ProControllerContext& c if (ret == 0){ take_turn(stream, context, battle, progress, label); }else if (ret == 1){ - finish_battle(stream, context, label); - return opponent; + return finish_battle(stream, context, label); }else if (ret == 2){ stream.log("[" + label + "] Declining move learning; preserving scripted move positions."); pbf_move_right_joystick(context, {0, -1}, 160ms, 840ms); @@ -433,12 +387,12 @@ BattleOpponent fight_with_best_move(VideoStream& stream, ProControllerContext& c } // namespace -void confirm_battle_overworld(VideoStream& stream, ProControllerContext& context, +size_t confirm_battle_overworld(VideoStream& stream, ProControllerContext& context, const std::string& label){ - talk_over(stream, context, label); + return talk_over_seen(stream, context, label).box_looks; } -BattleOpponent fight_story_battle(VideoStream& stream, ProControllerContext& context, const std::string& label, BattleFormat format){ +size_t fight_story_battle(VideoStream& stream, ProControllerContext& context, const std::string& label, BattleFormat format){ // The best move each turn; the next Pokemon after a faint; no new moves. return fight_with_best_move(stream, context, {.battle_type = BattleType::TRAINER, .format = format, .switch_on_faint = true}, label); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h index 9ec9bf76a4..5c39e2df31 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h @@ -17,7 +17,6 @@ #include "CommonTools/VisualDetector.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" -#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -94,18 +93,17 @@ class PotionLedger{ // lines until the overworld is quiet. Roark's badge lines pause for the // fanfare, and someone may walk off before the next line, so a moment // with no box is not the end. Fails with a screenshot after two minutes. -void confirm_battle_overworld(VideoStream& stream, ProControllerContext& context, const std::string& label); +// Returns how many looks showed a box: someone talked after the battle. +size_t confirm_battle_overworld(VideoStream& stream, ProControllerContext& context, const std::string& label); // A story battle that may go badly: the best move each turn, Potions // under 35 %, the next Pokemon when one faints, and "Keep old moves" // on level-up prompts. `doubles` confirms the default target each // turn in DOUBLES. Returns once the battle has ended, -// won or lost; after a loss we stand in the last Pokemon Center. Returns who -// the battle's intro text ("You are challenged by ...") said the opponent -// was: UNREAD when it was not read (no word table for the game's language, -// battle_intro_reads(), or the line was -// gone before the first read), never a guess. -BattleOpponent fight_story_battle(VideoStream& stream, ProControllerContext& context, const std::string& label, BattleFormat format); +// won or lost; after a loss we stand in the last Pokemon Center. Returns how +// many looks showed a box on the overworld after the battle +// (confirm_battle_overworld()): who talked after it, in any language. +size_t fight_story_battle(VideoStream& stream, ProControllerContext& context, const std::string& label, BattleFormat format); // Any trainer battle, whatever the starter: the best move on the menu each // turn until the battle ends, then the text after it. diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp index 5b51d2ca89..e2e1c4f8a9 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp @@ -11,7 +11,6 @@ #include #include -#include #include #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" @@ -20,7 +19,6 @@ #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteTiming.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" -#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" #include "PokemonBDSP_AutoStory_BadgeCheck.h" @@ -96,15 +94,16 @@ enum class GymMeeting{ LEADER_WON, }; -// A talk has opened. Who it is, is what the battle's intro text says ("You -// are challenged by Gym Leader Roark!"), read on the screen by -// fight_story_battle(): never the count of trainers beaten or the distance -// walked (lab-B, 2026-11-12 22:50: the second trainer, met by a blocked walk -// and an A press, was fought as Roark, and 01.05 saved with no badge; 23:49: -// after a reload Roark was "Gym trainer 1"). The column only hints, and says -// how to wait: Roark's lines pause for more than 4 s before the battle -// (r210d: "only talked"), so up at his tile a battle is expected. An intro -// that was not read fails the step: the next one would be a guess. +// A talk has opened. Who it was is told after a won battle by whether +// anyone talked on the overworld (gym_opponent_after_battle()): Roark's +// badge speech, in any language. Never the count of trainers beaten or the +// distance walked (lab-B, 2026-11-12 22:50: the second trainer, met by a +// blocked walk and an A press, was fought as Roark, and 01.05 saved with no +// badge; 23:49: after a reload Roark was "Gym trainer 1"), and not the +// intro text, which OCR read in English only (01:17, French). The column +// only hints, and says how to wait: Roark's lines pause for more than 4 s +// before the battle (r210d: "only talked"), so up at his tile a battle is +// expected. GymMeeting meet_gym_opponent(VideoStream& stream, ProControllerContext& context, const GymColumn& column){ const bool leader_hint = column.leader_here(); stream.log(std::format("[01.04] About {:.1f} tiles up the gym (a hint, not the opponent: {}).", @@ -117,16 +116,10 @@ GymMeeting meet_gym_opponent(VideoStream& stream, ProControllerContext& context, if (in_pokemon_center(stream)){ return GymMeeting::LOST; } - stream.log(std::format("[01.04] Opponent by the battle's intro text: {}.", battle_opponent_name(met.opponent))); - switch (met.opponent){ - case BattleOpponent::LEADER: - return GymMeeting::LEADER_WON; - case BattleOpponent::TRAINER: - return GymMeeting::TRAINER_WON; - case BattleOpponent::UNREAD: - break; - } - fail_step(stream, "Oreburgh Gym: the battle's intro text was not read, so the opponent is not known to be a trainer or Roark (its frames are in DebugDumps)."); + const GymOpponent opponent = gym_opponent_after_battle(met.box_looks_after); + stream.log(std::format("[01.04] Beat {}: a box on the overworld after the battle in {} look(s).", + gym_opponent_name(opponent), met.box_looks_after)); + return opponent == GymOpponent::LEADER ? GymMeeting::LEADER_WON : GymMeeting::TRAINER_WON; } // Inside the Oreburgh Gym: straight up the middle, fighting the trainers @@ -191,10 +184,6 @@ void gym_until_roark_beaten(VideoStream& stream, ProControllerContext& context, // Losing costs some money and keeps the experience, so each try comes // back stronger: Roark won twice by hand against Piplup Lv 10 and 14 and // lost the third time. - const std::optional refusal = gym_language_refusal(story_language()); - if (refusal){ - fail_step(stream, *refusal); - } GymColumn column = oreburgh_gym_column(); bool won = false; for (size_t attempt = 0; attempt < 5 && !won; attempt++){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h index e0af08049c..e7db3a5e99 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h @@ -12,8 +12,14 @@ * * Position is not identity either: it is dead reckoning from the time held, * so it is only a hint (a log line, and whether to expect Roark's long - * pause). Who the opponent is, is read from the battle's intro text - * (BattleIntro.h). + * pause). Nor is the intro text: read by OCR, it held only in the + * languages it had word tables for, and a French game fought both gym + * battles and Roark's blind, then failed (lab-B, 2026-11-13 01:17). Who the + * opponent was is told by what happens after the battle, in any language: + * Roark talks on the overworld (the Coal Badge, then the TM, about 40 s of + * lines at 01:16), while a gym trainer's lines are all in the battle and + * the overworld comes back quiet (lab-B 2026-11-12 22:49: the trainer's + * cleanup was over in 7.8 s, the fade and the 5 s quiet). * */ @@ -21,30 +27,32 @@ #define PokemonAutomation_PokemonBDSP_AutoStory_GymOpponent_H #include -#include -#include -#include -#include "CommonFramework/Language.h" -#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h" +#include +#include namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonBDSP{ -// Why 01.04 cannot run in `language`, or none when it can. Roark is told -// from a trainer only by the battle's intro text; with no word table for -// the language, every gym battle would be fought blind and the step would -// fail after them (lab-B, 2026-11-13 01:17, French), so it refuses first. -inline std::optional gym_language_refusal(Language language){ - if (battle_intro_reads(language)){ - return std::nullopt; - } - return std::format( - "Oreburgh Gym: the battle's intro text is not read in {}, so Roark could not be told from a trainer. " - "Refusing before the gym's battles.", - language_data(language).name - ); +// Who a won gym battle was against. +enum class GymOpponent{ + TRAINER, + LEADER, +}; +inline std::string_view gym_opponent_name(GymOpponent opponent){ + return opponent == GymOpponent::LEADER ? "Roark" : "a gym trainer"; +} + +// This many looks with a box on the overworld after a won battle are the +// leader's speech. Roark's lines fill dozens of looks; one look is allowed +// to be a stray read of the battle's last frame. +constexpr size_t GYM_LEADER_TALK_LOOKS = 2; + +// Who a won gym battle was against, from the looks with a box on the +// overworld after it (confirm_battle_overworld()). +inline GymOpponent gym_opponent_after_battle(size_t box_looks_after){ + return box_looks_after >= GYM_LEADER_TALK_LOOKS ? GymOpponent::LEADER : GymOpponent::TRAINER; } // Where we stand on the way up the gym's column, in tiles north of the diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkFight.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkFight.h index dd1b1082d3..436a8339f1 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkFight.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkFight.h @@ -2,8 +2,8 @@ * * From: https://github.com/PokemonAutomation/ * - * A talk that may end in a trainer's battle, with who the battle's intro - * text said the opponent was. + * A talk that may end in a trainer's battle, with whether anyone talked on + * the overworld after the battle. * */ @@ -17,13 +17,13 @@ namespace NintendoSwitch{ namespace PokemonBDSP{ -// How a talk ended, and who the battle's intro text said it was against. +// How a talk ended, and what was said after its battle. struct TalkFight{ bool fought = false; - // UNREAD when there was no battle, or its intro was not read. - BattleOpponent opponent = BattleOpponent::UNREAD; + // Looks with a box on the overworld after the battle (0 with none). + size_t box_looks_after = 0; }; -// talk_or_fight() that also says who the opponent was. +// talk_or_fight() that also says whether anyone talked after the battle. TalkFight talk_or_fight_who( VideoStream& stream, ProControllerContext& context, const std::string& label, TalkExpect expect = TalkExpect::TALK_OR_BATTLE, BattleFormat format = BattleFormat::SINGLES diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp index 9871501ec0..546b853a59 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp @@ -18,20 +18,24 @@ namespace PokemonBDSP{ TalkOverEnd talk_over(VideoStream& stream, ProControllerContext& context, const std::string& label, const TalkOverSpec& spec){ + return talk_over_seen(stream, context, label, spec).end; +} + +TalkOverSeen talk_over_seen(VideoStream& stream, ProControllerContext& context, const std::string& label, const TalkOverSpec& spec){ const WallClock deadline = current_time() + TALK_OVER_LIMIT; - TalkOver judge(spec.rule); + TalkOverTally judge(spec.rule); BdspScreenObserver eyes(spec.watch == TalkOverWatch::TRAINER ? BattleCue::TRAINER : BattleCue::WILD_MENU, spec.banner); while (current_time() < deadline){ context.wait_for_all_requests(); const WallClock now = current_time(); switch (judge.look(eyes.observe(stream.video().snapshot(), now), now)){ case SceneMove::DONE: - stream.log(std::format("[Talk-over] {}: over; {}.", label, judge.describe())); - return TalkOverEnd::QUIET; + stream.log(std::format("[Talk-over] {}: over; {}; a box in {} look(s).", label, judge.describe(), judge.box_looks())); + return {.end = TalkOverEnd::QUIET, .box_looks = judge.box_looks()}; case SceneMove::BATTLE: if (spec.watch == TalkOverWatch::TRAINER){ stream.log("[Talk-over] " + label + ": a battle started."); - return TalkOverEnd::BATTLE; + return {.end = TalkOverEnd::BATTLE, .box_looks = judge.box_looks()}; } stream.log("[Talk-over] " + label + ": in a wild battle; fleeing."); run_from_wild_battle(stream, context); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h index 9469c3954c..4f94cde719 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h @@ -77,8 +77,45 @@ enum class TalkOverEnd{ BATTLE, // Only with TalkOverWatch::TRAINER. }; +// TalkOver's judgement, with how many looks showed a box: what was said, +// in any language, without reading it. Pure: tests replay looks through it. +class TalkOverTally{ +public: + explicit TalkOverTally(TalkOverRule rule = {}) + : m_judge(rule) + {} + SceneMove look(const Pokemon::ScreenEvents& events, WallClock now){ + const SceneMove move = m_judge.look(events, now); + if (move == SceneMove::NEXT){ + m_box_looks++; + } + return move; + } + size_t box_looks() const{ + return m_box_looks; + } + bool warped() const{ + return m_judge.warped(); + } + std::string describe() const{ + return m_judge.describe(); + } + +private: + TalkOver m_judge; + size_t m_box_looks = 0; +}; + +// How talk_over_seen() ended, and how many of its looks showed a box. +struct TalkOverSeen{ + TalkOverEnd end = TalkOverEnd::QUIET; + size_t box_looks = 0; +}; + // Presses through any text and scene on the console until TalkOver says it // is over. Fails with a screenshot after TALK_OVER_LIMIT. +TalkOverSeen talk_over_seen(VideoStream& stream, ProControllerContext& context, const std::string& label, const TalkOverSpec& spec = {}); +// talk_over_seen() without the count. TalkOverEnd talk_over(VideoStream& stream, ProControllerContext& context, const std::string& label, const TalkOverSpec& spec = {}); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp index 101eedd09e..338745aa5f 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp @@ -72,11 +72,11 @@ TalkFight talk_or_fight_who(VideoStream& stream, ProControllerContext& context, route_status(stream, "Fighting " + label + "."); // The battle ends with its speeches over (confirm_battle_overworld()): // Roark's badge lines keep the Potion's menu from opening until then. - const BattleOpponent opponent = fight_story_battle(stream, context, label, format); + const size_t box_looks_after = fight_story_battle(stream, context, label, format); if (!in_pokemon_center(stream)){ use_potion_first_pokemon(stream, context, 0.8); } - return {.fought = true, .opponent = opponent}; + return {.fought = true, .box_looks_after = box_looks_after}; } // A rough check that we stand in a Pokemon Center: its floor fills the lower diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp deleted file mode 100644 index 07ea0ab001..0000000000 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp +++ /dev/null @@ -1,256 +0,0 @@ -/* BDSP AutoStory - Battle Intro - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include -#include -#include -#include -#include "CommonFramework/ImageTools/ImageBoxes.h" -#include "CommonFramework/ImageTypes/ImageViewRGB32.h" -#include "CommonFramework/VideoPipeline/VideoOverlay.h" -#include "CommonTools/OCR/OCR_Routines.h" -#include "Common/Cpp/Strings/Unicode.h" -#include "PokemonBDSP_AutoStory_BattleIntro.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonBDSP{ - -using namespace std::chrono_literals; - - -namespace{ - -// A letter with its accent dropped: OCR reads "Arene" as often as "Arène", -// so both sides of a match are folded. Latin-1 letters and the French -// ligature; anything else that is not ASCII is not a letter here. -std::string_view fold_letter(char32_t c){ - static constexpr std::string_view LATIN1 = - "aaaaaaaceeeeiiii" // U+00E0 - U+00EF - "dnooooo/ouuuuyty"; // U+00F0 - U+00FF - if (c < 0x80){ - return {}; - } - if (c >= 0xC0 && c <= 0xDE && c != 0xD7){ - c += 0x20; - } - if (c >= 0xE0 && c <= 0xFF && c != 0xF7){ - return LATIN1.substr(c - 0xE0, 1); - } - if (c == 0x152 || c == 0x153){ - return "oe"; - } - return {}; -} - -// Lower case ASCII letters and single spaces only: OCR adds punctuation and -// stray marks, and the match is on words. -std::string words_of(std::string_view text){ - std::string out; - const auto space = [&out](){ - if (!out.empty() && out.back() != ' '){ - out += ' '; - } - }; - for (char32_t c : utf8_to_utf32(std::string(text))){ - if (c < 0x80 && std::isalpha((int)c)){ - out += (char)std::tolower((int)c); - continue; - } - const std::string_view folded = fold_letter(c); - if (folded.empty()){ - space(); - }else{ - out += folded; - } - } - while (!out.empty() && out.back() == ' '){ - out.pop_back(); - } - return out; -} - -// What the intro says in one language, in folded words (words_of()). -struct IntroWords{ - Language language; - // Any of these in the line makes it the leader's: his name or his title. - // No gym trainer's class or name has them. - std::vector leader; - // The words just before a trainer's class and name. Empty when the - // trainer's line is not known word for word: then any line that is not - // the leader's is a trainer's, once read the same twice (it is the - // battle's first text). - std::string_view trainer; -}; - -// English: the game's text ("You are challenged by Gym Leader Roark!", read -// on lab-B). French: Roark is "Pierrick", the title "Champion d'Arene" -// (pokepedia.fr, "Pierrick" and "Champion d'Arene"); the trainer's line is -// not verified word for word, so it has no words of its own. -const std::vector& intro_word_tables(){ - static const std::vector TABLES{ - {Language::English, {"leader", "roark"}, "challenged by"}, - {Language::French, {"pierrick", "champion", "arene"}, ""}, - }; - return TABLES; -} - -const IntroWords* intro_words(Language language){ - for (const IntroWords& table : intro_word_tables()){ - if (table.language == language){ - return &table; - } - } - return nullptr; -} - -size_t letters_in(std::string_view words){ - size_t letters = 0; - for (char ch : words){ - letters += ch != ' '; - } - return letters; -} - -// After the trainer's words, at least this many letters of the class and name -// must be there for the line to be a trainer's, not the start of a typed one. -constexpr size_t TRAINER_TEXT_MIN = 5; -// A line with no trainer's words to look for: at least this many letters. -constexpr size_t TRAINER_LINE_MIN = 12; - -// The battle dialog's text, inside its margins (its frame: BattleDialogDetector). -const ImageFloatBox INTRO_TEXT(0.10, 0.86, 0.80, 0.08); -// Reads of the text box are this far apart: OCR is slow and the line types out over seconds. -constexpr std::chrono::milliseconds INTRO_READ_PERIOD = 300ms; - -} // namespace - - -std::string_view battle_opponent_name(BattleOpponent opponent){ - switch (opponent){ - case BattleOpponent::LEADER: - return "the gym leader"; - case BattleOpponent::TRAINER: - return "a trainer"; - case BattleOpponent::UNREAD: - break; - } - return "not read"; -} - -bool battle_intro_reads(Language language){ - return intro_words(language) != nullptr; -} - -BattleOpponent classify_battle_intro(std::string_view text, Language language){ - const IntroWords* table = intro_words(language); - const std::string words = words_of(text); - if (table == nullptr || words.empty()){ - return BattleOpponent::UNREAD; - } - for (std::string_view word : table->leader){ - if (words.find(word) != std::string::npos){ - return BattleOpponent::LEADER; - } - } - if (table->trainer.empty()){ - return letters_in(words) >= TRAINER_LINE_MIN ? BattleOpponent::TRAINER : BattleOpponent::UNREAD; - } - const std::string marker = std::format("{} ", table->trainer); - const size_t at = words.find(marker); - if (at == std::string::npos){ - return BattleOpponent::UNREAD; - } - return letters_in(std::string_view(words).substr(at + marker.size())) >= TRAINER_TEXT_MIN - ? BattleOpponent::TRAINER - : BattleOpponent::UNREAD; -} - -BattleOpponent BattleIntroReading::add(std::string_view text){ - if (m_verdict == BattleOpponent::LEADER){ - return m_verdict; - } - const BattleOpponent read = classify_battle_intro(text, m_language); - if (read == BattleOpponent::LEADER){ - m_verdict = BattleOpponent::LEADER; - }else if (read == BattleOpponent::TRAINER){ - const std::string words = words_of(text); - if (words == m_last_trainer_text){ - m_verdict = BattleOpponent::TRAINER; - } - m_last_trainer_text = words; - }else{ - m_last_trainer_text.clear(); - } - return m_verdict; -} - - -BattleIntroWatcher::BattleIntroWatcher(Language language, Color color) - : VisualInferenceCallback("BattleIntroWatcher") - , m_language(language) - , m_color(color) - , m_dialog(color) - , m_text(INTRO_TEXT) - , m_reading(language) -{} - -void BattleIntroWatcher::make_overlays(VideoOverlaySet& items) const{ - m_dialog.make_overlays(items); - items.add(m_color, m_text); -} - -bool BattleIntroWatcher::process_frame(const ImageViewRGB32& frame, WallClock timestamp){ - if (frame.width() == 0 || frame.height() == 0){ - return false; - } - { - LockGuard lg(m_lock); - if (m_reading.verdict() != BattleOpponent::UNREAD){ - return true; - } - if (m_last_read != WallClock::min() && timestamp - m_last_read < INTRO_READ_PERIOD){ - return false; - } - m_last_read = timestamp; - } - if (!m_dialog.detect(frame) || !battle_intro_reads(m_language)){ - return false; - } - const std::string text = OCR::ocr_read(m_language, extract_box_reference(frame, m_text), OCR::PageSegMode::SINGLE_BLOCK); - LockGuard lg(m_lock); - m_last_text = text; - if (m_frames.size() >= INTRO_FRAMES_KEPT){ - m_frames.erase(m_frames.begin()); - } - m_frames.emplace_back(frame.copy()); - return m_reading.add(text) != BattleOpponent::UNREAD; -} - -BattleOpponent BattleIntroWatcher::verdict() const{ - LockGuard lg(m_lock); - return m_reading.verdict(); -} - -std::string BattleIntroWatcher::last_text() const{ - LockGuard lg(m_lock); - return m_last_text; -} - -std::vector BattleIntroWatcher::frames_read() const{ - LockGuard lg(m_lock); - std::vector out; - for (const ImageRGB32& frame : m_frames){ - out.emplace_back(frame.copy()); - } - return out; -} - - - -} // namespace PokemonBDSP -} // namespace NintendoSwitch -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h deleted file mode 100644 index e50d3488d6..0000000000 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h +++ /dev/null @@ -1,112 +0,0 @@ -/* BDSP AutoStory - Battle Intro - * - * From: https://github.com/PokemonAutomation/ - * - * Who a trainer battle is against, read from the text it opens with: "You - * are challenged by Gym Leader Roark!" against "... by Youngster !" - * for a gym trainer. Position and time walked only guess who stopped us - * (01.04 took Roark for a trainer after a reload, lab-B 2026-11-12 23:49); - * the text on the screen says it. - * - * Each language the intro is read in has a word table (battle_intro_reads()): - * English and French so far. A run in another language must not reach a - * battle whose opponent matters (01.04 refuses at its start). - * - */ - -#ifndef PokemonAutomation_PokemonBDSP_AutoStory_BattleIntro_H -#define PokemonAutomation_PokemonBDSP_AutoStory_BattleIntro_H - -#include -#include -#include -#include "Common/Cpp/Concurrency/Mutex.h" -#include "Common/Cpp/Time.h" -#include "CommonFramework/ImageTypes/ImageRGB32.h" -#include "CommonFramework/Language.h" -#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" -#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonBDSP{ - - -// What the intro text said of the opponent. -enum class BattleOpponent{ - UNREAD, // No intro text was read (nothing, a garble, or another language). - LEADER, // "Gym Leader ". - TRAINER, // "challenged by ", with no Leader in it. -}; - -// For the log, after "opponent: ": "the gym leader", "a trainer" or "not read". -std::string_view battle_opponent_name(BattleOpponent opponent); - -// Whether the intro text is read in `language`: it has a word table. -bool battle_intro_reads(Language language); - -// One OCR read of the intro's text box, in `language`. Empty or unrelated -// text is UNREAD, and so is every read in a language with no word table. -// Accents are ignored (OCR drops them). A read may be a line still being -// typed out, so TRAINER here is not final: BattleIntroReading decides that. -BattleOpponent classify_battle_intro(std::string_view text, Language language = Language::English); - -// The reads of one battle's intro, to a verdict. A leader's name settles it -// at once (the line's words "Gym Leader" come before the name, and no -// trainer's read has them); a trainer is settled by the same text twice in a -// row, which says the line is fully typed. -class BattleIntroReading{ -public: - explicit BattleIntroReading(Language language = Language::English) - : m_language(language) - {} - - // Takes one OCR read; returns the verdict so far. - BattleOpponent add(std::string_view text); - BattleOpponent verdict() const{ - return m_verdict; - } - -private: - Language m_language; - BattleOpponent m_verdict = BattleOpponent::UNREAD; - std::string m_last_trainer_text; -}; - - -// Reads the intro's text box while the battle's dialog shows, a few times a -// second, until a verdict. `process_frame()` is true once there is one. -// It keeps the last few frames it read, for a dump when nothing was told. -class BattleIntroWatcher : public VisualInferenceCallback{ -public: - explicit BattleIntroWatcher(Language language, Color color = COLOR_YELLOW); - - virtual void make_overlays(VideoOverlaySet& items) const override; - virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override; - - BattleOpponent verdict() const; - // The last text read, for the log. - std::string last_text() const; - // The last frames read (oldest first), at most INTRO_FRAMES_KEPT. - std::vector frames_read() const; - - static constexpr size_t INTRO_FRAMES_KEPT = 4; - -private: - Language m_language; - Color m_color; - BattleDialogDetector m_dialog; - ImageFloatBox m_text; - mutable Mutex m_lock; - BattleIntroReading m_reading; - std::string m_last_text; - std::vector m_frames; - WallClock m_last_read = WallClock::min(); -}; - - - -} // namespace PokemonBDSP -} // namespace NintendoSwitch -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 6686c40f91..47fac311f7 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1786,8 +1786,6 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WalkRules.h Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.cpp Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.h - Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.cpp - Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleIntro.h Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.cpp Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.h Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_MenuCursorDetector.cpp