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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Badge Check
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#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::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
);
}



} // namespace Pokemon
} // namespace PokemonAutomation
47 changes: 47 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_BadgeCheck.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* 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 <string>
#include <string_view>
#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. A badge not seen is not a badge confirmed: this fails too.
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 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);



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* BDSP AutoStory - Actions Tests

Check warning on line 1 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=859 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand All @@ -9,6 +9,7 @@
*
*/

#include <algorithm>
#include <chrono>
#include <format>
#include <functional>
Expand All @@ -17,23 +18,28 @@
#include <vector>
#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"
#include "CommonFramework/Logging/Logger.h"
#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_Battle.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"
#include "PokemonBDSP_AutoStory_NewGame.h"
#include "PokemonBDSP_AutoStory_TalkOver.h"
#include "PokemonBDSP_AutoStory_Actions_Tests.h"

namespace PokemonAutomation{
Expand Down Expand Up @@ -493,6 +499,150 @@
return true;
}

// 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<Pokemon::ScreenEvents>& 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;
}
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 (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<ScreenEvents> looks; GymOpponent opponent; };
std::vector<ScreenEvents> roark(20, box);
roark.insert(roark.end(), 3, fanfare);
roark.insert(roark.end(), 12, box);
const Case cases[] = {
{"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 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;
}

// 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&){

Check warning on line 578 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::test_potion_ledger_stops_without_gain( Logger &): function_lines=40 (advisory above 25, limit 50)
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;
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()){
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;
}

UnitTestResult test_choose_move(Logger&){
struct Case{ std::string_view name; std::vector<MoveSlot> slots; size_t best; };
const Case cases[] = {
Expand Down Expand Up @@ -675,6 +825,22 @@
.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: 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 = {},
},
{
.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 = {},
},
{
.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 = {},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* 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(

Check warning on line 25 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::verify_badge( VideoStream & stream , ProControllerContext & context , Pokemon :: StoryCheckLevel level , size_t index , std :: string_view badge , int menu_icons): function_lines=32 (advisory above 25, limit 50)
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/";

Check warning on line 42 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
Filesystem::create_directories(Filesystem::Path(folder));
const std::string path = folder + now_to_filestring() + ".png";

Check warning on line 44 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
card.frame->save(path);
stream.log("Trainer Card screenshot: " + path);

Check warning on line 46 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")

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(std::string("Badge check: the ") + std::string(badge) + " is on the Trainer Card.");

Check warning on line 55 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}



} // namespace PokemonBDSP
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -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 <cstddef>
#include <string_view>
#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 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
);



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