diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index ba282b0a9a..86768e52cf 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -394,7 +394,7 @@ jobs: with: # The fork's fixtures: upstream's, the SystemKeyboardDetector and HomeTileReader screenshots and pending FRLG image submissions (Fly landing, party menu, Route 21 battle, Pokemon Mansion 1F and B1F hold frames, and the Pidgey known-moves frame, the PC's box-screen fade, Bruno's Onix Roar battle menus). repository: 'dayfine/CommandLineTests' - ref: '3c9d2c1dfb31af1e2a1ff44e3f71a24f50f5238d' + ref: 'ab6b437ceae2f346c60f891c5bd282f6b8dd1fac' path: 'CommandLineTests' # The unit tests assume the downloadable resources they need are already downloaded diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp index f99277ac70..dd4ae42798 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp @@ -252,17 +252,25 @@ class VerifyRow{ "[Verify] {}: phase 1, {} tries from one save, {} must pass; phase 2, up to {} tries, the first pass saved", id, m_verify.tries, m_verify.min_pass, m_verify.reset_budget ), m_hooks); + // Phase 1 ends as soon as its verdict is known: min_pass passes, or + // too few tries left to reach it. size_t passes = 0; - for (size_t c = 1; c <= m_verify.tries; c++){ + for (size_t c = 1; c <= m_verify.tries && passes < m_verify.min_pass; c++){ const std::optional passed = try_once(/*phase=*/1, c, m_verify.tries); if (!passed){ return std::move(m_result); } passes += *passed ? 1 : 0; + if (passes + (m_verify.tries - c) < m_verify.min_pass){ + return stop("verify_below_min_pass", std::format( + "phase 1 passed {} of {}, {} left: under {}", passes, c, m_verify.tries - c, m_verify.min_pass + )); + } } if (passes < m_verify.min_pass){ return stop("verify_below_min_pass", std::format("phase 1 passed {} of {}, under {}", passes, m_verify.tries, m_verify.min_pass)); } + say(std::format("[Verify] {}: phase 1 reached {} passes", id, passes), m_hooks); for (size_t c = 1; c <= m_verify.reset_budget; c++){ const std::optional passed = try_once(/*phase=*/2, c, m_verify.reset_budget); if (!passed){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryVerifyRows_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryVerifyRows_Tests.cpp index 4786e899bb..48fcfc2c90 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryVerifyRows_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryVerifyRows_Tests.cpp @@ -185,9 +185,14 @@ class Test_StoryVerifyRow : public UnitTest{ const VerifyRowCase VERIFY_ROW_CASES[] = { { - .name = "ten passes: saved on the first phase 2 try", + .name = "every try passes: phase 1 stops at its fifth pass, saved on the first phase 2 try", .input = {.tries = {win()}}, - .expectation = {.completed = true, .fights = 11, .saves = 1, .reloads = 10}, + .expectation = {.completed = true, .fights = 6, .saves = 1, .reloads = 5}, + }, + { + .name = "six losses first: min_pass 5 out of reach, phase 1 stops there, no save", + .input = {.tries = {lose()}}, + .expectation = {.failure = "verify_below_min_pass", .fights = 6, .saves = 0, .reloads = 5}, }, { .name = "four passes in phase 1, under min_pass 5: stop, no save", @@ -196,18 +201,18 @@ const VerifyRowCase VERIFY_ROW_CASES[] = { }, { .name = "phase 2 passes on its third try: saved once", - .input = {.tries = joined(joined(repeated(5, win()), repeated(7, lose())), {win()})}, - .expectation = {.completed = true, .fights = 13, .saves = 1, .reloads = 12}, + .input = {.tries = joined(joined(repeated(5, win()), repeated(2, lose())), {win()})}, + .expectation = {.completed = true, .fights = 8, .saves = 1, .reloads = 7}, }, { .name = "no pass in the reset budget of 5: stop, no save", .input = {.tries = joined(repeated(5, win()), {lose()})}, - .expectation = {.failure = "verify_out_of_budget", .fights = 15, .saves = 0, .reloads = 14}, + .expectation = {.failure = "verify_out_of_budget", .fights = 10, .saves = 0, .reloads = 9}, }, { .name = "wins over the game's cap don't pass: never saved", .input = {.tries = joined(repeated(5, win()), {win_over_cap()})}, - .expectation = {.failure = "verify_out_of_budget", .fights = 15, .saves = 0, .reloads = 14}, + .expectation = {.failure = "verify_out_of_budget", .fights = 10, .saves = 0, .reloads = 9}, }, { .name = "the save is short of the start check: stop before fighting", @@ -217,7 +222,7 @@ const VerifyRowCase VERIFY_ROW_CASES[] = { { .name = "a won try whose postcondition check fails right before the save: never saved", .input = {.verify = {.tries = 2, .min_pass = 0, .reset_budget = 2}, .tries = {win()}, .postconditions_hold = false}, - .expectation = {.failure = "verify_out_of_budget", .fights = 4, .saves = 0, .reloads = 3}, + .expectation = {.failure = "verify_out_of_budget", .fights = 2, .saves = 0, .reloads = 1}, }, { .name = "the pass check can't read the game: no save, the loop stops at once", @@ -225,9 +230,9 @@ const VerifyRowCase VERIFY_ROW_CASES[] = { .expectation = {.failure = "postcondition_unread", .fights = 1, .saves = 0, .reloads = 0}, }, { - .name = "min_pass 0 of 2: straight to phase 2", - .input = {.verify = {.tries = 2, .min_pass = 0, .reset_budget = 1}, .tries = {lose(), lose(), win()}}, - .expectation = {.completed = true, .fights = 3, .saves = 1, .reloads = 2}, + .name = "min_pass 0 of 2: no phase 1 try, straight to phase 2", + .input = {.verify = {.tries = 2, .min_pass = 0, .reset_budget = 1}, .tries = {win()}}, + .expectation = {.completed = true, .fights = 1, .saves = 1, .reloads = 0}, }, }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.h index 029d87f526..852f3aa50d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.h @@ -30,6 +30,7 @@ #include #include +#include #include #include "Common/Cpp/Logging/AbstractLogger.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" @@ -76,6 +77,9 @@ struct PcView{ std::optional box = std::nullopt; // BOX_GRID: whether the cell under the hand holds a Pokemon, if read. std::optional occupied = std::nullopt; + // BOX_GRID: the species' slug of the Pokemon under the hand, if read + // (BoxCellReader, by the caller: this reader has no language). + std::optional species = std::nullopt; }; // The PC's screen on `screen`, with its cursor where it can be read. diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp index 9fd3102219..c4f3d1c5df 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp @@ -31,8 +31,11 @@ namespace PokemonFRLG{ namespace{ // Measured on captures of the page (console B, 2026-11-02): names from -// x 163, y 24 to 32 (with the shadow); the PP's digits y 34 to 43, the -// current PP's to x 216, "/" from 217 to 224, the full PP's from 224. +// x 163, y 24 to 32 (with the shadow); the PP's digits y 34 to 43. Across, +// the game prints (pret pokefirered src/pokemon_summary_screen.c, +// PrintMoveNameAndPP, the pane from x 160) the "PP" label from x 196 to 205, +// the current PP right-aligned in two 6-pixel cells from 206 to 217, "/" from +// 218 and the full PP's two cells from 224. The label takes the PP's colour. constexpr double ROW_STEP = 28; constexpr double NAME_TOP = 22; constexpr double PP_TOP = 33; @@ -217,8 +220,41 @@ bool row_has_move(const ImageViewRGB32& screen, size_t row){ return dark * 5 > badge.width() * badge.height(); } +// The colours the game writes a move's PP in (pret pokefirered +// src/pokemon_summary_screen.c, sPrintMoveTextColors, palette +// graphics/summary_screen/text_moves.pal): dark grey (32, 32, 32) on a grey +// shadow (222, 222, 222) at full PP or above half; yellow (238, 222, 0) at +// half or less, orange (255, 148, 0) at a quarter or less and red (238, 0, 0) +// at 0, on the shadows (255, 246, 139), (255, 238, 115) and (246, 222, 156). +// The three low-PP colours are the only ones with far more red than blue, +// and their ink has no blue while their shadows have 115 or more. +constexpr uint32_t DARK_PP_INK = 0xff202020; +constexpr uint32_t DARK_PP_SHADOW = 0xffdedede; +constexpr int LOW_PP_MIN_RED_OVER_BLUE = 80; +constexpr int LOW_PP_INK_MAX_BLUE = 64; + +// `pp` with low-PP yellow, orange and red redrawn in the dark grey the digit +// templates are cut from: their ink as the dark ink, their shadows as its +// shadow. Dark-grey PP comes back as it was. +ImageRGB32 pp_in_dark_ink(const ImageViewRGB32& pp){ + ImageRGB32 ink = pp.copy(); + for (size_t y = 0; y < ink.height(); y++){ + for (size_t x = 0; x < ink.width(); x++){ + const uint32_t pixel = ink.pixel(x, y); + const int red = (pixel >> 16) & 0xff; + const int blue = pixel & 0xff; + if (red - blue < LOW_PP_MIN_RED_OVER_BLUE){ + continue; + } + ink.pixel(x, y) = blue < LOW_PP_INK_MAX_BLUE ? DARK_PP_INK : DARK_PP_SHADOW; + } + } + return ink; +} + int read_pp(Logger& logger, const ImageViewRGB32& screen, const ImageFloatBox& box){ - const int pp = read_digits_waterfill_template(logger, extract_box_reference(game_box(screen), box), DigitTemplateType::StatBox); + const ImageRGB32 ink = pp_in_dark_ink(extract_box_reference(game_box(screen), box)); + const int pp = read_digits_waterfill_template(logger, ink, DigitTemplateType::StatBox); // A move has at most 64 PP (40 with three PP Ups). return pp >= 0 && pp <= 64 ? pp : -1; } @@ -229,7 +265,8 @@ ImageFloatBox summary_move_name_box(size_t row){ return ImageFloatBox(161.0 / 240, (NAME_TOP + ROW_STEP * double(row)) / 160, 76.0 / 240, 12.0 / 160); } ImageFloatBox summary_move_pp_box(size_t row){ - return ImageFloatBox(204.0 / 240, (PP_TOP + ROW_STEP * double(row)) / 160, 13.0 / 240, 11.0 / 160); + // From 206: the label's last column at 204 read as a "4" in yellow ink. + return ImageFloatBox(206.0 / 240, (PP_TOP + ROW_STEP * double(row)) / 160, 12.0 / 240, 11.0 / 160); } ImageFloatBox summary_move_max_pp_box(size_t row){ return ImageFloatBox(224.0 / 240, (PP_TOP + ROW_STEP * double(row)) / 160, 15.0 / 240, 11.0 / 160); @@ -253,11 +290,17 @@ std::vector read_summary_moves(Logger& logger, Language language, c if (!row_has_move(screen, row)){ break; } - moves.push_back(SummaryMove{ + SummaryMove move{ .move = read_summary_move_name(logger, language, screen, row), .pp = read_pp(logger, screen, summary_move_pp_box(row)), .max_pp = read_pp(logger, screen, summary_move_max_pp_box(row)), - }); + }; + // More PP than the full PP is a misread, not a reading: unread + // (the League after Agatha: SURF's yellow "6/15" read as 46/15). + if (move.max_pp >= 0 && move.pp > move.max_pp){ + move.pp = -1; + } + moves.push_back(std::move(move)); } return moves; } @@ -367,6 +410,19 @@ void add_tests_SummaryMovesReader(UnitTestDatabase& database){ // read as attract, ?, attract and steel-wing before its line's moves // were in MOVE_NAMES. database.add("PokemonFRLG/SummaryMovesReader/Pidgey_KnownMoves_eng.png"); + // FireRed at the League after Agatha, 2026-11-17: Blastoise with BITE + // 9/25 and SURF 6/15 in yellow, read as -1/-1 while only dark ink was + // read; then BITE 15/25 and SURF 6/15, its yellow "6" read as 46 while + // the box held the yellow "PP" label's edge. Added once the images are in + // the test repo. + for (std::string_view low_pp : { + "PokemonFRLG/SummaryMovesReader/Blastoise_LowPP_eng.png", + "PokemonFRLG/SummaryMovesReader/Blastoise_Surf6_eng.png", + }){ + if (Filesystem::exists(std::format("{}{}", UNIT_TEST_RESOURCE_PATH(), low_pp))){ + database.add(std::string(low_pp)); + } + } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index f3047bd791..de4d04fb9d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -52,6 +52,7 @@ #include "Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryCapture.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryPaceSim.h" +#include "Programs/AutoStory/PokemonFRLG_AutoStoryHallOfFame.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryCheckLevel_Tests.h" @@ -208,6 +209,7 @@ static void add_tests_AutoStoryAll(UnitTestDatabase& database){ add_tests_AutoStoryRngLoop(database); add_tests_AutoStoryCapture(database); add_tests_AutoStoryPcStorage(database); + add_tests_AutoStoryHallOfFame(database); add_tests_AutoStoryAbilities(database); add_tests_AutoStoryCheckLevel(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.cpp index 54ce1fd891..24e6b928b6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory.cpp @@ -557,14 +557,14 @@ static void run_story_rows( } Pokemon::StoryGameHooks game = story_game(env, story); Pokemon::SegmentHooks hooks = Pokemon::story_segment_hooks(game); - // A verify row's try passes by the League's item caps, counted from - // its start save (the loop reloads it before each try), and by our - // side as read off the console meeting the next fight's entry - // contract: checked right before the save, never on a guess. + // A verify row's try passes by our side as read off the console + // after the fight, the bag's balance and the lead's HP and PP, + // meeting the next fight's entry contract: checked right before the + // save, never on a guess. hooks.verify_pass = [&story, &rows](const std::string& id, Pokemon::RunState&){ for (const StoryCheckpoint& row : rows){ if (row.id == id){ - return check_league_save(story, row, story.saved_supplies); + return check_league_save(story, row); } } return Pokemon::outcome_ok(); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp index 64a3249f0e..2e1c6f33cb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp @@ -57,6 +57,9 @@ constexpr Milliseconds BATTLE_TIMEOUT = 600000ms; constexpr Milliseconds BATTLE_START_TIMEOUT = 20000ms; // No battle screen for this long: check for the overworld with START. constexpr Milliseconds BATTLE_GONE = 2000ms; +// After a won battle, how long without a START menu before the scene after +// it is taken as the next step's. +constexpr Milliseconds WON_SCENE = 10000ms; // A battle screen with neither an arrow nor a menu for this long: B, for // a prompt the detectors miss. constexpr Milliseconds STUCK = 8000ms; @@ -433,6 +436,13 @@ class BattleLoop{ return true; } } + // Won, and no START menu after it: a scene follows the battle (the + // Champion's, Oak into the Hall of Fame: console B, 2026-11-17), + // which is the next step's. + if (m_won && now - m_last_battle > WON_SCENE){ + m_console.log("Battle: won (the prize text read); the scene after it is the next step's."); + return true; + } // Oak's white text box in the rival's battle reads as no battle screen: // B moves it on, and B does nothing on the overworld once the battle is over. if (now - m_last_press > 1500ms){ @@ -665,6 +675,8 @@ class BattleLoop{ std::optional letters = read_battle_text(m_language, screen); if (letters && (m_policy_texts.empty() || m_policy_texts.back() != *letters)){ m_console.log(std::format("Battle: text \"{}\".", *letters)); + // pokefirered: "{PLAYER} got ¥{N}\nfor winning!", the battle's last text. + m_won = m_won || letters->find("forwinning") != std::string::npos; m_policy_texts.push_back(std::move(*letters)); } } @@ -692,6 +704,8 @@ class BattleLoop{ std::vector m_policy_texts; BattleScreen m_last_seen = BattleScreen::NONE; bool m_seen_battle = false; + // The prize text "for winning" read (League fights read the battle's texts). + bool m_won = false; bool m_confirmed = false; bool m_press_a = true; // A move was chosen and no battle menu has shown since. diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp index 6e195a3da3..d2ecdebd8c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp @@ -198,6 +198,7 @@ std::vector recovery_items(const Supplies& supplies){ BattleDecisions::BattleDecisions(Supplies& supplies, BattleKind kind) : m_supplies(supplies) , m_kind(kind) + , m_foes(supplies.foes) { if (supplies.lead.level > 0){ m_level_cap = static_cast(std::min(battle_level(supplies.lead) + MAX_LEVELS_IN_BATTLE, 255)); @@ -313,14 +314,7 @@ TurnAction BattleDecisions::switch_lead_back(){ } size_t BattleDecisions::track_foe(){ - const std::vector& foes = m_supplies.foes; - for (size_t index = m_foe_index; index < foes.size() && !m_supplies.foe_out.empty(); index++){ - if (foes[index] == m_supplies.foe_out){ - m_foe_index = index; - break; - } - } - return m_foe_index; + return m_foes.out(m_supplies.foe_out); } TurnAction BattleDecisions::policy_turn(const PartyStatus& status, std::string heal_reason){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h index 4f1ad5f8bc..f972f5a9c4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h @@ -30,6 +30,7 @@ #include "Pokemon/AutoStory/Pokemon_ScriptedReadings.h" #include "PokemonFRLG_AutoStoryBattle.h" #include "PokemonFRLG_AutoStoryBattlePolicy.h" +#include "PokemonFRLG_AutoStoryBattleTrack.h" #include "PokemonFRLG_AutoStoryParty.h" #include "PokemonFRLG_AutoStoryPlannedBattle.h" #include "PokemonFRLG_AutoStoryBattleTurns.h" @@ -129,7 +130,8 @@ class BattleDecisions{ std::optional m_planned; // The lead's Attack and Sp. Atk stages from the X items used, for its attack. AttackContext m_attack; - size_t m_foe_index = 0; + // The step's foes as they came out. + FoeParty m_foes; bool m_lost = false; // A switch back to the lead couldn't be made: the rule is off. bool m_switch_off = false; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattlePolicy.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattlePolicy.h index 1d97d319de..69ba9a2a5e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattlePolicy.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattlePolicy.h @@ -86,7 +86,8 @@ struct PolicyView{ StatusCondition status = StatusCondition::NONE; // Its Speed as POKEMON SKILLS shows it, before badges and stages. int speed = 0; - // The foe out, in the step's "foes" order; those before it have fainted. + // The foe out, as its slot in the step's "foes" (FoeParty: the + // trainer may send them in any order). size_t foe_index = 0; // The foe's HP bar's filled share, 1 when not read. double foe_fill = 1.0; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTrack.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTrack.cpp index 0b0c487cdc..e6a441f2dd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTrack.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTrack.cpp @@ -9,6 +9,7 @@ #include #include +#include #include "PokemonFRLG_AutoStoryBattleTrack.h" namespace PokemonAutomation{ @@ -287,6 +288,41 @@ void BattleTrack::turn_chosen(){ } +FoeParty::FoeParty(std::vector species) + : m_species(std::move(species)) + , m_fallen(m_species.size(), false) +{} + +size_t FoeParty::out(std::string_view species){ + m_fell.reset(); + std::optional found; + for (size_t slot = 0; slot < m_species.size() && !found; slot++){ + if (!m_fallen[slot] && m_species[slot] == species){ + found = slot; + } + } + for (size_t slot = 0; slot < m_species.size() && !found; slot++){ + if (m_species[slot] == species){ + found = slot; + } + } + if (!found){ + return m_out.value_or(0); + } + if (m_out && *m_out != *found){ + m_fallen[*m_out] = true; + m_fell = m_out; + } + m_fallen[*found] = false; + m_out = found; + return *found; +} + +int FoeParty::alive() const{ + return static_cast(std::count(m_fallen.begin(), m_fallen.end(), false)); +} + + } // namespace PokemonFRLG } // namespace NintendoSwitch diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTrack.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTrack.h index e23ac2f485..12528414b5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTrack.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTrack.h @@ -97,6 +97,36 @@ class BattleTrack{ }; +// A trainer's party as it comes out. Gen 3's trainer AI picks the next +// Pokemon by its matchup with ours, not by party slot (console B, +// 2026-11-17, Champion try 1: Venusaur, the last slot, came out second). +// A member that leaves the field is taken to have fallen (no text read +// tells a faint from the AI's rare switch); one that comes back is up. +class FoeParty{ +public: + // The party's species by slot. + explicit FoeParty(std::vector species); + + // The foe out is `species`: the first member of it not fallen (else + // the first of it, back after all). The one out before, if another, + // fell. Not read or not in the party: the one out stays. Returns the + // foe's slot. + size_t out(std::string_view species); + // The member that fell at the last out(), if one did. + std::optional fell() const{ + return m_fell; + } + // The members not fallen, the one out included. + int alive() const; + +private: + std::vector m_species; + std::vector m_fallen; + std::optional m_out; + std::optional m_fell; +}; + + // Registers the battle track tests. void add_tests_AutoStoryBattleTrack(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTrack_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTrack_Tests.cpp index 349aa8dc96..8b53565067 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTrack_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTrack_Tests.cpp @@ -8,6 +8,7 @@ */ #include +#include #include #include #include @@ -238,6 +239,101 @@ const TrackCase TRACK_CASES[] = { }, }; + +const std::vector CHAMPION_PARTY = {"pidgeot", "alakazam", "rhydon", "gyarados", "arcanine", "venusaur"}; +const std::vector AGATHA_PARTY = {"gengar", "golbat", "haunter", "arbok", "gengar"}; + +struct FoePartyInput{ + std::vector party = {}; + // The species read out each turn, in order. + std::vector outs = {}; +}; +// What out() gave each turn. +struct FoeSeen{ + size_t slot = 0; + int alive = 0; + std::optional fell = std::nullopt; + + bool operator==(const FoeSeen&) const = default; +}; +struct FoePartyExpectation{ + std::vector seen = {}; +}; +struct FoePartyCase{ + std::string name; + FoePartyInput input; + FoePartyExpectation expectation; +}; + +std::string describe(const std::vector& seen){ + std::string text; + for (const FoeSeen& turn : seen){ + text += std::format("[slot {}, {} alive, fell {}] ", turn.slot, turn.alive, turn.fell ? std::to_string(*turn.fell) : "-"); + } + return text; +} + +class Test_FoeParty : public UnitTest{ +public: + explicit Test_FoeParty(FoePartyCase test_case) + : UnitTest(std::format("PokemonFRLG::AutoStoryBattleTrack - foe party: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + FoeParty party(m_case.input.party); + std::vector got; + for (const std::string& species : m_case.input.outs){ + const size_t slot = party.out(species); + got.push_back(FoeSeen{.slot = slot, .alive = party.alive(), .fell = party.fell()}); + } + if (got != m_case.expectation.seen){ + return std::format("Got {}; expected {}.", describe(got), describe(m_case.expectation.seen)); + } + return true; + } + +private: + FoePartyCase m_case; +}; + +const FoePartyCase FOE_PARTY_CASES[] = { + { + .name = "console B, 2026-11-17, Champion try 1: sent out of party order", + .input = { + .party = CHAMPION_PARTY, + .outs = {"pidgeot", "venusaur", "alakazam", "rhydon", "gyarados", "arcanine"}, + }, + .expectation = {.seen = { + {.slot = 0, .alive = 6}, + {.slot = 5, .alive = 5, .fell = 0}, + {.slot = 1, .alive = 4, .fell = 5}, + {.slot = 2, .alive = 3, .fell = 1}, + {.slot = 3, .alive = 2, .fell = 2}, + {.slot = 4, .alive = 1, .fell = 3}, + }}, + }, + { + .name = "the same foe over turns: none fell", + .input = {.party = CHAMPION_PARTY, .outs = {"pidgeot", "pidgeot", "pidgeot"}}, + .expectation = {.seen = {{.slot = 0, .alive = 6}, {.slot = 0, .alive = 6}, {.slot = 0, .alive = 6}}}, + }, + { + .name = "a name not read: the one out stays", + .input = {.party = CHAMPION_PARTY, .outs = {"pidgeot", "", "missingno"}}, + .expectation = {.seen = {{.slot = 0, .alive = 6}, {.slot = 0, .alive = 6}, {.slot = 0, .alive = 6}}}, + }, + { + .name = "Agatha's second Gengar after the first fell", + .input = {.party = AGATHA_PARTY, .outs = {"gengar", "golbat", "gengar"}}, + .expectation = {.seen = {{.slot = 0, .alive = 5}, {.slot = 1, .alive = 4, .fell = 0}, {.slot = 4, .alive = 3, .fell = 1}}}, + }, + { + .name = "a foe back after a switch: up again", + .input = {.party = CHAMPION_PARTY, .outs = {"pidgeot", "rhydon", "pidgeot"}}, + .expectation = {.seen = {{.slot = 0, .alive = 6}, {.slot = 2, .alive = 5, .fell = 0}, {.slot = 0, .alive = 5, .fell = 2}}}, + }, +}; + } // namespace @@ -245,6 +341,9 @@ void add_tests_AutoStoryBattleTrack(UnitTestDatabase& database){ for (const TrackCase& test_case : TRACK_CASES){ database.add(test_case); } + for (const FoePartyCase& test_case : FOE_PARTY_CASES){ + database.add(test_case); + } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCapture.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCapture.cpp index fe374ac8c6..3178b11cfd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCapture.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCapture.cpp @@ -4,6 +4,7 @@ * */ +#include #include #include "CommonFramework/Exceptions/OperationFailedException.h" #include "CommonFramework/Exceptions/ProgramFinishedException.h" @@ -339,6 +340,7 @@ std::string capture_message(const CapturePlan& plan, const CaptureResult& result std::optional run_storage_game_step( + std::vector& protected_slots, Logger& logger, VideoStream* stream, const Pokemon::StoryStep& step, CaptureActions& capture, PcStorageActions& pc ){ @@ -368,6 +370,12 @@ std::optional run_storage_game_step( return Pokemon::outcome_failed("bad_args", step.action); } const PcResult result = run_pc_transfer(logger, pc, *transfer); + if ((result.end == PcEnd::DONE || result.end == PcEnd::SKIPPED) && transfer->protect && result.party_slot + && std::ranges::find(protected_slots, *result.party_slot) == protected_slots.end() + ){ + protected_slots.push_back(*result.party_slot); + logger.log(std::format("PC: party slot {} rides along; it is never sent into a battle.", *result.party_slot + 1)); + } switch (result.end){ case PcEnd::DONE: case PcEnd::SKIPPED: diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCapture.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCapture.h index 80c45dd43c..8c2cb989e7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCapture.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCapture.h @@ -56,7 +56,10 @@ namespace PokemonFRLG{ // throws ProgramFinishedException, so the segment stops without // reloading, with a screenshot from `stream` if given; any other end but // CAUGHT is a failed Outcome, as is a PC step that fails or is refused. +// A withdrawal with `protect` adds the party slot it joined to +// `protected_slots` (Supplies::protected_slots). std::optional run_storage_game_step( + std::vector& protected_slots, Logger& logger, VideoStream* stream, const Pokemon::StoryStep& step, CaptureActions& capture, PcStorageActions& pc ); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCapture_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCapture_Tests.cpp index 020d499288..a49cdafe85 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCapture_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCapture_Tests.cpp @@ -445,12 +445,13 @@ class StorageExecutor : public Pokemon::StoryStepExecutor{ LineLogger logger; FakeCaptureGame capture; IdlePc pc; + std::vector protected_slots; virtual Pokemon::Outcome run(const StoryStep& step, Pokemon::RunState&) override{ if (step.kind != Pokemon::StoryStepKind::GAME){ return Pokemon::unsupported_step(step); } - const std::optional outcome = run_storage_game_step(logger, nullptr, step, capture, pc); + const std::optional outcome = run_storage_game_step(protected_slots, logger, nullptr, step, capture, pc); return outcome ? *outcome : Pokemon::unsupported_step(step); } }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index a51225d506..91036003f7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -29,6 +29,7 @@ #include "PokemonFRLG_AutoStoryCapture.h" #include "PokemonFRLG_AutoStoryCenterHeal.h" #include "PokemonFRLG_AutoStoryEvents.h" +#include "PokemonFRLG_AutoStoryHallOfFame.h" #include "PokemonFRLG_AutoStoryGrind.h" #include "PokemonFRLG_AutoStoryBattlePolicy.h" #include "PokemonFRLG_AutoStoryBoulderPresses.h" @@ -689,7 +690,7 @@ Outcome open_gym_door(StoryEnv& env, RunState& state, const StoryStep& step){ Outcome other_game_step(StoryEnv& env, RunState& state, const StoryStep& step){ const Pokemon::Pose patch = state.pose; ConsoleCaptureActions capture(env.console, env.context, env.language, env.player_gender, [&]{ pace_trainer_battle(env, state, step, patch); }); - ConsolePcStorageActions pc(env.console, env.context); + ConsolePcStorageActions pc(env.console, env.context, env.language); std::optional done = run_field_game_step(env.console, env.context, env.language, env.player_gender, step, env.party); if (done && done->succeeded()){ if (const std::optional item = parse_use_item_step(step); item && is_repel(*item)){ @@ -698,7 +699,7 @@ Outcome other_game_step(StoryEnv& env, RunState& state, const StoryStep& step){ } } if (!done){ - done = run_storage_game_step(env.console.logger(), &env.console, step, capture, pc); + done = run_storage_game_step(env.supplies.protected_slots, env.console.logger(), &env.console, step, capture, pc); if (done){ env.party.invalidate(); } @@ -1003,6 +1004,25 @@ Pokemon::Checkpoint build_checkpoint(const StoryCheckpoint& data, StoryExecutor& env.start_map = data.start ? Pokemon::grid_pose_of(*data.start).map : ""; env.checkpoint = data.id; env.start_menu = data.menu; + // The Hall of Fame saves the game itself, so it is the row's save: a + // verify phase 1 try, which never saves, ends at the Champion's win + // before it (console B, 2026-11-17: the win's 60 s wait for a START + // menu called it lost under Oak's "Come with me!"). + if (!data.steps.empty() && data.steps.back().action == HALL_OF_FAME_ACTION + && !checkpoint.events.empty() && checkpoint.events.back().name == data.steps.back().name + ){ + Pokemon::Act hall = std::move(checkpoint.events.back().act); + checkpoint.events.pop_back(); + checkpoint.save = [&env, hall = std::move(hall)](RunState& state) mutable{ + const Pokemon::Outcome saved = hall(state); + if (saved.succeeded()){ + env.has_save = true; + env.saved_supplies = env.supplies; + } + return saved; + }; + return checkpoint; + } const StartMenu menu = data.menu; checkpoint.save = [&env, menu, id = data.id](RunState&){ return guarded(env, [&]{ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHallOfFame.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHallOfFame.cpp new file mode 100644 index 0000000000..e9e66c8505 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHallOfFame.cpp @@ -0,0 +1,198 @@ +/* AutoStory Hall of Fame + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageDiff.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_PokemonLeagueDetectors.h" +#include "PokemonFRLG_AutoStoryHallOfFame.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using namespace std::chrono_literals; + + +namespace{ + +// One look at the screen every so long. +constexpr Milliseconds HALL_OF_FAME_LOOK = 250ms; +// Still this many looks (1 s): a member's hold (2 s, 120 frames). +constexpr size_t MON_HOLD_LOOKS = 4; +// Changing this many looks (3 s): the confetti (6.7 s, 400 frames). A +// member's slide in takes at most 20 frames (300 px at 15 a frame). +constexpr size_t CONFETTI_LOOKS = 12; +// Still this many looks (3 s) after the confetti: the LEAGUE CHAMP card, +// which waits for A. The player's picture before it holds 2 s, then slides. +constexpr size_t CHAMPION_HOLD_LOOKS = 12; +// Two looks that differ by more than this (RMSD over the frame) changed. +// A confetti piece is 8 px and a member 64 px, of a 240x160 screen; the +// capture's noise between two frames of a still screen is under it. +constexpr double LOOK_CHANGED_RMSD = 3.0; +// The talk after the battle to the save message: EliteFourFarmer's bound. +constexpr Milliseconds SAVE_MESSAGE_TIMEOUT = 180000ms; +constexpr Milliseconds SAVE_TIMEOUT = 30000ms; +// The save's end to the card: six members (2.5 s each), the confetti +// (6.7 s) and the player's picture (about 4 s) take under 30 s. +constexpr Milliseconds SHOTS_TIMEOUT = 120000ms; + +std::string hall_of_fame_folder(){ + return std::format("{}AutoStory/hall_of_fame/", SCREENSHOTS_PATH()); +} + +// Saves `frame` as the shot, and logs where. Returns whether it was saved. +bool save_shot(Logger& logger, const ImageViewRGB32& frame, std::string_view stamp, HallOfFameShot shot, size_t mon){ + const std::string folder = hall_of_fame_folder(); + Filesystem::create_directories(Filesystem::Path(folder)); + std::string name = std::format("{}-{}", stamp, hall_of_fame_shot_name(shot)); + if (shot == HallOfFameShot::MON){ + name += std::format("-{}", mon); + } + const std::string path = std::format("{}{}.png", folder, name); + if (!frame.save(path)){ + logger.log(std::format("Hall of Fame: the {} screenshot couldn't be saved at {}.", hall_of_fame_shot_name(shot), path), COLOR_RED); + return false; + } + logger.log(std::format("Hall of Fame: {} screenshot saved at {}.", hall_of_fame_shot_name(shot), path), COLOR_BLUE); + return true; +} + +// B through the talk until the Hall of Fame saves, and the save done. +bool wait_for_save(ConsoleHandle& console, ProControllerContext& context){ + HallOfFameSavingWatcher saving(COLOR_RED); + context.wait_for_all_requests(); + const int ret = run_until( + console, context, + [](ProControllerContext& context){ + pbf_mash_button(context, BUTTON_B, SAVE_MESSAGE_TIMEOUT); + }, + {saving} + ); + if (ret < 0){ + return false; + } + console.log("Hall of Fame: saving."); + HallOfFameSavingOverWatcher saved(COLOR_RED); + return wait_until(console, context, SAVE_TIMEOUT, {saved}) >= 0; +} + +// Looks at the Hall of Fame until the card, saving the shots. +void take_shots(ConsoleHandle& console, ProControllerContext& context){ + const std::string stamp = now_to_filestring(); + HallOfFameShots shots; + VideoSnapshot last; + VideoSnapshot run_start; + size_t mons = 0; + const WallClock end = current_time() + SHOTS_TIMEOUT; + while (!shots.done() && current_time() < end){ + context.wait_for(HALL_OF_FAME_LOOK); + VideoSnapshot frame = console.video().snapshot(); + if (!frame){ + continue; + } + const bool changed = !last + || last->width() != frame->width() || last->height() != frame->height() + || ImageMatch::pixel_RMSD(*last.frame, *frame.frame) > LOOK_CHANGED_RMSD; + const HallOfFameShot shot = shots.see(changed); + if (shots.run_started()){ + run_start = frame; + } + switch (shot){ + case HallOfFameShot::NONE: + break; + case HallOfFameShot::MON: + save_shot(console.logger(), *frame.frame, stamp, shot, ++mons); + break; + case HallOfFameShot::TEAM: + save_shot(console.logger(), *run_start.frame, stamp, shot, mons); + break; + case HallOfFameShot::CHAMPION: + save_shot(console.logger(), *frame.frame, stamp, shot, mons); + break; + } + last = std::move(frame); + } + if (!shots.done()){ + console.log(std::format("Hall of Fame: no LEAGUE CHAMP card in {} s; {} members shot.", SHOTS_TIMEOUT.count() / 1000, mons), COLOR_RED); + } +} + +} // namespace + + +std::string_view hall_of_fame_shot_name(HallOfFameShot shot){ + switch (shot){ + case HallOfFameShot::NONE: return "none"; + case HallOfFameShot::MON: return "mon"; + case HallOfFameShot::TEAM: return "team"; + case HallOfFameShot::CHAMPION: return "champion"; + } + return "?"; +} + +HallOfFameShot HallOfFameShots::see(bool changed){ + m_run_started = false; + if (m_done){ + return HallOfFameShot::NONE; + } + if (changed){ + m_run_started = m_moving == 0; + m_moving++; + m_still = 0; + m_hold_shot = false; + if (!m_confetti && m_moving == CONFETTI_LOOKS){ + m_confetti = true; + return HallOfFameShot::TEAM; + } + return HallOfFameShot::NONE; + } + m_still++; + m_moving = 0; + if (m_hold_shot){ + return HallOfFameShot::NONE; + } + if (!m_confetti && m_still == MON_HOLD_LOOKS){ + m_hold_shot = true; + return HallOfFameShot::MON; + } + if (m_confetti && m_still == CHAMPION_HOLD_LOOKS){ + m_hold_shot = true; + m_done = true; + return HallOfFameShot::CHAMPION; + } + return HallOfFameShot::NONE; +} + +Pokemon::StoryActionSpec hall_of_fame_action_spec(){ + return {.kind = Pokemon::StoryStepKind::GAME, .name = HALL_OF_FAME_ACTION, .args = {}}; +} + +std::optional run_hall_of_fame_step( + ConsoleHandle& console, ProControllerContext& context, const Pokemon::StoryStep& step +){ + if (step.action != HALL_OF_FAME_ACTION){ + return std::nullopt; + } + if (!wait_for_save(console, context)){ + return Pokemon::outcome_failed("no_hall_of_fame", "the Hall of Fame's save message didn't show and go"); + } + console.log(std::format("Hall of Fame: saved; taking the screenshots in {}.", hall_of_fame_folder())); + take_shots(console, context); + return Pokemon::outcome_ok(); +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHallOfFame.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHallOfFame.h new file mode 100644 index 0000000000..f158483cbd --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHallOfFame.h @@ -0,0 +1,112 @@ +/* AutoStory Hall of Fame + * + * From: https://github.com/PokemonAutomation/ + * + * The step after the Champion's win: through the talk to the Hall of + * Fame, and screenshots of the team there. pokefirered's hall_of_fame.c: + * + * - Task_Hof_InitTeamSaveData prints "Saving... Don't turn off the + * power." and saves (HallOfFameSavingDetector, as EliteFourFarmer + * waits for it). + * - Task_Hof_DisplayMon slides each party member in to its place + * (SpriteCB_GetOnScreen, 15 px a frame) and holds it 120 frames (2 s) + * with its number, name and level; the ones before it stay, faded. + * - Task_Hof_PaletteFadeAndPrintWelcomeText: the whole team at full + * color and "Welcome to the HALL OF FAME!"; confetti falls for 400 + * frames (6.7 s). + * - The player's picture holds 120 frames, slides right, and the + * LEAGUE CHAMP card waits for A (Task_Hof_ExitOnKeyPressed); A rolls + * the credits. + * + * So a still screen for a second is a member's hold, the first run of + * motion that lasts seconds is the confetti, whose first frame is the + * whole team, and the long still after it is the card. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryHallOfFame_H +#define PokemonAutomation_PokemonFRLG_AutoStoryHallOfFame_H + +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The GAME step after the Champion's win: B through the talk until the +// Hall of Fame saves, then its screenshots (HallOfFameShots) in +// Screenshots/AutoStory/hall_of_fame/, each path logged. It ends on the +// LEAGUE CHAMP card, before the credits. No args. +inline const std::string HALL_OF_FAME_ACTION = "hall_of_fame"; + +// A screenshot of the Hall of Fame. +enum class HallOfFameShot{ + // Not one to keep. + NONE, + // A party member on its own, held with its name. + MON, + // The whole team, as the confetti starts. + TEAM, + // The LEAGUE CHAMP card, which waits for A. + CHAMPION, +}; +// "mon", "team", "champion": the shot's part of its file name. +std::string_view hall_of_fame_shot_name(HallOfFameShot shot); + +// Which looks at the Hall of Fame to keep, from whether each look, one +// every HALL_OF_FAME_LOOK (in the .cpp), differs from the one before. +class HallOfFameShots{ +public: + // One look: the shot it is. TEAM is the frame that started this run of + // changed looks (run_started()), which the caller kept. + HallOfFameShot see(bool changed); + // The last look started a run of changed looks: the caller keeps its + // frame, the TEAM shot if the run turns out to be the confetti. + bool run_started() const{ + return m_run_started; + } + // The CHAMPION card was seen: the scene is over. + bool done() const{ + return m_done; + } + +private: + size_t m_still = 0; + size_t m_moving = 0; + // The still run going on was shot. + bool m_hold_shot = false; + bool m_confetti = false; + bool m_run_started = false; + bool m_done = false; +}; + +// HALL_OF_FAME_ACTION's spec. +Pokemon::StoryActionSpec hall_of_fame_action_spec(); + +// On the console, a HALL_OF_FAME_ACTION step; nothing for another. Fails +// if the save message doesn't show; the screenshots are logged, and one +// that can't be taken doesn't fail the step. +std::optional run_hall_of_fame_step( + ConsoleHandle& console, ProControllerContext& context, const Pokemon::StoryStep& step +); + + +// Register the Hall of Fame shot tests. +void add_tests_AutoStoryHallOfFame(UnitTestDatabase& database); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHallOfFame_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHallOfFame_Tests.cpp new file mode 100644 index 0000000000..7cd4f165cf --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHallOfFame_Tests.cpp @@ -0,0 +1,144 @@ +/* AutoStory Hall of Fame Tests + * + * From: https://github.com/PokemonAutomation/ + * + * HallOfFameShots on the looks pokefirered's Hall of Fame gives at one + * every 250 ms (hall_of_fame.c): each member slides in (under 0.4 s) and + * holds 2 s, the confetti moves for 6.7 s, the player's picture holds + * 2 s and slides, and the LEAGUE CHAMP card waits for A. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonFRLG_AutoStoryHallOfFame.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// `count` looks, each changed or still. +void add_looks(size_t count, bool changed, std::vector& looks){ + looks.insert(looks.end(), count, changed); +} + +// The looks from the save message's end to the card, with `members` +// party members and a card shown `card_looks` looks. +std::vector hall_of_fame_looks(size_t members, size_t card_looks){ + std::vector looks; + for (size_t c = 0; c < members; c++){ + add_looks(/*count=*/2, /*changed=*/true, looks); // the slide in + add_looks(/*count=*/8, /*changed=*/false, looks); // the 2 s hold with its name + } + add_looks(/*count=*/27, /*changed=*/true, looks); // the welcome and the confetti + add_looks(/*count=*/2, /*changed=*/true, looks); // the fade to the player + add_looks(/*count=*/8, /*changed=*/false, looks); // the player's picture holds 2 s + add_looks(/*count=*/5, /*changed=*/true, looks); // and slides right + add_looks(/*count=*/card_looks, /*changed=*/false, looks); + return looks; +} + +struct HallOfFameShotsInput{ + std::vector looks; +}; + +struct HallOfFameShotsExpectation{ + size_t mons; + // The look whose frame the TEAM shot is (the first of the confetti), if any. + std::optional team_frame; + bool champion; +}; + +struct HallOfFameShotsCase{ + std::string name; + HallOfFameShotsInput input; + HallOfFameShotsExpectation expectation; +}; + +class Test_AutoStoryHallOfFameShots : public UnitTest{ +public: + explicit Test_AutoStoryHallOfFameShots(HallOfFameShotsCase test_case) + : UnitTest(std::format("PokemonFRLG::AutoStory - Hall of Fame shots: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + HallOfFameShots shots; + size_t mons = 0; + size_t teams = 0; + size_t champions = 0; + std::optional run_start; + std::optional team_frame; + for (size_t look = 0; look < m_case.input.looks.size(); look++){ + const HallOfFameShot shot = shots.see(m_case.input.looks[look]); + if (shots.run_started()){ + run_start = look; + } + mons += shot == HallOfFameShot::MON; + champions += shot == HallOfFameShot::CHAMPION; + if (shot == HallOfFameShot::TEAM){ + teams++; + team_frame = run_start; + } + } + const HallOfFameShotsExpectation& expected = m_case.expectation; + if (mons != expected.mons){ + return std::format("{} member shots, expected {}.", mons, expected.mons); + } + if (teams > 1 || team_frame != expected.team_frame){ + return std::format("{} team shots, of look {}.", teams, team_frame ? std::to_string(*team_frame) : "-"); + } + if ((champions == 1) != expected.champion || champions > 1 || shots.done() != expected.champion){ + return std::format("{} card shots.", champions); + } + return true; + } + +private: + HallOfFameShotsCase m_case; +}; + +const HallOfFameShotsCase HALL_OF_FAME_SHOTS_CASES[] = { + { + .name = "a party of six", + .input = {.looks = hall_of_fame_looks(6, 20)}, + .expectation = {.mons = 6, .team_frame = 60, .champion = true}, + }, + { + .name = "a party of two", + .input = {.looks = hall_of_fame_looks(2, 20)}, + .expectation = {.mons = 2, .team_frame = 20, .champion = true}, + }, + { + // The player's picture holds 2 s before it slides: not the card. + .name = "the card not reached yet", + .input = {.looks = hall_of_fame_looks(6, 8)}, + .expectation = {.mons = 6, .team_frame = 60, .champion = false}, + }, + { + .name = "a still screen with no team", + .input = {.looks = std::vector(40, false)}, + .expectation = {.mons = 1, .team_frame = std::nullopt, .champion = false}, + }, +}; + +} // namespace + + +void add_tests_AutoStoryHallOfFame(UnitTestDatabase& database){ + for (const HallOfFameShotsCase& test_case : HALL_OF_FAME_SHOTS_CASES){ + database.add(test_case); + } +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp index 52a35df051..4814885195 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.cpp @@ -12,6 +12,7 @@ #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldBike.h" #include "PokemonFRLG_AutoStoryActions.h" #include "PokemonFRLG_AutoStoryFieldMoves.h" +#include "PokemonFRLG_AutoStoryHallOfFame.h" #include "PokemonFRLG_AutoStoryHideout.h" #include "PokemonFRLG_AutoStoryHideoutLift.h" #include "PokemonFRLG_AutoStoryItemUse.h" @@ -205,6 +206,9 @@ std::optional run_field_game_step( push_poster_switch(console, context); return Pokemon::outcome_ok(); } + if (const std::optional hall = run_hall_of_fame_step(console, context, step)){ + return hall->succeeded() ? done() : *hall; + } if (!is_item_use_step(step)){ return std::nullopt; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.h index 5e1b50afc1..5c472c6e34 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHideout.h @@ -61,8 +61,9 @@ std::vector field_action_specs(); void push_poster_switch(ConsoleHandle& console, ProControllerContext& context); // On the console, a GAME step of this file's, a Pokemon Center's -// (is_center_variant()), a field move (is_field_move_step()) or an item -// from the bag (is_item_use_step()); +// (is_center_variant()), a field move (is_field_move_step()), an item +// from the bag (is_item_use_step()) or the Hall of Fame's +// (HALL_OF_FAME_ACTION); // nothing for another GAME step. The step ends on its pose_after, if it // has one (an Escape Rope's, a lift ride's, a Center's). std::optional run_field_game_step( diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemsPocketChange.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemsPocketChange.cpp index 70c49cc76c..d2c8785593 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemsPocketChange.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItemsPocketChange.cpp @@ -10,6 +10,7 @@ #include "PokemonFRLG_AutoStoryFossil.h" #include "PokemonFRLG_AutoStoryGrindStep.h" #include "PokemonFRLG_AutoStoryHealingSteps.h" +#include "PokemonFRLG_AutoStoryHallOfFame.h" #include "PokemonFRLG_AutoStoryLeagueSteps.h" #include "PokemonFRLG_AutoStoryHideout.h" #include "PokemonFRLG_AutoStoryItemsPocketChange.h" @@ -98,6 +99,7 @@ const std::vector& items_pocket_change_rows(){ {StoryStepKind::GAME, RESTORE_PP, ItemsPocketChange::COUNTED}, {StoryStepKind::GAME, CHECK_FULL_HP_ACTION, ItemsPocketChange::NONE}, {StoryStepKind::GAME, BAG_CHECK_ACTION, ItemsPocketChange::NONE}, + {StoryStepKind::GAME, HALL_OF_FAME_ACTION, ItemsPocketChange::NONE}, {StoryStepKind::GAME, LEAGUE_TOP_UP_ACTION, ItemsPocketChange::COUNTED}, }; return ROWS; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueRows.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueRows.cpp index 97361e4d78..01bb736f7a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueRows.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueRows.cpp @@ -61,17 +61,6 @@ const LeagueFightData* next_league_fight(const LeagueFightData& fight){ return nullptr; } -SlugCounts supplies_counts(const Supplies& supplies){ - SlugCounts counts(supplies.other_counts.begin(), supplies.other_counts.end()); - counts["potion"] = supplies.potions; - counts["super-potion"] = supplies.super_potions; - counts["antidote"] = supplies.antidotes; - counts["parlyz-heal"] = supplies.parlyz_heals; - counts["awakening"] = supplies.awakenings; - counts["full-heal"] = supplies.full_heals; - return counts; -} - LeagueEntryCheck league_entry_check(const LeagueFightData& next, LeagueSide side){ LeagueEntryCheck check; PlannedCarry carry{.hp = side.hp, .max_hp = side.max_hp, .pp = side.pp, .full_pp = side.full_pp, .bag = side.bag}; @@ -88,23 +77,11 @@ LeagueEntryCheck league_entry_check(const LeagueFightData& next, LeagueSide side return check; } -Pokemon::Outcome league_row_pass(const StoryCheckpoint& row, const SlugCounts& start_bag, const LeagueSide& end){ +Pokemon::Outcome league_row_pass(const StoryCheckpoint& row, const LeagueSide& end){ const LeagueFightData* fight = league_row_fight(row); if (fight == nullptr){ return Pokemon::outcome_ok(); } - SlugCounts used; - std::vector spent; - for (const auto& [item, count] : start_bag){ - const int less = count - count_of(end.bag, item); - if (less > 0){ - used[item] = less; - spent.push_back(std::format("{} {}", item, less)); - } - } - if (!league_try_passes(*fight, LeagueTry{.won = true, .used = used})){ - return Pokemon::outcome_failed("league_over_caps", std::format("{} spent {}", fight->name, joined(spent))); - } const LeagueFightData* next = next_league_fight(*fight); if (next == nullptr){ return Pokemon::outcome_ok(); @@ -112,8 +89,8 @@ Pokemon::Outcome league_row_pass(const StoryCheckpoint& row, const SlugCounts& s const LeagueEntryCheck check = league_entry_check(*next, end); if (!check.short_of.empty()){ return Pokemon::outcome_failed("league_next_entry", std::format( - "{} spent {}; after the top-up [{}] before {}, read vs wanted: {}; short of {}", - fight->name, joined(spent), joined(check.topped_up), next->name, joined(check.compared), joined(check.short_of) + "after {} and the top-up [{}] before {}, read vs wanted: {}; short of {}", + fight->name, joined(check.topped_up), next->name, joined(check.compared), joined(check.short_of) )); } return Pokemon::outcome_ok(); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueRows.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueRows.h index 928c2c1062..e9219e1a30 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueRows.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueRows.h @@ -59,14 +59,13 @@ struct LeagueEntryCheck{ LeagueEntryCheck league_entry_check(const LeagueFightData& next, LeagueSide side); // Whether a won try of `row`, its side read as `end`, passes and may be -// saved: for a row whose battle is a League fight, the items spent from -// `start_bag` (the start save's) within the fight's caps, and `end` able -// to start the next fight (league_entry_check()). Any other row passes. -// The failure's detail logs every value compared. -Pokemon::Outcome league_row_pass(const StoryCheckpoint& row, const SlugCounts& start_bag, const LeagueSide& end); - -// The supplies' counts by bag slug. -SlugCounts supplies_counts(const Supplies& supplies); +// saved: for a row whose battle is a League fight, `end` able to start +// the next fight (league_entry_check()). The balance left counts, not +// what the fight spent: the plan's counts for the next fight hold +// whatever the top-up before this one took. The last fight passes on the +// win. Any other row passes. The failure's detail logs every value +// compared. +Pokemon::Outcome league_row_pass(const StoryCheckpoint& row, const LeagueSide& end); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueRows_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueRows_Tests.cpp index 8bb5d8b678..8b8a4dcc54 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueRows_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueRows_Tests.cpp @@ -6,8 +6,8 @@ * through the shared engine (run_segment()) against a fake game: each * try's battle is scripted (won or lost, the items spent), the reload * goes back to the bag of the last save, and a try passes by - * league_row_pass(): the fight's item caps, and the side it leaves able - * to start the next fight after its top-up. And the top-up before a + * league_row_pass(): the side it leaves able to start the next fight + * after its top-up, whatever the fight spent. And the top-up before a * fight (planned_between(), the console's league_top_up): one Super * Potion, two, or a Full Restore, and the Elixir only before the * Champion. @@ -125,7 +125,7 @@ class FakeLeague{ return Pokemon::outcome_ok(); }; hooks.verify_pass = [this](const std::string& id, Pokemon::RunState&){ - return league_row_pass(fight_row(id), m_saved.bag, m_side); + return league_row_pass(fight_row(id), m_side); }; hooks.log = [this](const std::string& line){ log += std::format("\n {}", line); @@ -279,22 +279,29 @@ class Test_LeagueRows : public UnitTest{ LeagueRunCase m_case; }; -// Each fight row plays its 10 phase 1 tries and one phase 2 try when -// every try passes. -constexpr int TRIES_ALL_PASS = 11; +// When every try passes, each fight row plays phase 1 to its min_pass +// (Lorelei 5, Bruno 1, Agatha 5, Lance 2, the Champion 2) and one phase 2 +// try. +constexpr int LORELEI_ALL_PASS = 6; +constexpr int BRUNO_ALL_PASS = 2; +constexpr int AGATHA_ALL_PASS = 6; +constexpr int LANCE_ALL_PASS = 3; +constexpr int CHAMPION_ALL_PASS = 3; +constexpr int FROM_BRUNO_ALL_PASS = BRUNO_ALL_PASS + AGATHA_ALL_PASS + LANCE_ALL_PASS + CHAMPION_ALL_PASS; +constexpr int ALL_PASS = LORELEI_ALL_PASS + FROM_BRUNO_ALL_PASS; const LeagueRunCase LEAGUE_RUN_CASES[] = { { .name = "every try a win within the caps: 20.01 to 20.05 each saved once", .input = {}, - .expectation = {.completed = true, .fights = 5 * TRIES_ALL_PASS, .saves = 5, .full_restores_saved = 8}, + .expectation = {.completed = true, .fights = ALL_PASS, .saves = 5, .full_restores_saved = 8}, }, { - .name = "Bruno's first phase 2 win spends 3 Full Restores, over his cap of 2: not saved; the next win is", + .name = "Bruno's first win spends 4 of 8 Full Restores, leaving 4, under Agatha's 5: no pass; the next win passes, and phase 2's is saved", .input = {.tries = { - {"20.02", joined(joined(repeated(10, win()), {win({{"full-restore", 3}})}), {win({{"full-restore", 1}})})}, + {"20.02", {win({{"full-restore", 4}}), win({{"full-restore", 1}})}}, }}, - .expectation = {.completed = true, .fights = 5 * TRIES_ALL_PASS + 1, .saves = 5, .full_restores_saved = 7}, + .expectation = {.completed = true, .fights = ALL_PASS + 1, .saves = 5, .full_restores_saved = 7}, }, { .name = "Lorelei passes 4 of phase 1's 10, under min_pass 5: the run stops before Bruno, nothing saved", @@ -302,9 +309,9 @@ const LeagueRunCase LEAGUE_RUN_CASES[] = { .expectation = {.failure = "verify_below_min_pass", .failed_row = "20.01", .fights = 10, .saves = 0, .full_restores_saved = 8}, }, { - .name = "Lorelei wins with 2 Full Restores, over her cap of 1, every time: phase 1 passes none, the run stops", - .input = {.tries = {{"20.01", {win({{"full-restore", 2}})}}}}, - .expectation = {.failure = "verify_below_min_pass", .failed_row = "20.01", .fights = 10, .saves = 0, .full_restores_saved = 8}, + .name = "Lorelei wins leaving 5 Full Restores, under Bruno's 6, every time: min_pass out of reach after 6 tries, the run stops", + .input = {.tries = {{"20.01", {win({{"full-restore", 3}})}}}}, + .expectation = {.failure = "verify_below_min_pass", .failed_row = "20.01", .fights = 6, .saves = 0, .full_restores_saved = 8}, }, { .name = "the bag at the Indigo Plateau Center short of a Full Restore: 20.01 stops before the League's door", @@ -324,12 +331,17 @@ const LeagueRunCase LEAGUE_RUN_CASES[] = { }, .tries = {{"20.04", {win({{"full-restore", 2}}, /*hp_left=*/45)}}}, }, - .expectation = {.failure = "verify_below_min_pass", .failed_row = "20.04", .fights = 10, .saves = 0, .full_restores_saved = 4}, + .expectation = {.failure = "verify_below_min_pass", .failed_row = "20.04", .fights = 9, .saves = 0, .full_restores_saved = 4}, + }, + { + .name = "Bruno wins at 45 HP, so the top-up before Agatha takes a Full Restore, more than her plan's spend of none: her wins still leave Lance's 4, saved", + .input = {.first = "20.02", .tries = {{"20.02", {win(/*used=*/{}, /*hp_left=*/45)}}}}, + .expectation = {.completed = true, .fights = FROM_BRUNO_ALL_PASS, .saves = 4, .full_restores_saved = 7}, }, { .name = "Agatha wins leaving Surf at 2, under Lance's 3: never saved", .input = {.tries = {{"20.03", {win(/*used=*/{}, MAX_HP, /*surf_spent=*/13)}}}}, - .expectation = {.failure = "verify_below_min_pass", .failed_row = "20.03", .fights = 2 * TRIES_ALL_PASS + 10, .saves = 2, .full_restores_saved = 8}, + .expectation = {.failure = "verify_below_min_pass", .failed_row = "20.03", .fights = LORELEI_ALL_PASS + BRUNO_ALL_PASS + 6, .saves = 2, .full_restores_saved = 8}, }, }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueSteps.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueSteps.cpp index facadf9044..973f2bafde 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueSteps.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueSteps.cpp @@ -177,6 +177,11 @@ Outcome league_top_up(StoryEnv& env, const StoryStep& step){ // save's bag, which the save check counts the try's spending from. set_from_bag(env.saved_supplies, rows); + // The lead's PP off its summary every try: after a reload the story's + // moves can be a later try's (console B, 2026-11-17: tries 2 to 4 + // before the Champion skipped the Elixir with Blizzard at 0, counting + // try 1's PP after its Elixir). + read_lead_moves(env, step_start_menu(step.args)); if (!know_lead_hp(env, step, max_hp, full_if_unknown != nullptr && *full_if_unknown)){ return Pokemon::outcome_failed("precondition_failed", std::format("{}: the lead's HP is unknown before {}", step.name, fight->name)); } @@ -222,11 +227,19 @@ int row_max_hp(const StoryCheckpoint& row){ // The lead's HP and moves' PP off its summary into `side`; the failure // (VERIFY_UNREAD) if a read failed or is unsure. std::optional read_lead(StoryEnv& env, const StoryCheckpoint& row, const LeagueFightData& fight, LeagueSide& side){ - const int max_hp = env.supplies.lead.stats && env.supplies.lead.stats->hp > 0 ? env.supplies.lead.stats->hp : row_max_hp(row); const std::optional summary = read_party_summary(env.console, env.context, row.menu, env.language, /*slot=*/0); if (!summary){ return unread(env, row.id, "the lead's summary wasn't read"); } + // The maximum from this same summary first: a fight's level-up raises + // it (console B, 2026-11-17: Lv55 after Lorelei, 145/174 read against + // the 171 known before the fight). + int max_hp = row_max_hp(row); + if (summary->stats && summary->stats->hp > 0){ + max_hp = summary->stats->hp; + }else if (env.supplies.lead.stats && env.supplies.lead.stats->hp > 0){ + max_hp = env.supplies.lead.stats->hp; + } const std::optional hp = summary_current_hp(summary->hp_digits, max_hp); if (!hp){ return unread(env, row.id, std::format("the lead's HP wasn't read (\"{}\" against max {})", summary->hp_digits, max_hp)); @@ -286,11 +299,16 @@ void log_save_read(StoryEnv& env, const StoryCheckpoint& row, const LeagueFightD } // namespace -Outcome check_league_save(StoryEnv& env, const StoryCheckpoint& row, const Supplies& start){ +Outcome check_league_save(StoryEnv& env, const StoryCheckpoint& row){ const LeagueFightData* fight = league_row_fight(row); if (fight == nullptr){ return Pokemon::outcome_ok(); } + // The last fight passes on the win: the Hall of Fame after it saved the + // game and runs into the credits, with no menu to read. + if (next_league_fight(*fight) == nullptr){ + return Pokemon::outcome_ok(); + } Supplies now = env.supplies; if (std::optional failed = read_bag(env, row, now)){ return *failed; @@ -300,7 +318,7 @@ Outcome check_league_save(StoryEnv& env, const StoryCheckpoint& row, const Suppl return *failed; } log_save_read(env, row, *fight, side); - const Outcome passed = league_row_pass(row, supplies_counts(start), side); + const Outcome passed = league_row_pass(row, side); if (passed.succeeded()){ // The story's counts are the console's from here. env.supplies = std::move(now); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueSteps.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueSteps.h index f4ded9cef4..527b4217ac 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueSteps.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueSteps.h @@ -44,11 +44,11 @@ inline const std::string LEAGUE_TOP_UP_ACTION = "league_top_up"; // be saved (league_row_pass()), from our side as read off the console // now, not the story's own counts: the Items pocket, the lead's HP // (summary_current_hp()) and its moves' PP (the summary's KNOWN MOVES). -// `start` is the start save's supplies. Every value compared is logged, +// Every value compared is logged, // wanted against read. A read that fails or is unsure fails // "postcondition_unread" (the loop stops: never a save on a guess), with // the screen kept in DebugDumps/AutoStory/league. -Pokemon::Outcome check_league_save(StoryEnv& env, const StoryCheckpoint& row, const Supplies& start); +Pokemon::Outcome check_league_save(StoryEnv& env, const StoryCheckpoint& row); // LEAGUE_TOP_UP_ACTION's spec. std::vector league_action_specs(); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeague_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeague_Tests.cpp index 618a9ea6d9..514b9f7cf9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeague_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeague_Tests.cpp @@ -25,11 +25,14 @@ #include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h" +#include "PokemonFRLG_AutoStoryBattleDecisions.h" #include "PokemonFRLG_AutoStoryBattlePolicy.h" #include "PokemonFRLG_AutoStoryEvents.h" +#include "PokemonFRLG_AutoStoryHallOfFame.h" #include "PokemonFRLG_AutoStoryItemsPocket.h" #include "PokemonFRLG_AutoStoryLeagueFightData.h" #include "PokemonFRLG_AutoStoryLeagueSteps.h" +#include "PokemonFRLG_AutoStoryPcTransfer.h" #include "PokemonFRLG_AutoStoryPoses.h" #include "PokemonFRLG_AutoStoryRowSim.h" #include "PokemonFRLG_AutoStoryRows20_Tests.h" @@ -333,6 +336,49 @@ size_t step_index(const StoryCheckpoint& row, std::string_view name){ return index; } +// 20.01a: the walk ends below the PC (17, 9) facing it, and the Magikarp +// is withdrawn there by its species, to ride along, before the Mart. +UnitTestResult test_magikarp_from_box(){ + const StoryCheckpoint& row = league_row("20.01a"); + StoryPose at = *story_pose(Pokemon::pose_of(*row.start)); + OverworldSim sim(story_maps(), MapPosition{at.map, at.tile}, *at.facing); + if (const std::string failure = walk_league_row(row, "magikarp_from_box", sim, at); !failure.empty()){ + return failure; + } + const MapPosition below_pc{"IndigoPlateau_PokemonCenter_1F", {17, 10}}; + if (!(sim.state().position == below_pc) || sim.state().facing != Direction::NORTH){ + return std::format("20.01a: ended on {}, not below the PC {} facing north.", to_string(sim.state().position), to_string(below_pc)); + } + const size_t withdraw = step_index(row, "magikarp_from_box"); + if (withdraw >= step_index(row, "buy_full_restores") || row.steps[withdraw].action != PC_ACTION){ + return "20.01a: expected the pc step magikarp_from_box before the Mart."; + } + const std::optional transfer = pc_transfer_of(row.steps[withdraw].args); + if (!transfer || transfer->action != PcAction::WITHDRAW || transfer->box != 0 + || transfer->species != std::optional("magikarp") || !transfer->protect + ){ + return "20.01a magikarp_from_box: expected a protected withdrawal of the magikarp from BOX 1."; + } + return true; +} + +// 20.01 to 20.05: with the lead down, the party list's sends go through +// the team and never reach the Magikarp in slot 6, however many times +// the list opens. +UnitTestResult test_magikarp_never_sent(){ + for (std::string_view id : {"20.01", "20.02", "20.03", "20.04", "20.05"}){ + Supplies supplies = league_row(id).supplies; + BattleDecisions decisions(supplies, BattleKind::TRAINER); + for (size_t c = 0; c < 6; c++){ + const std::optional slot = decisions.next_member_slot(); + if (slot == size_t(5)){ + return std::format("{}: the Magikarp's slot 6 was sent in.", id); + } + } + } + return true; +} + // The League's fight rows, in order: each starts where the one before // ends; its first steps are the level check and the top-up for its own // fight (league_top_up), before any walk; its battle is that fight; and @@ -380,6 +426,17 @@ UnitTestResult test_league_fight_rows(){ return true; } +// 20.05: the Hall of Fame's screenshots come right after the Champion. +UnitTestResult test_hall_of_fame_after_champion(){ + const StoryCheckpoint& row = league_row("20.05"); + const size_t champion = step_index(row, "champion"); + const size_t hall = step_index(row, "hall_of_fame"); + if (hall != champion + 1 || row.steps[hall].action != HALL_OF_FAME_ACTION){ + return "20.05: expected the hall_of_fame action right after the Champion's battle."; + } + return true; +} + const AutoStoryLeagueCase AUTO_STORY_LEAGUE_CASES[] = { { .name = "20.05 the Champion's Venusaur: Solar Beam is a KO risk only charged or in sun", @@ -391,6 +448,21 @@ const AutoStoryLeagueCase AUTO_STORY_LEAGUE_CASES[] = { .input = {.body = test_indigo_mart_counter}, .expectation = {.description = "the counter (2, 7), where 20.01 starts"}, }, + { + .name = "20.01a the Magikarp from BOX 1 at the PC", + .input = {.body = test_magikarp_from_box}, + .expectation = {.description = "below the PC (17, 10) facing north; a protected withdrawal of the magikarp before the Mart"}, + }, + { + .name = "20.01 to 20.05 the Magikarp is never sent into a battle", + .input = {.body = test_magikarp_never_sent}, + .expectation = {.description = "no send of party slot 6 with the lead fainted"}, + }, + { + .name = "20.05 the Hall of Fame's screenshots after the Champion", + .input = {.body = test_hall_of_fame_after_champion}, + .expectation = {.description = "the hall_of_fame action right after the champion battle"}, + }, { .name = "20.01 the League's door, into Lorelei's room", .input = {.body = []{ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcDialog.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcDialog.h index e86f527eae..4e6ab075a5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcDialog.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcDialog.h @@ -86,7 +86,13 @@ class PcStorageDialog{ PcDecision on_mon_menu(const PcView& view); PcDecision on_box_chooser(const PcView& view); + // The box slot the hand goes to next: the transfer's, or for a + // species the next cell to read. + size_t target_slot() const; + PcTransfer m_transfer; + // A species withdrawal: the cell read next, row by row. + size_t m_search_slot = 0; bool m_booted = false; // The overworld read since A at the PC, while the PC boots. size_t m_booting_reads = 0; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcRun.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcRun.h index e6c1f8cc35..1ab241096e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcRun.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcRun.h @@ -45,8 +45,8 @@ class PcStorageActions{ // How a PC transfer sequence concluded. enum class PcEnd{ DONE, - // A DEPOSIT with PcMissing::SKIP whose slot the party hasn't got: the - // PC wasn't touched. + // A DEPOSIT with PcMissing::SKIP whose slot the party hasn't got, or a + // WITHDRAW of a species the party has: the PC wasn't touched. SKIPPED, // pc_transfer_error() refused it before the PC was touched. REFUSED, @@ -56,6 +56,9 @@ enum class PcEnd{ struct PcResult{ PcEnd end = PcEnd::FAILED; std::string detail = {}; + // A WITHDRAW that is DONE: the party slot (from 0) it joined, the + // first one free; SKIPPED: the slot that already holds its species. + std::optional party_slot = std::nullopt; }; // Reads after this many presses without the overworld back: a failure. diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage.cpp index fdaea2a94b..8426b1b230 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage.cpp @@ -7,10 +7,13 @@ #include #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_BoxCellReader.h" #include "VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h" #include "PokemonFRLG_AutoStoryActions.h" #include "PokemonFRLG_AutoStoryGameArgs.h" #include "PokemonFRLG_AutoStoryPcStorage.h" +#include "PokemonFRLG_AutoStoryStartMenu.h" +#include "PokemonFRLG_AutoStorySummary.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -39,7 +42,10 @@ std::string pc_transfer_text(const PcTransfer& transfer){ transfer.if_missing == PcMissing::SKIP ? " if_present" : "" ); } - return std::format("withdraw box={} slot={}", transfer.box + 1, transfer.box_slot + 1); + const std::string what = transfer.species + ? std::format("species={}", *transfer.species) + : std::format("slot={}", transfer.box_slot + 1); + return std::format("withdraw box={} {}{}", transfer.box + 1, what, transfer.protect ? " protect" : ""); } // Whether `view` is a PC screen to act on, not one changing: a fade (a @@ -66,12 +72,18 @@ Pokemon::StoryActionArgs pc_args(const PcTransfer& transfer){ {"if_present", transfer.if_missing == PcMissing::SKIP}, }; } - return { + Pokemon::StoryActionArgs args = { {"do", std::string("withdraw")}, - {"slot", int64_t(transfer.box_slot + 1)}, {"box", int64_t(transfer.box + 1)}, {"if_present", false}, + {"protect", transfer.protect}, }; + if (transfer.species){ + args["species"] = *transfer.species; + }else{ + args["slot"] = int64_t(transfer.box_slot + 1); + } + return args; } std::optional pc_transfer_of(const Pokemon::StoryActionArgs& args){ @@ -93,11 +105,29 @@ std::optional pc_transfer_of(const Pokemon::StoryActionArgs& args){ } return transfer; } - const std::optional slot = counted_arg(args, "slot", BOX_SLOTS); - if (*action != "withdraw" || !slot){ + if (*action != "withdraw"){ return std::nullopt; } transfer.action = PcAction::WITHDRAW; + const bool* protect = game_arg(args, "protect"); + transfer.protect = protect != nullptr && *protect; + const std::string* species = game_arg(args, "species"); + const bool has_slot = args.contains("slot"); + // A slot or a species, not both. + if ((species != nullptr) == has_slot){ + return std::nullopt; + } + if (species != nullptr){ + if (species->empty()){ + return std::nullopt; + } + transfer.species = *species; + return transfer; + } + const std::optional slot = counted_arg(args, "slot", BOX_SLOTS); + if (!slot){ + return std::nullopt; + } transfer.box_slot = *slot; return transfer; } @@ -254,10 +284,23 @@ PcDecision PcStorageDialog::on_box_grid(const PcView& view){ if (!view.cursor || *view.cursor >= BOX_SLOTS){ return {PcPress::FAIL, "the hand wasn't read on the box"}; } + // A species: each cell the hand reaches is read, its Pokemon taken if + // it is the one, else the hand goes on to the next cell. + if (m_transfer.species && *view.cursor == m_search_slot){ + if (view.occupied != false && !view.species){ + return {PcPress::FAIL, std::format("box {} slot {}: the species wasn't read", m_transfer.box + 1, m_search_slot + 1)}; + } + if (view.occupied != false && *view.species == *m_transfer.species){ + return {PcPress::A}; + } + if (++m_search_slot >= BOX_SLOTS){ + return {PcPress::FAIL, std::format("no {} in box {}", *m_transfer.species, m_transfer.box + 1)}; + } + } const size_t row = *view.cursor / BOX_COLUMNS; const size_t column = *view.cursor % BOX_COLUMNS; - const size_t target_row = m_transfer.box_slot / BOX_COLUMNS; - const size_t target_column = m_transfer.box_slot % BOX_COLUMNS; + const size_t target_row = target_slot() / BOX_COLUMNS; + const size_t target_column = target_slot() % BOX_COLUMNS; if (row != target_row){ return {row < target_row ? PcPress::DOWN : PcPress::UP}; } @@ -270,6 +313,10 @@ PcDecision PcStorageDialog::on_box_grid(const PcView& view){ return {PcPress::A}; } +size_t PcStorageDialog::target_slot() const{ + return m_transfer.species ? m_search_slot : m_transfer.box_slot; +} + PcDecision PcStorageDialog::on_mon_menu(const PcView& view){ // RELEASE is on this menu: the cursor is never moved on it. if (m_transferred){ @@ -318,6 +365,17 @@ PcResult run_pc_transfer(Logger& logger, PcStorageActions& actions, const PcTran logger.log(std::format("PC: skipped \"{}\": the party has {} Pokemon.", name, party.size())); return {PcEnd::SKIPPED, ""}; } + // A species the party already has was withdrawn on an earlier try + // (a retry from a save made after it, console B 2026-11-17: 20.01a's + // Magikarp already in slot 6): done, in the slot it holds. + if (transfer.action == PcAction::WITHDRAW && transfer.species){ + for (size_t c = 0; c < party.size(); c++){ + if (party[c].species == *transfer.species){ + logger.log(std::format("PC: skipped \"{}\": party slot {} has it.", name, c + 1)); + return {.end = PcEnd::SKIPPED, .detail = "", .party_slot = c}; + } + } + } const std::string refused = pc_transfer_error(transfer, party); if (!refused.empty()){ logger.log("PC: refused \"" + name + "\": " + refused + ".", COLOR_RED); @@ -329,6 +387,10 @@ PcResult run_pc_transfer(Logger& logger, PcStorageActions& actions, const PcTran const PcDecision decision = dialog.next(view); if (decision.press == PcPress::DONE){ logger.log("PC: \"" + name + "\" done."); + if (transfer.action == PcAction::WITHDRAW){ + // A withdrawal joins the party at its end. + return {.end = PcEnd::DONE, .detail = "", .party_slot = party.size()}; + } return {PcEnd::DONE, ""}; } if (decision.press == PcPress::FAIL){ @@ -356,13 +418,26 @@ size_t read_party_size(ConsoleHandle& console, ProControllerContext& context){ } -ConsolePcStorageActions::ConsolePcStorageActions(ConsoleHandle& console, ProControllerContext& context) +ConsolePcStorageActions::ConsolePcStorageActions(ConsoleHandle& console, ProControllerContext& context, Language language) : m_console(console) , m_context(context) + , m_language(language) {} std::vector ConsolePcStorageActions::read_party(){ - return std::vector(read_party_size(m_console, m_context)); + std::vector party(read_party_size(m_console, m_context)); + // A full party: its last slot's species, from its summary, so that a + // withdrawal done on an earlier try is seen as done (a withdrawal joins + // at the end). The PC is reached only with the full START menu. + if (party.size() == PARTY_SIZE){ + const std::optional last = read_party_summary( + m_console, m_context, StartMenu::FULL, m_language, /*slot=*/PARTY_SIZE - 1 + ); + if (last){ + party.back().species = last->species; + } + } + return party; } PcView ConsolePcStorageActions::read_view(){ @@ -371,7 +446,13 @@ PcView ConsolePcStorageActions::read_view(){ if (!screen){ return {}; } - const PcView view = read_pc_screen(m_console, screen); + PcView view = read_pc_screen(m_console, screen); + if (view.screen == PcScreen::BOX_GRID && view.occupied != false){ + const std::string species = BoxCellReader().read_name(m_console, m_language, screen); + if (!species.empty()){ + view.species = species; + } + } if (view.screen != PcScreen::OVERWORLD){ return view; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage.h index 9f8c54906a..b56b806139 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage.h @@ -49,6 +49,7 @@ #include #include "Common/Cpp/Logging/AbstractLogger.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/Language.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" @@ -76,12 +77,13 @@ size_t read_party_size(ConsoleHandle& console, ProControllerContext& context); // The console's PC steps. Party members are read by count only: none is // taken as fainted, since the story heals before it uses the PC. Screens -// are read with read_pc_screen(); the overworld only once a second frame, +// are read with read_pc_screen(), a box cell's species with BoxCellReader +// in `language`; the overworld only once a second frame, // a moment later, shows it too, since a frame between two menus can look // like it. class ConsolePcStorageActions : public PcStorageActions{ public: - ConsolePcStorageActions(ConsoleHandle& console, ProControllerContext& context); + ConsolePcStorageActions(ConsoleHandle& console, ProControllerContext& context, Language language); virtual std::vector read_party() override; virtual PcView read_view() override; @@ -90,6 +92,7 @@ class ConsolePcStorageActions : public PcStorageActions{ private: ConsoleHandle& m_console; ProControllerContext& m_context; + Language m_language; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage_Tests.cpp index 6e96af70cf..db73555c5f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage_Tests.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include "Common/Cpp/Exceptions.h" @@ -78,6 +79,9 @@ class FakePc : public PcStorageActions{ if (m_screen == PcScreen::BOX_GRID){ view.box = current_box; view.occupied = !boxes[current_box][m_cursor].empty(); + if (*view.occupied){ + view.species = boxes[current_box][m_cursor]; + } } if (m_screen == PcScreen::BOX_CHOOSER){ view.box = m_chooser_box; @@ -362,6 +366,8 @@ struct PcCase{ std::string box2_after = {}; // The presses, when the case checks them. std::string presses = {}; + // The party slot a withdrawal joined, when the case checks it. + std::optional joined_slot = std::nullopt; }; const std::vector PC_CASES = { { @@ -391,6 +397,37 @@ const std::vector PC_CASES = { .party_after = "squirtle magikarp", .box1_after = "pidgey spearow ekans", }, + { + // 20.01a: the shiny Magikarp, wherever it is in BOX 1, joins a + // party of five as its sixth. + .name = "withdraw the Magikarp by its species, past a Pidgey and an empty cell", + .transfer = {.action = PcAction::WITHDRAW, .box = 0, .species = "magikarp", .protect = true}, + .party = {{.species = "blastoise"}, {.species = "oddish"}, {.species = "pidgey"}, {.species = "vaporeon"}, {.species = "lapras"}}, + .box1 = {"pidgey", "", "magikarp"}, + .end = PcEnd::DONE, + .party_after = "blastoise oddish pidgey vaporeon lapras magikarp", + .box1_after = "pidgey", + .joined_slot = 5, + }, + { + .name = "withdraw a species the box hasn't got", + .transfer = {.action = PcAction::WITHDRAW, .box = 0, .species = "magikarp"}, + .party = {{.species = "blastoise"}}, + .box1 = {"pidgey"}, + .end = PcEnd::FAILED, + .party_after = "blastoise", + .box1_after = "pidgey", + }, + { + .name = "refuse to withdraw a species into a full party", + .transfer = {.action = PcAction::WITHDRAW, .box = 0, .species = "magikarp", .protect = true}, + .party = {{.species = "a"}, {.species = "b"}, {.species = "c"}, {.species = "d"}, {.species = "e"}, {.species = "f"}}, + .box1 = {"magikarp"}, + .end = PcEnd::REFUSED, + .party_after = "a b c d e f", + .box1_after = "magikarp", + .presses = "", + }, { .name = "withdraw with the box up 2.5 s after A: 2 s of fade, 0.5 s before the hand (console B, 2026-11-16)", .transfer = {.action = PcAction::WITHDRAW, .box = 0, .box_slot = 0}, @@ -482,6 +519,15 @@ const std::vector PC_CASES = { .box1_after = "magikarp", .presses = "", }, + { + .name = "a species the party already has: skipped as done, its slot kept", + .transfer = {.action = PcAction::WITHDRAW, .box = 0, .species = "magikarp", .protect = true}, + .party = {{.species = "a"}, {.species = "b"}, {.species = "c"}, {.species = "d"}, {.species = "e"}, {.species = "magikarp"}}, + .end = PcEnd::SKIPPED, + .party_after = "a b c d e magikarp", + .presses = "", + .joined_slot = 5, + }, { .name = "a storage menu whose cursor isn't read: stop there", .transfer = {.action = PcAction::DEPOSIT, .party_slot = 1}, @@ -526,8 +572,9 @@ UnitTestResult test_pc_transfers(){ const bool presses_ok = item.presses.empty() && item.end != PcEnd::REFUSED ? true : joined(pc.presses) == item.presses; + const bool joined_ok = !item.joined_slot || result.party_slot == item.joined_slot; if (result.end != item.end || party_species(pc.party) != item.party_after - || joined(box1) != item.box1_after || joined(box2) != item.box2_after || !presses_ok + || joined(box1) != item.box1_after || joined(box2) != item.box2_after || !presses_ok || !joined_ok ){ return std::format( "{}: expected end {} with party \"{}\", box 1 \"{}\", box 2 \"{}\"; got end {} ({}) with party \"{}\", " @@ -599,13 +646,15 @@ UnitTestResult test_pc_args(){ {.action = PcAction::DEPOSIT, .party_slot = 1, .box = 0}, {.action = PcAction::WITHDRAW, .box = 13, .box_slot = 29}, {.action = PcAction::DEPOSIT, .party_slot = 1, .box = 0, .if_missing = PcMissing::SKIP}, + {.action = PcAction::WITHDRAW, .box = 0, .species = "magikarp", .protect = true}, }; for (size_t c = 0; c < transfers.size(); c++){ const PcTransfer& transfer = transfers[c]; const std::optional back = pc_transfer_of(pc_args(transfer)); if (!back || back->action != transfer.action || back->box != transfer.box || back->party_slot != transfer.party_slot || back->box_slot != transfer.box_slot - || back->if_missing != transfer.if_missing + || back->if_missing != transfer.if_missing || back->species != transfer.species + || back->protect != transfer.protect ){ return std::format("Transfer {} didn't read back from its args.", c + 1); } @@ -620,6 +669,9 @@ UnitTestResult test_pc_args(){ args("deposit", 2, 15), args("withdraw", 31, 1), args("release", 1, 1), + // A withdrawal names a slot or a species, not both. + Pokemon::StoryActionArgs{{"do", std::string("withdraw")}, {"box", int64_t(1)}}, + Pokemon::StoryActionArgs{{"do", std::string("withdraw")}, {"box", int64_t(1)}, {"slot", int64_t(1)}, {"species", std::string("magikarp")}}, }; for (const Pokemon::StoryActionArgs& bad : bad_args){ if (pc_transfer_of(bad)){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcTransfer.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcTransfer.h index e379545317..21cf5e4411 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcTransfer.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcTransfer.h @@ -51,14 +51,23 @@ struct PcTransfer{ size_t box = 0; // WITHDRAW: the box slot, row by row, six to a row. size_t box_slot = 0; + // WITHDRAW: the species to take, found by reading the box's cells + // row by row from the top left instead of going to `box_slot`. + std::optional species = std::nullopt; + // WITHDRAW: the Pokemon joins the party only to ride along: its party + // slot goes into Supplies::protected_slots, never sent into a battle. + bool protect = false; PcMissing if_missing = PcMissing::REFUSE; }; // The GAME action of a PC step, slots and boxes counted from 1 as the game -// shows them: do WORD (required), "deposit" or "withdraw"; slot INTEGER -// (required), the party slot (1-6) of a deposit or the box slot (1-30) -// of a withdrawal; box INTEGER (required, 1-14); if_present BOOLEAN -// (false): a deposit skipped when the party hasn't got the slot. +// shows them: do WORD (required), "deposit" or "withdraw"; slot INTEGER, +// the party slot (1-6) of a deposit (required) or the box slot (1-30) of +// a withdrawal; species WORD, a withdrawal's species, found in the box +// instead of a slot (a withdrawal names one of the two); box INTEGER +// (required, 1-14); if_present BOOLEAN (false): a deposit skipped when the +// party hasn't got the slot; protect BOOLEAN (false): a withdrawal that +// rides along, never sent into a battle. inline const std::string PC_ACTION = "pc"; // `transfer` as its action's args. Pokemon::StoryActionArgs pc_args(const PcTransfer& transfer); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden20.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden20.h index 8f2ee799bc..2fa495c12a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden20.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden20.h @@ -19,8 +19,12 @@ namespace PokemonFRLG{ // The stored plan lines of 20.01a and after. inline constexpr std::string_view FRLG_PLAN_GOLDEN_20[] = { - "20.01a indigo_mart_clerk from IndigoPlateau_PokemonCenter_1F (13, 12) facing north to IndigoPlateau_PokemonCenter_1F (2, 7) facing west", - "20.01a walk west 9 -> IndigoPlateau_PokemonCenter_1F (4, 12) [walk only]", + "20.01a indigo_pc_front from IndigoPlateau_PokemonCenter_1F (13, 12) facing north to IndigoPlateau_PokemonCenter_1F (17, 10) facing north", + "20.01a walk east 4 -> IndigoPlateau_PokemonCenter_1F (17, 12) [walk only]", + "20.01a walk north 2 (to wall) -> IndigoPlateau_PokemonCenter_1F (17, 10) [walk only]", + "20.01a indigo_mart_clerk from IndigoPlateau_PokemonCenter_1F (17, 10) facing north to IndigoPlateau_PokemonCenter_1F (2, 7) facing west", + "20.01a walk south 2 -> IndigoPlateau_PokemonCenter_1F (17, 12) [walk only]", + "20.01a walk west 13 -> IndigoPlateau_PokemonCenter_1F (4, 12) [walk only]", "20.01a walk north 5 -> IndigoPlateau_PokemonCenter_1F (4, 7) [walk only]", "20.01a walk west 2 (to wall) -> IndigoPlateau_PokemonCenter_1F (2, 7) [walk only]", "20.01 below_league_door from IndigoPlateau_PokemonCenter_1F (2, 7) facing west to IndigoPlateau_PokemonCenter_1F (4, 2) facing north", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.cpp index c381d09211..178ed8fa52 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.cpp @@ -294,6 +294,14 @@ std::string turn_log(const LeagueFightData& fight, const PlannedState& state, co ); } +std::vector party_species(const LeagueTrainerData& trainer){ + std::vector species; + for (const LeagueFoeData& foe : trainer.party){ + species.push_back(foe.species); + } + return species; +} + } // namespace @@ -329,25 +337,20 @@ PlannedState planned_state(const PlannedInputs& inputs, const LeagueFoesData& da PlannedBattle::PlannedBattle(const LeagueFightData& fight, const LeagueTrainerData& trainer) : m_fight(&fight) , m_trainer(&trainer) + , m_foes(party_species(trainer)) {} PlannedFoeTrack PlannedBattle::foe_out(const Supplies& supplies, const PolicyView& view){ const std::vector& party = m_trainer->party; - size_t slot = std::min(view.foe_index, party.empty() ? 0 : party.size() - 1); - for (size_t index = slot; index < party.size() && !supplies.foe_out.empty(); index++){ - if (party[index].species == supplies.foe_out){ - slot = index; - break; - } - } - const LeagueFoeData foe = party.empty() ? LeagueFoeData{} : party[slot]; + const size_t slot = m_foes.out(supplies.foe_out); + const LeagueFoeData foe = slot < party.size() ? party[slot] : LeagueFoeData{}; m_track.foe_out(slot, foe.ability); const auto carried = m_trainer->items.find("full-restore"); return PlannedFoeTrack{ .foe = foe, .slot = slot, .fill = view.foe_fill, - .party_alive = static_cast(party.size() - slot), + .party_alive = m_foes.alive(), .full_restores_carried = carried == m_trainer->items.end() ? 0 : carried->second, }; } @@ -361,8 +364,7 @@ PlannedBattle::Turn PlannedBattle::choose(const Supplies& supplies, const Policy .foe = foe_out(supplies, view), }; // The foe out last turn fell. - const std::optional fallen = m_slot && *m_slot < inputs.foe.slot ? m_slot : std::nullopt; - m_slot = inputs.foe.slot; + const std::optional fallen = m_foes.fell(); const int speed = m_track.state().own.speed; if (fallen && league_early_reset(*m_fight, *fallen, speed, m_used)){ return Turn{ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.h index 0503d3b936..00bc6eb32a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.h @@ -91,9 +91,10 @@ class PlannedBattle{ const LeagueTrainerData* m_trainer; BattleTrack m_track; PlannedStreak m_streak; - // The items used in the fight, and the foe's party slot last turn. + // The trainer's party as it came out. + FoeParty m_foes; + // The items used in the fight. SlugCounts m_used; - std::optional m_slot; }; // The League fight `trainer` (a battle step's "trainer") is fought by diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle_Tests.cpp index 5b40611eaf..b23dec3620 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle_Tests.cpp @@ -8,6 +8,8 @@ * */ +#include +#include #include #include #include @@ -17,6 +19,7 @@ #include "Common/Cpp/TestRunners/UnitTest.h" #include "PokemonFRLG_AutoStoryLeagueFoes.h" #include "PokemonFRLG_AutoStoryPlannedBattle.h" +#include "PokemonFRLG_AutoStorySupplies.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -326,10 +329,193 @@ const StateCase STATE_CASES[] = { }, }; + +// One choice of a League fight: the foe read out, the foe index the +// battle loop passed, our HP and the texts since the last choice. +struct SendTurn{ + std::string foe_out = {}; + size_t foe_index = 0; + int hp = 171; + std::vector texts = {}; + // X items used after this choice. + std::vector used = {}; +}; +struct SendsInput{ + std::string trainer = {}; + std::vector turns = {}; +}; +// A choice's log line: the foe, its party slot, the party alive and our +// danger; or the plan's early reset. +struct SendSeen{ + std::string species = {}; + size_t slot = 0; + int left = 0; + bool danger = false; + bool early_reset = false; + + bool operator==(const SendSeen&) const = default; +}; +struct SendsExpectation{ + std::vector seen = {}; +}; +struct SendsCase{ + std::string name; + SendsInput input; + SendsExpectation expectation; +}; + +// The turn log's " / (slot , left), D ". +SendSeen seen_of(const PlannedBattle::Turn& turn){ + if (turn.early_reset){ + return SendSeen{.early_reset = true}; + } + SendSeen seen; + const std::string& log = turn.log; + const size_t at = log.find(" (slot "); + const size_t hp = log.rfind(' ', at == std::string::npos ? 0 : at - 1); + const size_t name = log.rfind(", ", hp == std::string::npos ? 0 : hp); + const size_t danger = log.find(", D "); + if (at == std::string::npos || hp == std::string::npos || name == std::string::npos || danger == std::string::npos){ + return seen; + } + seen.species = log.substr(name + 2, hp - name - 2); + int slot = 0; + if (std::sscanf(log.c_str() + at, " (slot %d, %d left)", &slot, &seen.left) != 2){ + return SendSeen{}; + } + seen.slot = static_cast(slot); + seen.danger = std::atoi(log.c_str() + danger + 4) > 0; + return seen; +} + +std::string describe(const std::vector& seen){ + std::string text; + for (const SendSeen& turn : seen){ + text += turn.early_reset ? std::string("[early reset] ") + : std::format("[{} slot {}, {} left, danger {}] ", turn.species, turn.slot, turn.left, turn.danger); + } + return text; +} + +class Test_PlannedSends : public UnitTest{ +public: + explicit Test_PlannedSends(SendsCase test_case) + : UnitTest(std::format("PokemonFRLG::AutoStoryPlannedBattle - sends: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::optional battle = planned_battle(m_case.input.trainer); + if (!battle){ + return std::format("No League fight {}.", m_case.input.trainer); + } + Supplies supplies; + supplies.lead = blastoise(); + supplies.other_counts = {{"full-restore", 8}, {"x-special", 15}, {"x-speed", 5}, {"x-accuracy", 3}, {"guard-spec", 2}}; + std::vector got; + for (const SendTurn& turn : m_case.input.turns){ + supplies.foe_out = turn.foe_out; + const PolicyView view{ + .hp = static_cast(turn.hp), + .max_hp = 171, + .speed = 117, + .foe_index = turn.foe_index, + .battle_texts = turn.texts, + }; + got.push_back(seen_of(battle->choose(supplies, view))); + for (const std::string& item : turn.used){ + battle->item_used(item); + } + } + if (got != m_case.expectation.seen){ + return std::format("Got {}; expected {}.", describe(got), describe(m_case.expectation.seen)); + } + return true; + } + +private: + SendsCase m_case; +}; + +const SendsCase SENDS_CASES[] = { + { + // The battle loop's foe index stuck at Venusaur's slot from the + // second foe on; Blastoise met Arcanine at 26 HP and fell to + // Extreme Speed with the policy seeing no danger. + .name = "console B, 2026-11-17, Champion try 1: Venusaur second", + .input = { + .trainer = "TRAINER_CHAMPION_FIRST_BULBASAUR", + .turns = { + {.foe_out = "pidgeot", .foe_index = 0}, + {.foe_out = "venusaur", .foe_index = 5}, + {.foe_out = "alakazam", .foe_index = 5}, + {.foe_out = "rhydon", .foe_index = 5}, + {.foe_out = "gyarados", .foe_index = 5}, + {.foe_out = "arcanine", .foe_index = 5, .hp = 26}, + }, + }, + .expectation = {.seen = { + {.species = "pidgeot", .slot = 0, .left = 6, .danger = true}, + // Solar Beam is its only attack: no hit without the sun. + {.species = "venusaur", .slot = 5, .left = 5, .danger = false}, + {.species = "alakazam", .slot = 1, .left = 4, .danger = true}, + {.species = "rhydon", .slot = 2, .left = 3, .danger = true}, + {.species = "gyarados", .slot = 3, .left = 2, .danger = true}, + {.species = "arcanine", .slot = 4, .left = 1, .danger = true}, + }}, + }, + { + .name = "Bruno's Onix falls after three X Speeds with our Speed below +0: the plan's early reset", + .input = { + .trainer = "TRAINER_ELITE_FOUR_BRUNO", + .turns = { + {.foe_out = "onix", .used = {"x-speed", "x-speed", "x-speed"}}, + { + .foe_out = "hitmonlee", + // Four falls, each after another text: a text read twice in a row + // is one change (BattleTrack::read_turn()). + .texts = { + "blastoisesspeedfell", "foeonixusedrocktomb", "blastoisesspeedfell", "foeonixusedrocktomb", + "blastoisesspeedfell", "foeonixusedrocktomb", "blastoisesspeedfell", + }, + }, + }, + }, + .expectation = {.seen = {{.species = "onix", .slot = 0, .left = 5, .danger = true}, {.early_reset = true}}}, + }, + { + .name = "Bruno's Hitmonlee falls first: no early reset", + .input = { + .trainer = "TRAINER_ELITE_FOUR_BRUNO", + .turns = { + { + .foe_out = "hitmonlee", + .used = {"x-speed", "x-speed", "x-speed"}, + }, + { + .foe_out = "onix", + // Four falls, each after another text: a text read twice in a row + // is one change (BattleTrack::read_turn()). + .texts = { + "blastoisesspeedfell", "foeonixusedrocktomb", "blastoisesspeedfell", "foeonixusedrocktomb", + "blastoisesspeedfell", "foeonixusedrocktomb", "blastoisesspeedfell", + }, + }, + }, + }, + .expectation = {.seen = { + {.species = "hitmonlee", .slot = 2, .left = 5, .danger = true}, + {.species = "onix", .slot = 0, .left = 4, .danger = true}, + }}, + }, +}; + } // namespace void add_tests_AutoStoryPlannedBattle(UnitTestDatabase& database){ + for (const SendsCase& test_case : SENDS_CASES){ + database.add(test_case); + } for (const FoesCase& test_case : FOES_CASES){ database.add(test_case); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable.cpp index f70506aab3..facbe14804 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable.cpp @@ -12,6 +12,7 @@ #include "PokemonFRLG_AutoStoryGameArgs.h" #include "PokemonFRLG_AutoStoryGrind.h" #include "PokemonFRLG_AutoStoryHealingSteps.h" +#include "PokemonFRLG_AutoStoryHallOfFame.h" #include "PokemonFRLG_AutoStoryLeagueSteps.h" #include "PokemonFRLG_AutoStoryHideout.h" #include "PokemonFRLG_AutoStoryItemUse.h" @@ -89,9 +90,11 @@ using Pokemon::Direction; Pokemon::StoryActionSpec pc_action_spec(){ return {.kind = StoryStepKind::GAME, .name = PC_ACTION, .args = { required_arg("do", StoryArgType::WORD, "\"deposit\" or \"withdraw\""), - required_arg("slot", StoryArgType::INTEGER, "the party slot to deposit, or the box slot to withdraw, from 1"), + absent_arg("slot", StoryArgType::INTEGER, "the party slot to deposit (required), or the box slot to withdraw, from 1"), + absent_arg("species", StoryArgType::WORD, "a withdrawal's species' slug, found by reading the box instead of a slot"), required_arg("box", StoryArgType::INTEGER, "the box, from 1; a withdrawal's must be the one the PC shows"), optional_arg("if_present", StoryArgType::BOOLEAN, false, "skip a deposit when the party hasn't got the slot"), + optional_arg("protect", StoryArgType::BOOLEAN, false, "a withdrawal that rides along: its party slot is never sent into a battle"), }}; } @@ -279,7 +282,7 @@ std::span story_action_specs(){ for (Pokemon::StoryActionSpec spec : { pc_action_spec(), capture_action_spec(), fossil_action_spec(), level_action_spec(), mart_action_spec(), teach_action_spec(), grind_action_spec(), grind_check_action_spec(), - slope_action_spec(), count_items_action_spec(), + slope_action_spec(), count_items_action_spec(), hall_of_fame_action_spec(), }){ specs.push_back(std::move(spec)); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.cpp index 66303ffb00..368c4859e2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.cpp @@ -111,6 +111,17 @@ Supplies read_supplies(const JsonObject& execution, std::string_view source){ }else if (execution.get_value("lead") != nullptr){ throw ParseException(std::format("{}: lead must be a string", source)); } + // Party slots, from 1 as the game shows them, never sent into a + // battle: the lead (1) always fights. + if (const JsonValue* slots = execution.get_value("protected_slots")){ + for (const JsonValue& slot : slots->to_array_throw(std::string(source))){ + const int64_t number = slot.to_integer_throw(std::string(source)); + if (number < 2 || number > 6){ + throw ParseException(std::format("{}: protected slot {} out of range (2-6)", source, number)); + } + supplies.protected_slots.push_back((size_t)number - 1); + } + } return supplies; } @@ -197,7 +208,7 @@ Pokemon::StoryFileRow read_story_file(const JsonValue& value, s std::set note_keys; const Pokemon::ExecutionReader read_execution = [&](const JsonObject& execution, std::string_view where){ shared = Pokemon::read_story_row_execution( - execution, where, {"menu", "supplies", "lead", "handoff", "gift", "live_start", "notes"} + execution, where, {"menu", "supplies", "lead", "protected_slots", "handoff", "gift", "live_start", "notes"} ); checkpoint.menu = read_start_menu(execution.get_string_throw("menu", std::string(where)), where); checkpoint.supplies = read_supplies(execution, where); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.01.plan.json index 2c131e9442..041bdbf471 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.01.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.01.plan.json @@ -251,6 +251,7 @@ }, "execution": { "menu": "full", + "protected_slots": [6], "verify": { "tries": 10, "min_pass": 5, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.01a.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.01a.plan.json index ccd36795ed..9058534c18 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.01a.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.01a.plan.json @@ -3,9 +3,30 @@ "game": "frlg", "checkpoint": { "id": "20.01a", - "title": "Indigo Plateau's Mart: Full Restores before the Elite Four", + "title": "Indigo Plateau: the Magikarp from BOX 1, Full Restores before the Elite Four", "start": {"map": "IndigoPlateau_PokemonCenter_1F", "tiles": [[13, 12]], "facing": "north"}, "events": [ + { + "id": "indigo_pc_front", + "kind": "route", + "action": "run", + "args": { + "target": {"map": "IndigoPlateau_PokemonCenter_1F", "tiles": [[17, 10]], "facing": "north"} + }, + "why": "from the nurse to below the PC (17, 9), right of her counter, facing it" + }, + { + "id": "magikarp_from_box", + "kind": "game_action", + "action": "pc", + "args": { + "do": "withdraw", + "species": "magikarp", + "box": 1, + "protect": true + }, + "why": "the shiny Magikarp (Gift RNG at 00.12b, put in BOX 1 at 04.04) rides into the League with the team for the Hall of Fame (user, 2026-11-16): WITHDRAW, found by reading each cell's species in BOX 1. protect: its party slot is never sent into a battle. A full party refuses the withdrawal and stops the row: which member goes to the box is the user's call" + }, { "id": "indigo_mart_clerk", "kind": "route", @@ -13,7 +34,7 @@ "args": { "target": {"map": "IndigoPlateau_PokemonCenter_1F", "tiles": [[2, 7]], "facing": "west"} }, - "why": "from the nurse across the lobby to the Mart's counter, facing the clerk (0, 7) across it (1, 7)" + "why": "from the PC across the lobby to the Mart's counter, facing the clerk (0, 7) across it (1, 7)" }, { "id": "buy_full_restores", @@ -45,6 +66,7 @@ "notes": { "checkpoint": [ "Starts at Indigo Plateau's nurse, where 19.05 ends with the party healed. The Mart is its own row, as 19.01a is for the Route 22 rival: 20.01 starts at the counter with the bag as Lorelei needs it.", + "The PC (17, 9) is the Center's PC tile (metatile 0x62, as Route 4's (11, 1); pokefirered IndigoPlateau_PokemonCenter_1F layout), used from (17, 10) below it. The withdrawal puts the Magikarp at the party's end: with the five of the plans (the starter, the Oddish, the Pidgey, the Eevee and the Lapras) slot 6, which 20.01 to 20.05 keep out of the battles (protected_slots).", "Ends at the counter (2, 7) facing the clerk, where 20.01 starts." ] } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.02.plan.json index f1d3e167cb..80875af5fb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.02.plan.json @@ -209,6 +209,7 @@ }, "execution": { "menu": "full", + "protected_slots": [6], "verify": { "tries": 10, "min_pass": 1, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.03.plan.json index be54d429d3..2da5921425 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.03.plan.json @@ -208,6 +208,7 @@ }, "execution": { "menu": "full", + "protected_slots": [6], "verify": { "tries": 10, "min_pass": 5, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.04.plan.json index da74fc1ab3..47e2544466 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.04.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.04.plan.json @@ -184,6 +184,7 @@ }, "execution": { "menu": "full", + "protected_slots": [6], "verify": { "tries": 10, "min_pass": 2, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.05.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.05.plan.json index 9c75f3fda2..c0716b3bc6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.05.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-20.05.plan.json @@ -168,7 +168,9 @@ }, { "id": "hall_of_fame", - "kind": "scene", + "kind": "game_action", + "action": "hall_of_fame", + "args": {}, "declared_after": { "map": "PokemonLeague_HallOfFame", "tiles": [ @@ -179,12 +181,13 @@ ], "facing": "north" }, - "why": "the rival's last words, Oak's entrance and his talk, and Oak leading the player out north (PokemonLeague_ChampionsRoom_Movement_PlayerExit) to the Hall of Fame (5, 12); its OnFrame script walks the player eight tiles north to (5, 4), Oak's congratulations, then the record (FLDEFF_HALL_OF_FAME_RECORD) and the Hall of Fame (special EnterHallOfFame), which saves the game itself and rolls the credits" + "why": "the rival's last words, Oak's entrance and his talk, and Oak leading the player out north (PokemonLeague_ChampionsRoom_Movement_PlayerExit) to the Hall of Fame (5, 12); its OnFrame script walks the player eight tiles north to (5, 4), Oak's congratulations, then the record (FLDEFF_HALL_OF_FAME_RECORD) and the Hall of Fame (special EnterHallOfFame), which saves the game itself. The step presses B until the save message (HallOfFameSavingDetector), then keeps screenshots in Screenshots/AutoStory/hall_of_fame/ by how the screen moves (user, 2026-11-16): each member's 2 s hold, the whole team as the confetti starts, and the LEAGUE CHAMP card, where it stops before the credits" } ] }, "execution": { "menu": "full", + "protected_slots": [6], "verify": { "tries": 10, "min_pass": 2, diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 1d36770b92..701160ac92 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2361,6 +2361,9 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcDialog.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcRun.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHallOfFame.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHallOfFame.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHallOfFame_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage_Tests.cpp