From 6cc528647cf50015778192d008d24413ec9e577f Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 01:41:32 -0800 Subject: [PATCH 01/14] FRLG AutoStory 16.03: the Silph rival by PokeGuy's C2 policy, Blizzard first Console B lost the 7F rival (2026-11-15, run 6): the step had no battle policy, so the lead attacked from turn 1 with no X items, and it came with Dig instead of 13.03's Blizzard; Venusaur slept it and the back line fell. 16.03 now starts with TM14 Blizzard over Dig (nothing if the lead knows it, skipped without the TM), and fights the rival by goal-based-setup with the doc's silph-rival goals and PokemonSimulation planning_risk() rows for console B's entry (Lv43, 136/136). Sim of that entry: 12% as run, 35% with C2 alone, 46% with Blizzard alone, 99.4% with both. --- .../PokemonFRLG_AutoStorySilph_Tests.cpp | 103 +++++++++++++++++- .../AutoStory/StoryData/frlg-16.03.plan.json | 76 ++++++++++++- 2 files changed, 177 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp index 732ea35c9a..6119734cad 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp @@ -3,17 +3,20 @@ * From: https://github.com/PokemonAutomation/ * * Rows 16.01 to 16.06 (StoryData/frlg-16.01.plan.json ...) on the overworld sim: from - * Saffron's nurse into Silph Co., up to 3F and through its pad to 5F. + * Saffron's nurse into Silph Co., up to 3F and through its pad to 5F; + * the 7F rival's fight as PokeGuy fought it. * */ #include +#include #include #include #include #include #include #include +#include #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" @@ -22,14 +25,17 @@ #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldGatedRoute.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h" +#include "PokemonFRLG_AutoStoryBattlePolicyData.h" #include "PokemonFRLG_AutoStoryEvents.h" #include "PokemonFRLG_AutoStoryMovedTrainers.h" #include "PokemonFRLG_AutoStoryPacedRoute.h" +#include "PokemonFRLG_AutoStoryParty.h" #include "PokemonFRLG_AutoStoryRooms.h" #include "PokemonFRLG_AutoStoryRowSim.h" #include "PokemonFRLG_AutoStoryRows_Tests.h" #include "PokemonFRLG_AutoStoryRows16_Tests.h" #include "PokemonFRLG_AutoStoryTable.h" +#include "PokemonFRLG_AutoStoryTeachPlan.h" #include "PokemonFRLG_AutoStoryTrainerLine.h" namespace PokemonAutomation{ @@ -514,6 +520,89 @@ UnitTestResult test_corridor_rocket_stands_where_he_stopped(){ return true; } +// 16.03's step named `name`; none if it has none. +const StoryStep* rival_row_step(std::string_view name){ + for (const StoryStep& step : checkpoint("16.03").steps){ + if (step.name == name){ + return &step; + } + } + return nullptr; +} + +// The 7F rival by PokeGuy's C2 (PokemonSimulation docs/battles/pokeguy-conditions.md, +// silph-rival: Sp. Atk +2, accuracy +2, outspeed, Guard Spec first). Without it, +// console B's lead attacked from turn 1, Venusaur slept it and the battle +// was lost (2026-11-15 01:22, 16.03 run 6); the sim of that entry wins 12% +// of 500, and 99% with the policy and Blizzard. The rows are PokemonSimulation's +// planning_risk() for each foe leading against that entry (Lv43, 136/136). +UnitTestResult test_rival_fought_by_policy(){ + const StoryStep* rival = rival_row_step("rival"); + if (rival == nullptr || rival->kind != StoryStepKind::BATTLE){ + return "16.03 has no rival battle step."; + } + const BattlePolicy policy = battle_policy_of(rival->args); + if (policy.kind != BattlePolicyKind::GOAL_BASED_SETUP){ + return "16.03 rival: expected the goal-based-setup policy."; + } + const SetupGoals& goals = policy.goals; + if (goals.spa != 2 || goals.accuracy != 2 || !goals.outspeed || !goals.guard_accuracy + || goals.torrent_power || goals.target_torrent + ){ + return "16.03 rival: expected the doc's goals, Sp. Atk +2, accuracy +2, outspeed and Guard Spec first."; + } + const auto found = rival->args.find("foe_threats"); + const auto* rows = found == rival->args.end() ? nullptr : std::get_if>>(&found->second); + const std::vector> expected = { + {76, 18, 17, 39, 19, 0, 0}, // Pidgeot + {78, 40, 15, 40, 40, 0, 0}, // Gyarados + {51, 14, 5, 32, 16, 0, 0}, // Growlithe + {93, 0, 4, 44, 22, 0, 0}, // Alakazam + {76, 46, 26, 96, 48, 0, 0}, // Venusaur + }; + if (rows == nullptr || *rows != expected){ + return "16.03 rival: foe_threats are not the generator's rows for console B's entry."; + } + return true; +} + +// Before the walk, TM14 Blizzard over Dig, as 13.03 teaches it: console B's +// lead came to the rival with Dig (2026-11-15, 16.03 run 6), and Blizzard is +// what wins against Venusaur. A lead that knows it already is left as it is. +UnitTestResult test_blizzard_before_rival(){ + const std::vector& steps = checkpoint("16.03").steps; + if (steps.empty() || steps[0].kind != StoryStepKind::GAME || steps[0].action != TEACH_ACTION){ + return "16.03: expected it to start with a TEACH."; + } + const std::optional plan = teach_plan_of(steps[0].args); + if (!plan || plan->item != "tm14" || plan->move != "blizzard" || plan->member != 0){ + return "16.03: expected TM14 Blizzard on the lead."; + } + const TeachDecision console_b = decide_teach(*plan, {"bite", "water-pulse", "surf", "dig"}); + if (!console_b.error.empty() || console_b.forget != std::optional(3)){ + return std::format("16.03: expected Blizzard over Dig on console B's lead: {}", console_b.error); + } + if (!decide_teach(*plan, {"bite", "water-pulse", "surf", "blizzard"}).already_known){ + return "16.03: a lead that knows Blizzard should be left as it is."; + } + return true; +} + +// The lead's attack against the rival's Venusaur: Blizzard, the one that +// is super effective (PokeGuy: one Blizzard, 1:29:39). +UnitTestResult test_blizzard_against_venusaur(){ + PartyMember lead; + lead.species = "blastoise"; + lead.moves_read = true; + lead.level = 43; + lead.moves = {{"bite", 25}, {"water-pulse", 20}, {"surf", 14}, {"blizzard", 5}}; + constexpr size_t BLIZZARD = 3; + if (attack_slot_against(lead, {"venusaur"}) != BLIZZARD){ + return "Expected Blizzard against Venusaur."; + } + return true; +} + const AutoStorySilphCase AUTO_STORY_SILPH_CASES[] = { { .input = {.name = "16.01 Silph Co.'s stairs to 3F and its pad to 5F", .body = test_pad_to_5f}, @@ -567,6 +656,18 @@ const AutoStorySilphCase AUTO_STORY_SILPH_CASES[] = { .input = {.name = "the Master Ball option and SPEEDRUN", .body = test_master_ball_option}, .expectation = {.description = "skipped when the option is off or in a SPEEDRUN"}, }, + { + .input = {.name = "16.03 the rival by PokeGuy's C2 policy", .body = test_rival_fought_by_policy}, + .expectation = {.description = "goal-based-setup to Sp. Atk +2 and accuracy +2, outspeed, PokemonSimulation planning_risk() rows"}, + }, + { + .input = {.name = "16.03 TM14 Blizzard before the rival", .body = test_blizzard_before_rival}, + .expectation = {.description = "a TEACH first: Blizzard over Dig, nothing if the lead knows it"}, + }, + { + .input = {.name = "16.03 the lead's attack against the rival's Venusaur", .body = test_blizzard_against_venusaur}, + .expectation = {.description = "Blizzard"}, + }, }; } // namespace diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.03.plan.json index 25897b0b53..b7ff616c1d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.03.plan.json @@ -20,6 +20,28 @@ "trainer_at:SilphCo_5F/rocket_m@9,21=11,21" ], "events": [ + { + "id": "teach_blizzard", + "kind": "game_action", + "action": "teach", + "args": { + "item": "tm14", + "move": "blizzard", + "slot": 1, + "keep": [ + "tackle", + "withdraw", + "tail-whip", + "water-gun", + "bubble", + "bite", + "mega-kick", + "water-pulse", + "surf" + ] + }, + "why": "TM14 Blizzard over Dig, as 13.03 teaches it (pokeguy's moves here: Surf, Water Pulse, Bite, Blizzard; the user approved the TM spend 2026-11-12), unless the lead knows it: console B's lead came to the rival with Dig (2026-11-15, run 6) and lost to Venusaur; PokemonSimulation's silph-rival sim of that entry (Lv43, 136/136) wins 35% by C2 with Dig and 99% with Blizzard. Keep list: every move the lead can know but Dig; skipped if the bag has no TM14" + }, { "id": "to_3f_trainer", "kind": "route", @@ -126,6 +148,58 @@ "growlithe", "alakazam", "venusaur" + ], + "policy": "goal-based-setup", + "setup_spa": 2, + "setup_accuracy": 2, + "setup_outspeed": true, + "setup_guard_accuracy": true, + "foe_threats": [ + [ + 76, + 18, + 17, + 39, + 19, + 0, + 0 + ], + [ + 78, + 40, + 15, + 40, + 40, + 0, + 0 + ], + [ + 51, + 14, + 5, + 32, + 16, + 0, + 0 + ], + [ + 93, + 0, + 4, + 44, + 22, + 0, + 0 + ], + [ + 76, + 46, + 26, + 96, + 48, + 0, + 0 + ] ] }, "declared_after": { @@ -138,7 +212,7 @@ ], "facing": "south" }, - "why": "the rival (TRAINER_RIVAL_SILPH_BULBASAUR, for a Squirtle): Pidgeot Lv37, Gyarados Lv38, Growlithe Lv35, Alakazam Lv35, Venusaur Lv40; then he leaves (VAR_MAP_SCENE_SILPH_CO_7F 1)" + "why": "the rival (TRAINER_RIVAL_SILPH_BULBASAUR, for a Squirtle): Pidgeot Lv37, Gyarados Lv38, Growlithe Lv35, Alakazam Lv35, Venusaur Lv40, fought by pokeguy's policy C2 (goal-based-setup: Sp. Atk +2 and accuracy +2 by X Specials and X Accuracies, X Speed until faster, Guard Spec first; the doc's silph-rival row, pokeguy 1:28:54 to 1:30:09); foe_threats are the rows PokemonSimulation's planning_risk() gives for each foe leading against console B's entry (Lv43, 136/136, 2026-11-15 run 6); without the policy the lead attacked from turn 1, Venusaur slept it and the battle was lost (sim of that entry: 12% of 500); then he leaves (VAR_MAP_SCENE_SILPH_CO_7F 1)" } ] }, From 249cad39de9bf5c2b8d1e258f80b90ffd9a48d26 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 02:01:41 -0800 Subject: [PATCH 02/14] FRLG AutoStory teach: scroll the TM Case to a TM below its rows; a missing TM fails 13.03's teach_blizzard read only the five rows shown (HM01-HM04, TM06) and skipped TM14 on the sixth. open_row() now steps DOWN to the list's end, then UP to its top, until the rows shown have the target; only then NOT_IN_CASE. A TM or HM the plan picked up but the case hasn't got now fails the step (teach_no_item, red log, debug image) instead of skipping it. --- .../PokemonFRLG_AutoStoryAbilities.cpp | 3 +- .../PokemonFRLG_AutoStoryAbilities_Tests.cpp | 2 +- .../AutoStory/PokemonFRLG_AutoStoryTeach.cpp | 6 +- .../AutoStory/PokemonFRLG_AutoStoryTeach.h | 3 +- .../PokemonFRLG_AutoStoryTeachScreens.cpp | 45 +++++++++- .../PokemonFRLG_AutoStoryTeachScreens.h | 8 +- ...okemonFRLG_AutoStoryTeachScreens_Tests.cpp | 84 +++++++++++++++++-- .../PokemonFRLG_AutoStoryTeach_Tests.cpp | 2 +- .../AutoStory/StoryData/frlg-16.03.plan.json | 2 +- 9 files changed, 135 insertions(+), 20 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.cpp index a8584ae77b..1a217a5ac8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.cpp @@ -34,8 +34,7 @@ Outcome teach(Logger& logger, const TeachPlan& plan, StoryAbilities& abilities){ if (result.end == TeachEnd::LEARNED){ return Pokemon::outcome_ok(); } - // NO_ITEM: skipped, with run_teach()'s log line. - if (result.end == TeachEnd::ALREADY_KNOWN || result.end == TeachEnd::NO_ITEM){ + if (result.end == TeachEnd::ALREADY_KNOWN){ return Pokemon::outcome_unchanged(); } return Pokemon::outcome_failed(std::format("teach_{}", teach_end_name(result.end)), teach_message(plan, result)); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp index f188f1e214..58d211460e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp @@ -156,7 +156,7 @@ std::vector step_cases(){ {"level unread", [](StubGame& game){ game.level.reset(); }, level_step("check", "", {.slot = 0, .level = 16}), "level_unread"}, {"a Mart purchase that fails", none, mart, "mart_failed"}, {"a move it knows already succeeds", none, teach_step("teach", "", {.item = "hm03", .move = "surf", .member = 0}), "unchanged"}, - {"a TM not in the TM Case is skipped", none, teach_step("teach", "", {.item = "tm03", .move = "water-pulse", .member = 0, .forget_move = "water-gun"}), "unchanged"}, + {"a TM not in the TM Case fails", none, teach_step("teach", "", {.item = "tm03", .move = "water-pulse", .member = 0, .forget_move = "water-gun"}), "teach_no_item"}, {"a level that can't be read stops a GRIND", [](StubGame& game){ game.level.reset(); }, grind, "grind_unread"}, {"a GRIND without a nurse", none, game_step(GRIND_ACTION, {{"slot", int64_t(1)}, {"level", int64_t(20)}}), "bad_args"}, {"no YES/NO at the fossil", none, fossil_step("fossil", "", Fossil::HELIX), "fossil_failed"}, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeach.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeach.cpp index 84e7b3463e..357d61370e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeach.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeach.cpp @@ -235,6 +235,8 @@ class TeachRun{ case ItemOpen::OPENED: break; case ItemOpen::NOT_IN_CASE: + // The plan picked it up earlier: a story step went wrong. + m_actions.dump_screen("not-in-case"); return leave(TeachEnd::NO_ITEM, std::format("no {} in the TM Case", m_plan.item)); case ItemOpen::FAILED: m_actions.dump_screen("open-failed"); @@ -362,7 +364,7 @@ TeachResult run_teach( found.member = *slot; } TeachResult result = TeachRun(actions, reader, found).run(); - const Color color = result.end == TeachEnd::LEARNED ? COLOR_BLACK : result.end == TeachEnd::NO_ITEM ? COLOR_ORANGE : COLOR_RED; + const Color color = result.end == TeachEnd::LEARNED ? COLOR_BLACK : COLOR_RED; logger.log(teach_message(found, result), color); return result; } @@ -379,8 +381,6 @@ std::string teach_message(const TeachPlan& plan, const TeachResult& result){ : std::format("{}: learned, forgot {}.", what, result.forgot); case TeachEnd::ALREADY_KNOWN: return std::format("{}: it knows it already; nothing done.", what); - case TeachEnd::NO_ITEM: - return std::format("{}: {}; skipped.", what, result.detail); default: return std::format("{}: {}: {}.", what, teach_end_name(result.end), result.detail); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeach.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeach.h index 14ebef4ad6..2c08d81308 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeach.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeach.h @@ -75,7 +75,8 @@ enum class TeachEnd{ REFUSED, // The member's moves couldn't be read: nothing was pressed. UNREAD, - // The TM or HM isn't in the TM CASE: skipped, nothing taught. + // The TM or HM isn't in the TM CASE: nothing taught, the step fails + // (the plan picked it up earlier). NO_ITEM, FAILED, }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.cpp index a0685f67f7..b7d964c4da 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.cpp @@ -41,6 +41,9 @@ namespace{ constexpr size_t MAX_LOOKS = 40; // Looks at the same moves page after A on a move: the game refused it. constexpr size_t MOVES_PAGE_STILL = 8; +// Presses one way through a list longer than its rows shown: past the +// TM CASE's 58 TMs, 8 HMs and CLOSE. +constexpr size_t MAX_LIST_PRESSES = 70; std::string rows_text(const std::vector& rows){ std::string text; @@ -109,16 +112,50 @@ bool ScreenTeachActions::to_key_items(){ return false; } +std::optional ScreenTeachActions::scroll_to_row(TeachScreen screen, std::string_view name){ + std::optional view = wait_for(screen); + std::vector seen; + // DOWN to the list's end, then UP to its top: the list opens where it + // was left. + for (const TeachPress way : {TeachPress::DOWN, TeachPress::UP}){ + for (size_t c = 0; view && c < MAX_LIST_PRESSES; c++){ + if (view->rows.empty()){ + m_logger.log(std::format("Teach: no rows read on {}.", teach_screen_name(screen)), COLOR_RED); + return std::nullopt; + } + for (const std::string& row : view->rows){ + if (std::find(seen.begin(), seen.end(), row) == seen.end()){ + seen.push_back(row); + } + } + if (std::find(view->rows.begin(), view->rows.end(), name) != view->rows.end()){ + return view; + } + press(way); + std::optional next = wait_for(screen); + // Neither the cursor nor the rows moved: the list's end. + const bool still = next && next->cursor == view->cursor && next->rows == view->rows; + view = std::move(next); + if (still){ + break; + } + } + } + if (view){ + m_logger.log(std::format("Teach: no {} in {} ({}).", name, teach_screen_name(screen), rows_text(seen)), COLOR_RED); + return view; + } + return std::nullopt; +} + ItemOpen ScreenTeachActions::open_row(TeachScreen screen, std::string_view name){ - const std::optional view = wait_for(screen); + const std::optional view = scroll_to_row(screen, name); if (!view){ return ItemOpen::FAILED; } const auto found = std::find(view->rows.begin(), view->rows.end(), name); if (found == view->rows.end()){ - m_logger.log(std::format("Teach: no {} in {} ({}).", name, teach_screen_name(screen), rows_text(view->rows)), COLOR_RED); - // No rows: nothing was read. - return view->rows.empty() ? ItemOpen::FAILED : ItemOpen::NOT_IN_CASE; + return ItemOpen::NOT_IN_CASE; } if (!move_cursor_to(screen, size_t(found - view->rows.begin()))){ return ItemOpen::FAILED; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.h index 571b170a30..cb5fad1bbc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.h @@ -128,8 +128,14 @@ class ScreenTeachActions : public TeachActions{ bool move_cursor_to(TeachScreen screen, size_t row); // In the bag: to KEY ITEMS. bool to_key_items(); + // On the list `screen`: DOWN, then UP, one row at a time until the rows + // shown have `name` or the list ends either way. The last view read: + // without `name` if the list hasn't got it; nothing if the list didn't + // show or no rows were read. + std::optional scroll_to_row(TeachScreen screen, std::string_view name); // On the list `screen`: A on the row `name`, then A on the item menu's - // top row (OPEN or USE). NOT_IN_CASE: the rows read haven't got it. + // top row (OPEN or USE). NOT_IN_CASE: the list, scrolled through end to + // end, hasn't got it. ItemOpen open_row(TeachScreen screen, std::string_view name); // After A on a member: what it showed. TeachPrompt member_answer(); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens_Tests.cpp index 88445df489..88839b0631 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens_Tests.cpp @@ -117,6 +117,12 @@ struct FakeTeachGame{ size_t looks = 0; // The TM in use, by row. size_t tm = 0; + // The TM CASE's rows shown at once (pokefirered tm_case.c: five), the + // top one shown, and the row its cursor opens on: the game keeps both + // from the last time it was open. + size_t tm_rows_shown = 5; + size_t tm_scroll = 0; + size_t tm_open_cursor = 0; std::vector presses = {}; std::vector dumps = {}; std::string error = {}; @@ -132,6 +138,18 @@ std::vector tm_rows(const FakeTeachGame& game){ return rows; } +// The TM CASE's top row shown: the list scrolls by one when the cursor +// moves past either end of the rows shown. +size_t tm_top_row(const FakeTeachGame& game){ + if (game.cursor < game.tm_scroll){ + return game.cursor; + } + if (game.cursor >= game.tm_scroll + game.tm_rows_shown){ + return game.cursor + 1 - game.tm_rows_shown; + } + return game.tm_scroll; +} + void show_texts(FakeTeachGame& game, std::deque texts, FakeScreen after){ game.texts = std::move(texts); game.after_texts = after; @@ -274,7 +292,7 @@ void press_bag(FakeTeachGame& game, TeachPress press){ void press_bag_menu(FakeTeachGame& game, TeachPress press){ if (press == TeachPress::A && game.cursor == 0){ game.screen = FakeScreen::TM_CASE; - game.cursor = 0; + game.cursor = game.tm_open_cursor; }else if (press == TeachPress::A || press == TeachPress::B){ game.screen = FakeScreen::BAG; game.cursor = game.bag_cursor; @@ -293,7 +311,9 @@ void press_tm_case(FakeTeachGame& game, TeachPress press){ game.screen = FakeScreen::TM_MENU; game.cursor = 0; }else{ + game.tm_scroll = tm_top_row(game); move_cursor(game, press, rows + 1); + game.tm_scroll = tm_top_row(game); } } @@ -399,8 +419,14 @@ TeachView view_of(const FakeTeachGame& game){ case FakeScreen::BAG_MENU: case FakeScreen::TM_MENU: return {.screen = TeachScreen::ITEM_MENU, .cursor = game.cursor}; - case FakeScreen::TM_CASE: - return {.screen = TeachScreen::TM_CASE, .cursor = game.cursor, .rows = tm_rows(game)}; + case FakeScreen::TM_CASE:{ + // The rows shown, CLOSE not read. + const size_t top = tm_top_row(game); + const std::vector rows = tm_rows(game); + const size_t end = std::min(rows.size(), top + game.tm_rows_shown); + return {.screen = TeachScreen::TM_CASE, .cursor = game.cursor - top, + .rows = std::vector(rows.begin() + std::min(top, rows.size()), rows.begin() + end)}; + } case FakeScreen::PROMPT: return {.screen = TeachScreen::PARTY_PROMPT, .text = TeachText::TEACH_WHICH}; case FakeScreen::TEXT: @@ -521,6 +547,26 @@ void wartortle_dig(FakeTeachGame& game){ void no_tm03(FakeTeachGame& game){ game.tms = {{"rock-tomb", 1}}; } +// Console B, 13.03 (2026-11-14): nine in the TM CASE, five shown; TM14 +// BLIZZARD is the sixth, below them. +void nine_tms(FakeTeachGame& game){ + game.tms = { + {"cut", 1}, {"fly", 1}, {"surf", 1}, {"strength", 1}, {"toxic", 1}, + {"blizzard", 1}, {"giga-drain", 1}, {"shock-wave", 1}, {"fire-blast", 1}, + }; + game.compatible.insert("blizzard"); +} +// Left open on CLOSE: BLIZZARD to CLOSE shown, TOXIC above them. +void nine_tms_left_on_close(FakeTeachGame& game){ + nine_tms(game); + game.compatible.insert("toxic"); + game.tm_open_cursor = 9; + game.tm_scroll = 5; +} +void nine_tms_no_blizzard(FakeTeachGame& game){ + nine_tms(game); + game.tms[5].count = 0; +} // Console B, 2026-11-03: the step stopped pressing on "Poof!" read as // "learned", and waited 5 minutes. void wartortle_dig_poof_learned(FakeTeachGame& game){ @@ -571,7 +617,7 @@ std::vector screens_cases(){ .moves = {"bite", "bubble", "withdraw", "water-pulse"}, }, { - .name = "no TM03 in the TM CASE: skipped, back to the overworld", + .name = "no TM03 in the TM CASE: failed, back to the overworld", .setup = no_tm03, .end = TeachEnd::NO_ITEM, .moves = {"bite", "tail-whip", "bubble", "withdraw"}, @@ -586,6 +632,32 @@ std::vector screens_cases(){ }; } +// A TM CASE longer than the rows it shows. +std::vector long_case_screens_cases(){ + const TeachPlan blizzard{.item = "tm14", .move = "blizzard", .member = 0, .forget_move = "tail-whip"}; + return { + { + .name = "console B 13.03: TM14 on the sixth row, below the five shown", + .setup = nine_tms, + .plan = blizzard, + .moves = {"bite", "blizzard", "bubble", "withdraw"}, + }, + { + .name = "TM06 above the rows shown, the TM CASE left on CLOSE", + .setup = nine_tms_left_on_close, + .plan = {.item = "tm06", .move = "toxic", .member = 0, .forget_move = "tail-whip"}, + .moves = {"bite", "toxic", "bubble", "withdraw"}, + }, + { + .name = "no TM14 in a TM CASE of eight: failed after its last row", + .setup = nine_tms_no_blizzard, + .plan = blizzard, + .end = TeachEnd::NO_ITEM, + .moves = {"bite", "tail-whip", "bubble", "withdraw"}, + }, + }; +} + // The TM's scene, with a free slot and after a forget, for a TM and an HM. std::vector scene_screens_cases(){ return { @@ -685,7 +757,7 @@ UnitTestResult check_screens_case(const ScreensCase& item){ return std::format("{}: didn't end on the overworld; presses: {}.", item.name, presses); } // A debug image for each failure, none otherwise. - const bool failed = item.end == TeachEnd::FAILED || item.end == TeachEnd::REFUSED; + const bool failed = item.end == TeachEnd::FAILED || item.end == TeachEnd::REFUSED || item.end == TeachEnd::NO_ITEM; if (failed == game.dumps.empty()){ return std::format("{}: {} debug images.", item.name, game.dumps.size()); } @@ -717,7 +789,7 @@ UnitTestResult test_stuck_scene(){ } UnitTestResult test_screens(){ - for (const std::vector& cases : {screens_cases(), scene_screens_cases(), tutor_screens_cases()}){ + for (const std::vector& cases : {screens_cases(), long_case_screens_cases(), scene_screens_cases(), tutor_screens_cases()}){ for (const ScreensCase& item : cases){ const UnitTestResult result = check_screens_case(item); if (result.result != UnitTestResult::PASSED){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeach_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeach_Tests.cpp index 2ec976748d..6d00d52652 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeach_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeach_Tests.cpp @@ -320,7 +320,7 @@ std::vector teach_cases_3(){ .opened = false, }, { - .name = "an item the TM Case hasn't got: skipped", + .name = "an item the TM Case hasn't got: failed", .plan = {.item = "hm05", .move = "flash", .member = 2, .forget_move = "acid"}, .end = TeachEnd::NO_ITEM, .moves = {"absorb", "cut", "poison-powder", "acid"}, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.03.plan.json index b7ff616c1d..769ff3f610 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.03.plan.json @@ -40,7 +40,7 @@ "surf" ] }, - "why": "TM14 Blizzard over Dig, as 13.03 teaches it (pokeguy's moves here: Surf, Water Pulse, Bite, Blizzard; the user approved the TM spend 2026-11-12), unless the lead knows it: console B's lead came to the rival with Dig (2026-11-15, run 6) and lost to Venusaur; PokemonSimulation's silph-rival sim of that entry (Lv43, 136/136) wins 35% by C2 with Dig and 99% with Blizzard. Keep list: every move the lead can know but Dig; skipped if the bag has no TM14" + "why": "TM14 Blizzard over Dig, as 13.03 teaches it (pokeguy's moves here: Surf, Water Pulse, Bite, Blizzard; the user approved the TM spend 2026-11-12), unless the lead knows it: console B's lead came to the rival with Dig (2026-11-15, run 6) and lost to Venusaur; PokemonSimulation's silph-rival sim of that entry (Lv43, 136/136) wins 35% by C2 with Dig and 99% with Blizzard. Keep list: every move the lead can know but Dig. 13.03 teaches it first, so this step finds it known; with neither Blizzard nor TM14 the step fails, as the plan picked TM14 up" }, { "id": "to_3f_trainer", From d290a6ecd85317fc74e59d61d1bf9d57feca16a3 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 02:18:22 -0800 Subject: [PATCH 03/14] FRLG AutoStory teach tests: the fake TM Case scrolls with the cursor off its bottom row, as on console B Console B, 16.03 (2026-11-15): the arrow stayed on the fourth row of five while the rows moved up one per DOWN, so moving to BLIZZARD's row index ran the cursor to CLOSE. The long-case tests go red on 249cad39d. --- ...okemonFRLG_AutoStoryTeachScreens_Tests.cpp | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens_Tests.cpp index 88839b0631..b5e2c647d5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens_Tests.cpp @@ -117,7 +117,7 @@ struct FakeTeachGame{ size_t looks = 0; // The TM in use, by row. size_t tm = 0; - // The TM CASE's rows shown at once (pokefirered tm_case.c: five), the + // The TM CASE's rows shown at once (five, the last cut in half), the // top one shown, and the row its cursor opens on: the game keeps both // from the last time it was open. size_t tm_rows_shown = 5; @@ -138,16 +138,20 @@ std::vector tm_rows(const FakeTeachGame& game){ return rows; } -// The TM CASE's top row shown: the list scrolls by one when the cursor -// moves past either end of the rows shown. -size_t tm_top_row(const FakeTeachGame& game){ - if (game.cursor < game.tm_scroll){ - return game.cursor; +// The TM CASE scrolls rather than put the cursor on its bottom row shown +// while rows are left below (console B, 16.03, 2026-11-15: the arrow +// stayed on the fourth row of five and the rows moved up one per DOWN), or +// on its top row while rows are left above (assumed the same). +void scroll_tm_case(FakeTeachGame& game){ + // The TMs and CLOSE. + const size_t total = tm_rows(game).size() + 1; + const size_t shown = game.tm_rows_shown; + while (game.cursor + 2 > game.tm_scroll + shown && game.tm_scroll + shown < total){ + game.tm_scroll++; } - if (game.cursor >= game.tm_scroll + game.tm_rows_shown){ - return game.cursor + 1 - game.tm_rows_shown; + while (game.cursor < game.tm_scroll + 1 && game.tm_scroll > 0){ + game.tm_scroll--; } - return game.tm_scroll; } void show_texts(FakeTeachGame& game, std::deque texts, FakeScreen after){ @@ -311,9 +315,8 @@ void press_tm_case(FakeTeachGame& game, TeachPress press){ game.screen = FakeScreen::TM_MENU; game.cursor = 0; }else{ - game.tm_scroll = tm_top_row(game); move_cursor(game, press, rows + 1); - game.tm_scroll = tm_top_row(game); + scroll_tm_case(game); } } @@ -421,7 +424,7 @@ TeachView view_of(const FakeTeachGame& game){ return {.screen = TeachScreen::ITEM_MENU, .cursor = game.cursor}; case FakeScreen::TM_CASE:{ // The rows shown, CLOSE not read. - const size_t top = tm_top_row(game); + const size_t top = game.tm_scroll; const std::vector rows = tm_rows(game); const size_t end = std::min(rows.size(), top + game.tm_rows_shown); return {.screen = TeachScreen::TM_CASE, .cursor = game.cursor - top, @@ -563,6 +566,12 @@ void nine_tms_left_on_close(FakeTeachGame& game){ game.tm_open_cursor = 9; game.tm_scroll = 5; } +// Console B, 16.03 (2026-11-15): TM14 BLIZZARD on the sixth row, as in +// 13.03; ROCK TOMB last. +void ten_tms(FakeTeachGame& game){ + nine_tms(game); + game.tms.push_back({"rock-tomb", 1}); +} void nine_tms_no_blizzard(FakeTeachGame& game){ nine_tms(game); game.tms[5].count = 0; @@ -642,6 +651,12 @@ std::vector long_case_screens_cases(){ .plan = blizzard, .moves = {"bite", "blizzard", "bubble", "withdraw"}, }, + { + .name = "console B 16.03: the cursor stays on the fourth row while the rows scroll", + .setup = ten_tms, + .plan = blizzard, + .moves = {"bite", "blizzard", "bubble", "withdraw"}, + }, { .name = "TM06 above the rows shown, the TM CASE left on CLOSE", .setup = nine_tms_left_on_close, From 85e02a92fd01410daa96dea3ac8517dbbf84723c Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 02:19:01 -0800 Subject: [PATCH 04/14] FRLG AutoStory teach: move the cursor onto a list row by name, found again in each view The TM Case scrolls under a cursor that stays off its bottom row, so a fixed row index walked the cursor past BLIZZARD to CLOSE (console B, 16.03). A is pressed only once the row under the cursor reads as the target. --- .../PokemonFRLG_AutoStoryTeachScreens.cpp | 29 +++++++++++++++++-- .../PokemonFRLG_AutoStoryTeachScreens.h | 3 ++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.cpp index b7d964c4da..d00ec0a6c0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.cpp @@ -91,6 +91,30 @@ bool ScreenTeachActions::move_cursor_to(TeachScreen screen, size_t row){ return false; } +bool ScreenTeachActions::move_cursor_onto(TeachScreen screen, std::string_view name){ + for (size_t c = 0; c < TEACH_LIST_ROWS * 2; c++){ + const TeachView view = m_screens.look(); + if (view.screen != screen || !view.cursor){ + m_logger.log(std::format("Teach: lost the cursor on {}.", teach_screen_name(screen)), COLOR_RED); + return false; + } + // Found again in each view: a long list scrolls under a cursor + // that stays off its bottom row. + const auto found = std::find(view.rows.begin(), view.rows.end(), name); + if (found == view.rows.end()){ + m_logger.log(std::format("Teach: {} left the rows on {} ({}).", name, teach_screen_name(screen), rows_text(view.rows)), COLOR_RED); + return false; + } + const size_t row = size_t(found - view.rows.begin()); + if (*view.cursor == row){ + return true; + } + press(*view.cursor < row ? TeachPress::DOWN : TeachPress::UP); + } + m_logger.log(std::format("Teach: the cursor didn't reach {} on {}.", name, teach_screen_name(screen)), COLOR_RED); + return false; +} + bool ScreenTeachActions::to_key_items(){ for (size_t c = 0; c < 4; c++){ const std::optional view = wait_for(TeachScreen::BAG); @@ -153,11 +177,10 @@ ItemOpen ScreenTeachActions::open_row(TeachScreen screen, std::string_view name) if (!view){ return ItemOpen::FAILED; } - const auto found = std::find(view->rows.begin(), view->rows.end(), name); - if (found == view->rows.end()){ + if (std::find(view->rows.begin(), view->rows.end(), name) == view->rows.end()){ return ItemOpen::NOT_IN_CASE; } - if (!move_cursor_to(screen, size_t(found - view->rows.begin()))){ + if (!move_cursor_onto(screen, name)){ return ItemOpen::FAILED; } press(TeachPress::A); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.h index cb5fad1bbc..7a1eb972bc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTeachScreens.h @@ -126,6 +126,9 @@ class ScreenTeachActions : public TeachActions{ std::optional wait_for(TeachScreen screen, size_t looks = 12); // On `screen`: the cursor to `row`. False if it didn't get there. bool move_cursor_to(TeachScreen screen, size_t row); + // On the list `screen`: the cursor onto the row `name`, which the rows + // shown have. False if it didn't get there or `name` left the rows. + bool move_cursor_onto(TeachScreen screen, std::string_view name); // In the bag: to KEY ITEMS. bool to_key_items(); // On the list `screen`: DOWN, then UP, one row at a time until the rows From 1719da6ae3b1bf5540416a8be8bd66b13d8dfd4a Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 03:00:56 -0800 Subject: [PATCH 05/14] FRLG AutoStory: a TEACH to the lead updates the battles' lead Console B lost the Silph rival (2026-11-15, 16.03 run 8) after teaching Blizzard over Dig: the battles kept the lead's moves read when the save loaded, so it attacked Pidgeot with Surf and Venusaur with Bite instead of Blizzard, and Venusaur slept it. A learned TEACH to party slot 1 now puts the move, at full PP, in the forgotten move's slot of the battles' lead. --- .../PokemonFRLG_AutoStoryAbilities.cpp | 4 ++ .../PokemonFRLG_AutoStoryAbilities.h | 4 ++ .../PokemonFRLG_AutoStoryAbilities_Tests.cpp | 67 +++++++++++++++++++ .../AutoStory/PokemonFRLG_AutoStoryEvents.cpp | 2 +- .../AutoStory/PokemonFRLG_AutoStoryParty.cpp | 15 +++++ .../AutoStory/PokemonFRLG_AutoStoryParty.h | 6 ++ 6 files changed, 97 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.cpp index 1a217a5ac8..901c235886 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.cpp @@ -7,6 +7,7 @@ #include #include "CommonFramework/Exceptions/ProgramFinishedException.h" #include "PokemonFRLG_AutoStoryAbilities.h" +#include "PokemonFRLG_AutoStoryParty.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -31,6 +32,9 @@ Outcome teach(Logger& logger, const TeachPlan& plan, StoryAbilities& abilities){ logger.log(std::format("Party: {} is used by slot {} ({}).", plan.move, result.member + 1, species.empty() ? "species unread" : species)); abilities.party->users().record(plan.move, result.member, species); } + if (result.end == TeachEnd::LEARNED && result.member == 0 && abilities.lead != nullptr){ + learn_taught(plan.move, result.forgot, *abilities.lead); + } if (result.end == TeachEnd::LEARNED){ return Pokemon::outcome_ok(); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.h index df10a48840..80e75883b0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.h @@ -22,6 +22,7 @@ #include "PokemonFRLG_AutoStoryGrind.h" #include "PokemonFRLG_AutoStoryMart.h" #include "PokemonFRLG_AutoStoryMember.h" +#include "PokemonFRLG_AutoStoryPartyMember.h" #include "PokemonFRLG_AutoStoryPartyRoster.h" #include "PokemonFRLG_AutoStoryTeach.h" @@ -41,6 +42,9 @@ struct StoryAbilities{ SpeciesFinder species = {}; // Where a TEACH of a field move records who learned it; none records nothing. PartyCache* party = nullptr; + // The lead as the battles know it: a TEACH to party slot 1 puts the + // move in it (learn_taught()); none changes nothing. + PartyMember* lead = nullptr; }; // A MART, TEACH, LEVEL, GRIND or fossil step with `abilities`: nothing for diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp index 58d211460e..b832f421a7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryAbilities_Tests.cpp @@ -10,10 +10,13 @@ #include #include #include +#include +#include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" #include "CommonFramework/Exceptions/ProgramFinishedException.h" #include "PokemonFRLG_AutoStoryAbilities.h" +#include "PokemonFRLG_AutoStoryParty.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -259,6 +262,66 @@ UnitTestResult test_teach_records_fly_user(){ } +// A TM Case holding the TM, and a lead with four moves that forgets the +// one asked: its moves read after teaching hold the new one in its place. +class LearningTeach : public TeachActions{ +public: + explicit LearningTeach(std::vector moves) + : moves(std::move(moves)) + {} + virtual ItemOpen open_item(std::string_view, std::string_view move) override{ + m_move = move; + return ItemOpen::OPENED; + } + virtual TeachPrompt choose_member(size_t) override{ return TeachPrompt::DELETE_PROMPT; } + virtual ForgetEnd forget(size_t slot) override{ + moves[slot] = m_move; + return ForgetEnd::LEARNED; + } + virtual void stop_teaching() override{} + virtual bool leave() override{ return true; } + virtual void dump_screen(std::string_view) override{} + + std::vector moves; + +private: + std::string m_move; +}; + +// The battles' lead knows a move taught to party slot 1: console B taught +// Blizzard over Dig before the Silph rival (2026-11-15, 16.03 run 8), but +// the battle kept the moves read at the save's load and attacked Venusaur +// with Bite. A TEACH to another slot leaves the lead as it is. +UnitTestResult test_teach_updates_battle_lead(){ + StubGame game; + QuietLogger logger; + LearningTeach teach({"bite", "water-pulse", "surf", "dig"}); + const MemberReader members = [&teach](size_t slot) -> std::optional{ + return MemberInfo{.species = slot == 0 ? "blastoise" : "lapras", .level = 43, .moves = teach.moves}; + }; + PartyMember lead = member_from_summary( + "blastoise", 43, {{"bite", 25, 25}, {"water-pulse", 20, 20}, {"surf", 14, 15}, {"dig", 10, 10}}, std::nullopt + ); + StoryAbilities abilities{members, game.mart, teach, game.patch, game.fossils, {}, nullptr, &lead}; + const Pokemon::StoryStep step = teach_step("teach_blizzard", "", {.item = "tm14", .move = "blizzard", .member = 0, .forget_move = "dig"}); + run_ability_game_step(logger, nullptr, step, abilities); + if (lead.moves.size() != 4 || lead.moves[3].move != "blizzard" || lead.moves[3].pp != 5 || lead.moves[2].pp != 14){ + return "the lead's slot 4 isn't Blizzard at 5 PP, the others kept."; + } + constexpr size_t BLIZZARD = 3; + if (attack_slot_against(lead, {"venusaur"}) != BLIZZARD){ + return "expected Blizzard against Venusaur after the TEACH."; + } + LearningTeach other({"bite", "water-pulse", "surf", "blizzard"}); + StoryAbilities second{members, game.mart, other, game.patch, game.fossils, {}, nullptr, &lead}; + run_ability_game_step(logger, nullptr, teach_step("teach_slot2", "", {.item = "tm13", .move = "ice-beam", .member = 1, .forget_move = "surf"}), second); + if (lead.moves[2].move != "surf"){ + return "a TEACH to party slot 2 changed the lead's moves."; + } + return true; +} + + // A shiny in a GRIND stops the program, not the checkpoint: a reload // would lose it. UnitTestResult test_grind_shiny_stops(){ @@ -310,6 +373,10 @@ const AutoStoryAbilitiesCase AUTO_STORY_ABILITIES_CASES[] = { .input = {"a Fly after a TEACH reads no summary", test_teach_records_fly_user}, .expectation = {}, }, + { + .input = {"a TEACH to the lead changes the battles' lead", test_teach_updates_battle_lead}, + .expectation = {}, + }, { .input = {"the LEVEL step's args", test_level_args}, .expectation = {}, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index 7679104c95..e029891ae9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -572,7 +572,7 @@ std::optional run_abilities(StoryEnv& env, RunState& state, const Story [&]{ pace_trainer_battle(env, state, step, patch); } ); ConsoleChoiceActions choice(env.console, env.context); - StoryAbilities abilities{console_member_reader(env.console, env.context, env.language, StartMenu::FULL), mart, teach, grind, choice, console_species_finder(env.console, env.context, env.language, env.party), &env.party}; + StoryAbilities abilities{console_member_reader(env.console, env.context, env.language, StartMenu::FULL), mart, teach, grind, choice, console_species_finder(env.console, env.context, env.language, env.party), &env.party, &env.supplies.lead}; if (const std::optional plan = step.action == GRIND_CHECK_ACTION ? grind_plan_of(step.args) : std::nullopt){ if (grind_reached_already(env.console.logger(), grind, abilities.members, *plan)){ state.flags.insert(step.name); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty.cpp index ec8d7dc213..d273752dc0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty.cpp @@ -430,6 +430,21 @@ std::optional attack_slot_against(const PartyMember& member, const std:: return plan; } +void learn_taught(std::string_view move, std::string_view forgot, PartyMember& member){ + const KnownMove taught{.move = std::string(move), .pp = max_pp(move)}; + if (!forgot.empty()){ + for (KnownMove& known : member.moves){ + if (known.move == forgot){ + known = taught; + return; + } + } + } + if (member.moves.size() < MAX_MOVES && !knows(member, move)){ + member.moves.emplace_back(taught); + } +} + size_t attack_pp(const PartyMember& member){ size_t pp = 0; for (const KnownMove& known : member.moves){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty.h index e80f501fdc..ae16cd586d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty.h @@ -58,6 +58,12 @@ std::optional attack_slot(const PartyMember& member); // moves, attack_slot() itself. std::optional attack_slot_against(const PartyMember& member, const std::vector& foes); +// A TM, HM or tutor taught it `move` at full PP: in `forgot`'s slot, or +// in a free one when it forgot nothing. The game puts it there (console B, +// 2026-11-15: Blizzard over Dig in slot 4); the battles read the moves by +// name, so a lead that missed it attacked with Bite. +void learn_taught(std::string_view move, std::string_view forgot, PartyMember& member); + // The PP left in the damaging moves it knows. size_t attack_pp(const PartyMember& member); From aadd271561d74c6037d15bf641f6e2cd5f5b5db2 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 12:09:11 -0800 Subject: [PATCH 06/14] CI: pin CommandLineTests to 6e1d3bb (Silph Co. black-below frame) --- .github/workflows/cpp-ci-serial-programs-base.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index e4bf2aa700..ed28c076c2 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -387,7 +387,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). repository: 'dayfine/CommandLineTests' - ref: '47763e72e1de0489bfd91433a78f40e80f3cedf8' + ref: '6e1d3bb33a0365d295280fcce9a01194d0df01a9' path: 'CommandLineTests' # The unit tests assume the downloadable resources they need are already downloaded From b8d77053b82ac03e1c68744b08946d35d9b6d698 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 12:09:12 -0800 Subject: [PATCH 07/14] FRLG AutoStory: no evolution hold or battle on black below a map At 16.05 (console B, 2026-11-15) a Silph Co. grunt was beaten and the overworld at the map's edge, black below the player, passed for the battle's text box: BattleDialogDetector's blue test matches black. The loop held B for an evolution three times for 20 s with no START in between and stopped. The same black before each step of 16.02 read as a battle on its way (FrlgScreenObserver), and the loop looked for a battle for 15 s. The evolution check and the screen observer now read the box with its gold and white frame (BattleTextBoxDetector), which the evolution scene draws too (pret evolution_scene.c: LoadBattleTextboxAndBackground). After a hold that runs out no other one starts, so START looks for the overworld. --- .../AutoStory/PokemonFRLG_AutoStoryBattle.cpp | 3 +- .../PokemonFRLG_AutoStoryBattleDecisions.cpp | 3 +- .../PokemonFRLG_AutoStoryBattleDecisions.h | 7 +- .../PokemonFRLG_AutoStoryBattleTurns.h | 7 +- .../PokemonFRLG_AutoStoryItems_Tests.cpp | 28 ++++++++ .../PokemonFRLG_AutoStoryParty_Tests.cpp | 71 +++++++++++-------- .../PokemonFRLG_AutoStoryScreenEvents.cpp | 6 +- .../PokemonFRLG_AutoStoryScreenEvents.h | 10 ++- ...okemonFRLG_AutoStoryScreenEvents_Tests.cpp | 48 +++++++++---- 9 files changed, 132 insertions(+), 51 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp index ac4bfadc70..6740e219a5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp @@ -19,6 +19,7 @@ #include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleFlashWatcher.h" #include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleHpBarReader.h" #include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattlePokemonDetector.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.h" #include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.h" #include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_PartyMenuDetector.h" #include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.h" @@ -124,7 +125,7 @@ BattleScreen read_battle_screen(const ImageViewRGB32& screen, bool confirmed){ BattleTextScreen read_text_screen(const ImageViewRGB32& screen){ return BattleTextScreen{ - .text_box = BattleDialogDetector(COLOR_RED).detect(screen), + .text_box = BattleTextBoxDetector(COLOR_RED).detect(screen), .hp_box = BattleScreenDetector(COLOR_RED).detect(screen), .arrow = AdvanceBattleDialogDetector(COLOR_RED).detect(screen), .yes_no = battle_yes_no_shows(screen) || yes_no_cursor(screen).has_value(), diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp index c560f4ea94..c0347def72 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp @@ -495,11 +495,12 @@ void EvolutionHold::turn_taken(TurnChoice choice){ } bool EvolutionHold::holds(const BattleTextScreen& screen) const{ - return m_attacked && m_held < EVOLUTION_HOLDS && screen.text_box && !screen.hp_box && !screen.arrow && !screen.yes_no && !screen.dark_cave; + return m_attacked && !m_ran_out && m_held < EVOLUTION_HOLDS && screen.text_box && !screen.hp_box && !screen.arrow && !screen.yes_no && !screen.dark_cave; } void EvolutionHold::held(Milliseconds held, bool lead_fainted){ m_held += held; + m_ran_out |= held >= MAX_HOLD; // Only the lead fights, so only it evolves; once a battle. if (held < EVOLUTION_SEEN || m_evolved || lead_fainted){ return; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h index 47abb689f5..d5f159b323 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h @@ -164,7 +164,10 @@ class EvolutionHold{ // dark cave's overworld, whose black reads as the text box (console B, // 2026-11-09, 08.01 hiker_lenny: three holds, then a stop). Nothing // is pressed while it shows, for at most EVOLUTION_HOLDS in all in a - // battle, in case it is something else. + // battle, in case it is something else; and none after a hold that + // ran out, as an evolution's arrow shows well within one, so START + // looks for the overworld (console B, 2026-11-15, 16.05: three holds + // in Silph Co., then a stop). bool holds(const BattleTextScreen& screen) const; // Such a screen showed for `held` with nothing pressed: long enough, // and the lead evolved (unless it had fainted). @@ -174,6 +177,8 @@ class EvolutionHold{ PartyMember& m_lead; bool m_attacked = false; bool m_evolved = false; + // A hold lasted MAX_HOLD. + bool m_ran_out = false; Milliseconds m_held = Milliseconds::zero(); }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h index cfdfe56deb..38ac3de5c9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h @@ -73,7 +73,8 @@ enum class YesNo{ // A battle screen as the loop reads it, for the evolution check. struct BattleTextScreen{ - // The battle's text box. + // The battle's text box, its gold and white frame included: the + // black below a small map or at a map's edge passes for its blue alone. bool text_box = false; // The battle menu or an HP box. bool hp_box = false; @@ -86,6 +87,10 @@ struct BattleTextScreen{ bool dark_cave = false; }; +// `screen` as the battle loop reads it for the evolution check. +BattleTextScreen read_text_screen(const ImageViewRGB32& screen); + + // Answer to an in-battle prompt dialogue. struct PromptAnswer{ YesNo answer = YesNo::NO; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp index 60cd99391d..94f4516628 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp @@ -37,6 +37,7 @@ #include "PokemonFRLG_AutoStoryBattle.h" #include "PokemonFRLG_AutoStoryBattleDecisions.h" #include "PokemonFRLG_AutoStoryBattlePolicy.h" +#include "PokemonFRLG_AutoStoryBattleTurns.h" #include "PokemonFRLG_AutoStoryItemUse.h" #include "PokemonFRLG_AutoStoryMartPlan.h" #include "PokemonFRLG_AutoStoryNewMembers.h" @@ -514,6 +515,30 @@ class Test_RunRefused : public ImageUnitTest{ } }; +// Whether the battle loop holds B for an evolution after a turn that +// attacked, on console B's captures named _.png. +class Test_EvolutionHold : public ImageUnitTest{ +public: + Test_EvolutionHold(const std::string& image) + : ImageUnitTest("PokemonFRLG::AutoStoryBattle - " + image, {image}) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + bool expected = false; + const std::vector words = parse_words(Filesystem::Path(image_paths()[0]).stem().string()); + if (words.empty() || !parse_bool(words.back(), expected)){ + return "Error: filename must be _."; + } + Supplies supplies; + EvolutionHold hold(supplies.lead); + hold.turn_taken(TurnChoice::MOVE); + return hold.holds(read_text_screen(images[0])) == expected; + } +}; + // repel_wore_off_shows() on console B's captures named // _.png, in English. class Test_RepelWoreOff : public ImageUnitTest{ @@ -870,6 +895,9 @@ void add_tests_AutoStoryItems(UnitTestDatabase& database){ // which the loop sent for 10 minutes; and the battle menu a turn before. database.add("PokemonFRLG/AutoStoryBattle/Route21_SpencerNoRunning_True.png"); database.add("PokemonFRLG/AutoStoryBattle/Route21_SpencerMenu_False.png"); + // 16.05, Silph Co. (console B, 2026-11-15): black at the map's edge + // read as the battle's text box after a win; three holds, a stop. + database.add("PokemonFRLG/AutoStoryBattle/SilphCo_AfterWin_BlackBelow_False.png"); // 12.03 on Route 21 (console B, 2026-11-13 10:06 and 11:02): "REPEL's // effect wore off..." still being typed; Spencer's battle text isn't it. database.add("PokemonFRLG/AutoStoryRepel/Route21_RepelWoreOff_True.png"); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty_Tests.cpp index 12fc97e335..f9ce7bfacb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty_Tests.cpp @@ -608,45 +608,58 @@ UnitTestResult test_run_then_overworld(Logger&){ return "START never looked for the overworld in the dark cave."; } -// Regression: console B, 2026-11-09, 08.01 hiker_lenny in Rock Tunnel -// without Flash: the battle won with a move, then the dark cave's -// overworld, black but for the lit circle on the player, read as the -// text box with no HP box. The loop held B for an evolution three times -// for 20 s, and gave up: "no battle screen and no START menu for 60 s -// after the battle". A dark cave's overworld is no evolution: START looks -// for the overworld within seconds of the last HP box. -UnitTestResult test_trainer_win_then_dark_cave(Logger&){ +// The battle loop's polls after a trainer beaten with a move: the last +// HP box at 1.5 s, then `after` for a minute, each hold for an evolution +// running out. Empty if START looks for the overworld within 5 s of the +// last HP box and after at most `holds_allowed` holds. +std::string start_after_win(const BattleTextScreen& after, size_t holds_allowed){ const BattleTextScreen defeated{.text_box = true, .hp_box = true, .arrow = true, .yes_no = false}; - const BattleTextScreen dark_cave{.text_box = true, .hp_box = false, .arrow = false, .yes_no = false, .dark_cave = true}; - std::vector polls{{.ms = 0, .screen = defeated}}; - for (int ms = 300; ms <= 1500; ms += 300){ - polls.push_back({.ms = ms, .screen = defeated}); - } - for (int ms = 1800; ms <= 60000; ms += 300){ - polls.push_back({.ms = ms, .screen = dark_cave}); - } - Supplies supplies; EvolutionHold hold(supplies.lead); BattleEndWatch watch; const WallClock start = current_time(); hold.turn_taken(TurnChoice::MOVE); - for (const LoopPoll& poll : polls){ - const WallClock now = start + std::chrono::milliseconds(poll.ms); - if (hold.holds(poll.screen)){ - return std::format("Held B for an evolution at {} ms, on the dark cave's overworld.", poll.ms); + size_t holds = 0; + for (Milliseconds at(0); at <= Milliseconds(65000); at += Milliseconds(300)){ + const BattleTextScreen& screen = at <= Milliseconds(1500) ? defeated : after; + if (holds < holds_allowed + 1 && hold.holds(screen)){ + holds++; + hold.held(EvolutionHold::MAX_HOLD, false); + at += EvolutionHold::MAX_HOLD; + continue; } - watch.seen(poll.screen.hp_box || poll.screen.yes_no ? BattleSeen::BATTLE : BattleSeen::NONE, now); - if (watch.check_overworld(now)){ - if (poll.ms > 5000){ - return std::format("START first looked for the overworld at {} ms.", poll.ms); - } - return true; + if (holds > holds_allowed){ + return std::format("Held B for an evolution {} times.", holds); + } + watch.seen(screen.hp_box || screen.yes_no ? BattleSeen::BATTLE : BattleSeen::NONE, start + at); + if (watch.check_overworld(start + at)){ + const Milliseconds late = at - holds * EvolutionHold::MAX_HOLD; + return late > Milliseconds(5000) ? std::format("START first looked for the overworld at {} ms.", at.count()) : ""; } } - return "START never looked for the overworld in the dark cave."; + return "START never looked for the overworld."; } +// Regression: console B, 2026-11-09, 08.01 in Rock Tunnel without Flash, +// and 2026-11-15, 16.05 at Silph Co.'s map edge: a battle won, then black +// read as the text box with no HP box; three 20 s holds for an evolution, +// then "no battle screen and no START menu for 60 s after the battle". +// A dark cave is no evolution; after a hold that ran out, START. +UnitTestResult test_trainer_win_then_dark_cave(Logger&){ + const BattleTextScreen dark_cave{.text_box = true, .hp_box = false, .arrow = false, .yes_no = false, .dark_cave = true}; + const BattleTextScreen box_alone{.text_box = true, .hp_box = false, .arrow = false, .yes_no = false}; + const std::string cave = start_after_win(dark_cave, 0); + if (!cave.empty()){ + return std::format("Dark cave: {}", cave); + } + const std::string edge = start_after_win(box_alone, 1); + if (!edge.empty()){ + return std::format("Text box alone: {}", edge); + } + return true; +} + + // START only once the battle is gone for a while, and then not at every // poll; A and B blind for at most 30 s since the last battle menu. UnitTestResult test_end_watch_timing(Logger&){ @@ -954,7 +967,7 @@ const AutoStoryPartyTestCase AUTO_STORY_PARTY_CASES[] = { {.input = {.name = "summary members: fewer moves, unread PP, strongest attack", .body = test_summary_members}, .expectation = {.description = "handles party members with partial move data"},}, {.input = {.name = "no B during an evolution", .body = test_no_b_during_evolution}, .expectation = {.description = "prevents cancelling evolution sequence"},}, {.input = {.name = "RUN, then a dark cave: no evolution wait, START soon", .body = test_run_then_overworld}, .expectation = {.description = "handles post-battle overworld transition in dark cave"},}, - {.input = {.name = "a trainer beaten, then a dark cave without Flash: no evolution wait, START soon", .body = test_trainer_win_then_dark_cave}, .expectation = {.description = "takes a dark cave's overworld after a win for no evolution"},}, + {.input = {.name = "a trainer beaten, then a dark cave or black at a map's edge: START soon", .body = test_trainer_win_then_dark_cave}, .expectation = {.description = "takes a dark cave's overworld after a win for no evolution"},}, {.input = {.name = "battle end: START after the battle is gone, A and B for 30 s at most", .body = test_end_watch_timing}, .expectation = {.description = "verifies timing constraints on battle end loop"},}, {.input = {.name = "attacks by matchup: Bite on the rival's Bulbasaur and Misty", .body = test_attack_by_matchup}, .expectation = {.description = "selects optimal attack move based on type matchup"},}, {.input = {.name = "attacks against an unknown foe: the plan's order", .body = test_attack_unknown_foe}, .expectation = {.description = "falls back to default move preference order against unknown foe"},}, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryScreenEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryScreenEvents.cpp index 09d5d945a1..147b6a2bd8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryScreenEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryScreenEvents.cpp @@ -18,7 +18,7 @@ FrlgScreenObserver::FrlgScreenObserver() : m_dialog(COLOR_RED) , m_choice(COLOR_RED) , m_battle_menu(COLOR_RED) - , m_battle_dialog(COLOR_RED) + , m_battle_box(COLOR_RED) {} ScreenEvents FrlgScreenObserver::observe(const VideoSnapshot& frame, WallClock){ @@ -35,7 +35,7 @@ ScreenEvents FrlgScreenObserver::observe(const VideoSnapshot& frame, WallClock){ // A uniform screen has no box or menu to read. return out; } - const bool battle = m_battle_menu.detect(screen) || m_battle_dialog.detect(screen); + const bool battle = m_battle_menu.detect(screen) || m_battle_box.detect(screen); out.set(ScreenEvent::BATTLE_STARTED, battle); out.set(ScreenEvent::DIALOG_BOX, !battle && m_dialog.detect(screen)); out.set(ScreenEvent::CHOICE_BOX, !battle && m_choice.detect(screen)); @@ -46,7 +46,7 @@ void FrlgScreenObserver::make_overlays(VideoOverlaySet& items) const{ m_dialog.make_overlays(items); m_choice.make_overlays(items); m_battle_menu.make_overlays(items); - m_battle_dialog.make_overlays(items); + m_battle_box.make_overlays(items); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryScreenEvents.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryScreenEvents.h index 8cf7765d09..bb85442255 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryScreenEvents.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryScreenEvents.h @@ -13,6 +13,7 @@ #include "CommonTools/ScreenEvents/ScreenEvents.h" #include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_BattleDialogs.h" #include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -22,7 +23,8 @@ namespace PokemonFRLG{ // The shared events from FRLG's detectors: // - DIALOG_BOX: the overworld's white text box (WhiteDialogDetector); // - CHOICE_BOX: a Yes/No or answer box beside it (SelectionDialogDetector); -// - BATTLE_STARTED: the battle menu or a battle's text box; +// - BATTLE_STARTED: the battle menu or a battle's text box +// (BattleTextBoxDetector); // - FADE_BLACK / FADE_WHITE: fade_of_screen(). // Nothing on a dark cave's overworld (dark_cave_screen()), whose black // the battle text box detector reads as a box. @@ -40,7 +42,11 @@ class FrlgScreenObserver : public ScreenEventObserver{ WhiteDialogDetector m_dialog; SelectionDialogDetector m_choice; BattleMenuDetector m_battle_menu; - BattleDialogDetector m_battle_dialog; + // The box with its gold and white frame: the black below a small map + // or at a map's edge passes for BattleDialogDetector's box (console B, + // Silph Co., 2026-11-14 and 15: a 15 s look for a battle before + // each step). + BattleTextBoxDetector m_battle_box; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryScreenEvents_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryScreenEvents_Tests.cpp index 55ed120d4d..330d79b6c0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryScreenEvents_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryScreenEvents_Tests.cpp @@ -28,26 +28,41 @@ namespace{ struct ScreenEventsCase{ struct Input{ - std::string name; // The frame's colour; none for no frame. std::optional color; // The colour of a square in the middle of the frame, around the // player; 0 for none. uint32_t middle = 0; + // The colour of the frame from the battle text box's top down; + // 0 for none. + uint32_t below = 0; }; struct Expectation{ ScreenEvents events; }; + std::string name; Input input; Expectation expectation; }; +// `color` over the pixels from (x0, y0) up to (x1, y1); none if it is 0. +void paint(ImageRGB32& image, uint32_t color, size_t x0, size_t y0, size_t x1, size_t y1){ + if (color == 0){ + return; + } + for (size_t y = y0; y < y1; y++){ + for (size_t x = x0; x < x1; x++){ + image.pixel(x, y) = color; + } + } +} + class Test_AutoStoryScreenEvents : public UnitTest{ public: explicit Test_AutoStoryScreenEvents(ScreenEventsCase test_case) - : UnitTest(std::format("PokemonFRLG::AutoStoryScreenEvents - {}", test_case.input.name)) + : UnitTest(std::format("PokemonFRLG::AutoStoryScreenEvents - {}", test_case.name)) , m_case(std::move(test_case)) {} @@ -56,13 +71,8 @@ class Test_AutoStoryScreenEvents : public UnitTest{ if (m_case.input.color.has_value()){ ImageRGB32 image(1920, 1080); image.fill(*m_case.input.color); - if (m_case.input.middle != 0){ - for (size_t y = 380; y < 700; y++){ - for (size_t x = 800; x < 1120; x++){ - image.pixel(x, y) = m_case.input.middle; - } - } - } + paint(image, m_case.input.below, 0, 740, 1920, 1080); + paint(image, m_case.input.middle, 800, 380, 1120, 700); frame = VideoSnapshot(std::move(image), WallClock()); } FrlgScreenObserver observer; @@ -82,21 +92,33 @@ class Test_AutoStoryScreenEvents : public UnitTest{ const ScreenEventsCase SCREEN_EVENTS_CASES[] = { { - .input = {"no frame shows nothing", std::nullopt}, + .name = "no frame shows nothing", + .input = {.color = std::nullopt}, .expectation = {{}}, }, { - .input = {"a black screen is a black fade only", 0xff000000}, + .name = "a black screen is a black fade only", + .input = {.color = 0xff000000}, .expectation = {{ScreenEvent::FADE_BLACK}}, }, { - .input = {"a white screen is a white fade only", 0xffffffff}, + .name = "a white screen is a white fade only", + .input = {.color = 0xffffffff}, .expectation = {{ScreenEvent::FADE_WHITE}}, }, { // Rock Tunnel without Flash (console B, 2026-11-09, 08.01): its // black read as a battle's text box before each step. - .input = {"a dark cave, black but for a lit middle, shows nothing", 0xff000000, 0xff8c7c64}, + .name = "a dark cave, black but for a lit middle, shows nothing", + .input = {.color = 0xff000000, .middle = 0xff8c7c64}, + .expectation = {{}}, + }, + { + // Silph Co. (console B, 2026-11-14 16.02, 2026-11-15 16.05): the + // black at a map's edge read as a battle's text box before each + // step, and the battle loop looked for a battle for 15 s. + .name = "a lit map with black below, where the battle box would be, shows nothing", + .input = {.color = 0xff88c8d8, .below = 0xff000000}, .expectation = {{}}, }, }; From 4f17436251519140f57849927472255e3678fd7e Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 12:16:12 -0800 Subject: [PATCH 08/14] FRLG AutoStory: define read_text_screen outside the anonymous namespace (GCC: ambiguous overload) --- .../Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp index 6740e219a5..7152d82918 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp @@ -123,6 +123,8 @@ BattleScreen read_battle_screen(const ImageViewRGB32& screen, bool confirmed){ return BattleScreen::NONE; } +} // namespace + BattleTextScreen read_text_screen(const ImageViewRGB32& screen){ return BattleTextScreen{ .text_box = BattleTextBoxDetector(COLOR_RED).detect(screen), @@ -133,6 +135,8 @@ BattleTextScreen read_text_screen(const ImageViewRGB32& screen){ }; } +namespace{ + // The player's Pokemon gone while the opponent's is still there. bool starter_fainted(const ImageViewRGB32& screen){ return !BattlePokemonDetector(COLOR_RED).detect(screen) && BattleOpponentDetector(COLOR_RED).detect(screen); From a9b4540c97eb3544422bb17515df65a8202db5ae Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 13:11:07 -0800 Subject: [PATCH 09/14] FRLG AutoStory: evolution wait on a reading source; scripted-readings tests wait_out_evolution() reads through Pokemon::ReadingSource, so a test feeds it ScriptedReadings (shared, Pokemon/AutoStory) on a virtual clock. The battle loop's ConsoleTextReadings waits on the controller and reads a snapshot as before. New suites: Pokemon::ScriptedReadings and PokemonFRLG::AutoStoryEvolutionWait (text-black-overworld, an evolution, no frame mid-evolution, 16.05's map-edge black as the text box and as the frame check reads it); read_text_screen()'s text box on the 16.05 frame. --- .../AutoStory/Pokemon_AutoStory_Tests.h | 2 + .../AutoStory/Pokemon_ScriptedReadings.h | 98 ++++++++ .../Pokemon_ScriptedReadings_Tests.cpp | 120 +++++++++ .../Pokemon_ScriptedReadings_Tests.h | 23 ++ .../AutoStory/PokemonFRLG_AutoStoryBattle.cpp | 46 +++- .../PokemonFRLG_AutoStoryBattleDecisions.cpp | 14 ++ .../PokemonFRLG_AutoStoryBattleDecisions.h | 10 + ...kemonFRLG_AutoStoryEvolutionWait_Tests.cpp | 228 ++++++++++++++++++ .../PokemonFRLG_AutoStoryItems_Tests.cpp | 21 ++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 10 files changed, 555 insertions(+), 11 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index cbd7f23b13..7f74f32480 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -25,6 +25,7 @@ #include "Pokemon_FrameReplay_Tests.h" #include "Pokemon_Interrupts_Tests.h" #include "Pokemon_SceneJudges_Tests.h" +#include "Pokemon_ScriptedReadings_Tests.h" #include "Pokemon_StoryActions_Tests.h" #include "Pokemon_StoryEvents_Tests.h" #include "Pokemon_StoryFailureClasses_Tests.h" @@ -69,6 +70,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_Interrupts(database); add_tests_Transitions(database); add_tests_FrameReplay(database); + add_tests_ScriptedReadings(database); add_tests_StoryActions(database); add_tests_StoryInterrupts(database); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings.h new file mode 100644 index 0000000000..72dd501234 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings.h @@ -0,0 +1,98 @@ +/* Scripted Readings + * + * From: https://github.com/PokemonAutomation/ + * + * A loop that polls the screen, waits and reads it again, written against + * ReadingSource so a test feeds it scripted readings on a virtual clock + * instead of a console's frames. The program's source waits on the + * controller and reads a snapshot; ScriptedReadings shows each scripted + * reading for its time, and the last one after the script ends. + * + * A reading is whatever the loop decides on, read from one frame (a + * game's struct of detector results). FrameReplay (Pokemon_FrameReplay.h) + * replays recorded frames through the detectors instead; scripted + * readings test the loop's decisions without images. + * + */ + +#ifndef PokemonAutomation_Pokemon_ScriptedReadings_H +#define PokemonAutomation_Pokemon_ScriptedReadings_H + +#include +#include +#include +#include "Common/Cpp/Time.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Where a polling loop gets its readings, and its clock. +template +class ReadingSource{ +public: + virtual ~ReadingSource() = default; + + // Lets `duration` pass: nothing pressed. + virtual void wait(Milliseconds duration) = 0; + // The screen now; none if no frame came. + virtual std::optional read() = 0; + // The source's clock. + virtual WallClock now() const = 0; +}; + + +// One step of a script: `reading` for `lasts`. No reading: no frame. +template +struct ScriptedReading{ + Milliseconds lasts = Milliseconds(0); + std::optional reading = {}; +}; + +// Scripted readings on a virtual clock that only wait() moves. A reading +// shows from the end of the one before it for its `lasts`; the last one +// shows on after the script ends. An empty script reads no frame. +template +class ScriptedReadings : public ReadingSource{ +public: + ScriptedReadings(std::vector> script, WallClock origin) + : m_script(std::move(script)) + , m_origin(origin) + {} + + // How long the source has waited in all. + Milliseconds elapsed() const{ + return m_elapsed; + } + + virtual void wait(Milliseconds duration) override{ + m_elapsed += duration; + } + virtual std::optional read() override{ + Milliseconds end(0); + for (const ScriptedReading& step : m_script){ + end += step.lasts; + if (m_elapsed < end){ + return step.reading; + } + } + if (m_script.empty()){ + return std::nullopt; + } + return m_script.back().reading; + } + virtual WallClock now() const override{ + return m_origin + m_elapsed; + } + +private: + std::vector> m_script; + WallClock m_origin; + Milliseconds m_elapsed = Milliseconds(0); +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.cpp new file mode 100644 index 0000000000..5ec9b7cfb6 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.cpp @@ -0,0 +1,120 @@ +/* Scripted Readings Tests + * + * From: https://github.com/PokemonAutomation/ + * + * ScriptedReadings on integer readings: which one shows after each wait, + * frames missing, the last one past the script's end, and the clock. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon_ScriptedReadings.h" +#include "Pokemon_ScriptedReadings_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +using namespace std::chrono_literals; + +const WallClock T0 = WallClock() + std::chrono::hours(1); + +struct ScriptedReadingsInput{ + std::vector> script = {}; + // Waited before each read. + std::vector waits = {}; +}; + +struct ScriptedReadingsExpectation{ + // One per wait. + std::vector> readings = {}; +}; + +struct ScriptedReadingsCase{ + std::string name = {}; + ScriptedReadingsInput input = {}; + ScriptedReadingsExpectation expectation = {}; +}; + +std::string reading_name(const std::optional& reading){ + return reading ? std::to_string(*reading) : "no frame"; +} + +class Test_ScriptedReadings : public UnitTest{ +public: + explicit Test_ScriptedReadings(ScriptedReadingsCase test_case) + : UnitTest("Pokemon::ScriptedReadings - " + test_case.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + ScriptedReadings source(m_case.input.script, T0); + Milliseconds waited(0); + for (size_t c = 0; c < m_case.input.waits.size(); c++){ + source.wait(m_case.input.waits[c]); + waited += m_case.input.waits[c]; + const std::optional reading = source.read(); + if (reading != m_case.expectation.readings[c]){ + return std::format( + "at {} ms: expected {}, read {}", + waited.count(), reading_name(m_case.expectation.readings[c]), reading_name(reading) + ); + } + if (source.now() != T0 + waited || source.elapsed() != waited){ + return std::format("at {} ms: the clock is at {} ms", waited.count(), source.elapsed().count()); + } + } + return true; + } + +private: + ScriptedReadingsCase m_case; +}; + +const ScriptedReadingsCase SCRIPTED_READINGS_CASES[] = { + { + .name = "each reading for its time", + .input = {.script = {{100ms, 1}, {200ms, 2}}, .waits = {0ms, 99ms, 1ms, 199ms}}, + .expectation = {.readings = {1, 1, 2, 2}}, + }, + { + .name = "the last reading after the script ends", + .input = {.script = {{100ms, 1}, {100ms, 2}}, .waits = {200ms, 60000ms}}, + .expectation = {.readings = {2, 2}}, + }, + { + .name = "a step without a reading is no frame", + .input = {.script = {{100ms, 1}, {100ms, std::nullopt}, {100ms, 3}}, .waits = {0ms, 100ms, 100ms}}, + .expectation = {.readings = {1, std::nullopt, 3}}, + }, + { + .name = "an empty script reads no frame", + .input = {.script = {}, .waits = {0ms, 1000ms}}, + .expectation = {.readings = {std::nullopt, std::nullopt}}, + }, + { + .name = "reading again without a wait reads the same", + .input = {.script = {{100ms, 1}, {100ms, 2}}, .waits = {50ms, 0ms, 0ms}}, + .expectation = {.readings = {1, 1, 1}}, + }, +}; + +} // namespace + + +void add_tests_ScriptedReadings(UnitTestDatabase& database){ + for (const ScriptedReadingsCase& test_case : SCRIPTED_READINGS_CASES){ + database.add(test_case); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.h new file mode 100644 index 0000000000..ff01f9afc0 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.h @@ -0,0 +1,23 @@ +/* Scripted Readings Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_ScriptedReadings_Tests_H +#define PokemonAutomation_Pokemon_ScriptedReadings_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Scripted readings served on their virtual clock. +void add_tests_ScriptedReadings(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp index 7152d82918..38f3c2e26c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/ImageTools/ImageStats.h" @@ -318,6 +319,36 @@ void send_member(ProControllerContext& context, size_t slot){ } +// The screen as the evolution check reads it: a wait on the controller, +// then a snapshot through read_text_screen(). +class ConsoleTextReadings : public Pokemon::ReadingSource{ +public: + ConsoleTextReadings(ConsoleHandle& console, ProControllerContext& context) + : m_console(console) + , m_context(context) + {} + + virtual void wait(Milliseconds duration) override{ + pbf_wait(m_context, duration); + m_context.wait_for_all_requests(); + } + virtual std::optional read() override{ + VideoSnapshot screen = m_console.video().snapshot(); + if (!screen){ + return std::nullopt; + } + return read_text_screen(screen); + } + virtual WallClock now() const override{ + return current_time(); + } + +private: + ConsoleHandle& m_console; + ProControllerContext& m_context; +}; + + class BattleLoop{ public: BattleLoop( @@ -427,7 +458,7 @@ class BattleLoop{ return true; } if (m_evolution.holds(read_text_screen(screen))){ - wait_out_evolution(); + hold_for_evolution(); return true; } return false; @@ -450,17 +481,10 @@ class BattleLoop{ } // Nothing pressed while the screen may be an evolution, up to MAX_HOLD. - void wait_out_evolution(){ - const WallClock start = current_time(); - bool holds = true; - while (holds && current_time() - start < EvolutionHold::MAX_HOLD){ - pbf_wait(m_context, POLL); - m_context.wait_for_all_requests(); - VideoSnapshot screen = m_console.video().snapshot(); - holds = !screen || m_evolution.holds(read_text_screen(screen)); - } + void hold_for_evolution(){ + ConsoleTextReadings readings(m_console, m_context); + const Milliseconds held = wait_out_evolution(readings, m_evolution, POLL); const WallClock end = current_time(); - const Milliseconds held = std::chrono::duration_cast(end - start); if (held >= 2000ms){ m_console.log(std::format("Battle: no B for {} s, for an evolution.", held.count() / 1000)); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp index c0347def72..63e13f3bd5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "PokemonFRLG_AutoStoryBattleDecisions.h" #include "PokemonFRLG_AutoStoryHealing.h" @@ -509,6 +510,19 @@ void EvolutionHold::held(Milliseconds held, bool lead_fainted){ evolve(m_lead); } +Milliseconds wait_out_evolution( + Pokemon::ReadingSource& source, const EvolutionHold& hold, Milliseconds poll +){ + const WallClock start = source.now(); + bool holds = true; + while (holds && source.now() - start < EvolutionHold::MAX_HOLD){ + source.wait(poll); + const std::optional screen = source.read(); + holds = !screen || hold.holds(*screen); + } + return std::chrono::duration_cast(source.now() - start); +} + void BattleEndWatch::seen(BattleSeen what, WallClock now){ if (what != BattleSeen::NONE){ m_last_battle = now; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h index d5f159b323..9a04126075 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h @@ -27,6 +27,7 @@ #include #include "Common/Cpp/Time.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon/AutoStory/Pokemon_ScriptedReadings.h" #include "PokemonFRLG_AutoStoryBattle.h" #include "PokemonFRLG_AutoStoryBattlePolicy.h" #include "PokemonFRLG_AutoStoryParty.h" @@ -182,6 +183,13 @@ class EvolutionHold{ Milliseconds m_held = Milliseconds::zero(); }; +// Nothing pressed while `hold` holds for what `source` reads, a look +// every `poll`, up to EvolutionHold::MAX_HOLD; a look with no frame holds +// on. How long it held, for EvolutionHold::held(). +Milliseconds wait_out_evolution( + Pokemon::ReadingSource& source, const EvolutionHold& hold, Milliseconds poll +); + // What the battle loop saw of the battle at a poll. enum class BattleSeen{ @@ -270,6 +278,8 @@ class LeadFaintWatch{ // Registers AutoStory party management unit tests. void add_tests_AutoStoryParty(UnitTestDatabase& database); +// Registers the battle loop's evolution waits on scripted readings. +void add_tests_AutoStoryEvolutionWait(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp new file mode 100644 index 0000000000..8c6e5be710 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp @@ -0,0 +1,228 @@ +/* AutoStory Evolution Wait Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The battle loop after its last turn, on scripted readings + * (Pokemon_ScriptedReadings.h): wait_out_evolution() for each hold, B + * otherwise, and START once the battle is gone, as between_turns(), + * battle_gone() and no_battle_screen() in PokemonFRLG_AutoStoryBattle.cpp + * run them. Regression for 16.05 (console B, 2026-11-15): black at Silph + * Co.'s map edge read as the text box, three holds, then a stop. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon/AutoStory/Pokemon_ScriptedReadings.h" +#include "PokemonFRLG_AutoStoryBattleDecisions.h" +#include "PokemonFRLG_AutoStoryBattleTurns.h" +#include "PokemonFRLG_AutoStorySupplies.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +namespace{ + +using namespace std::chrono_literals; +using Pokemon::ScriptedReading; +using Pokemon::ScriptedReadings; + +const WallClock T0 = WallClock() + std::chrono::hours(1); +// As the battle loop's. +constexpr Milliseconds POLL = 300ms; +// How long a run may take before it counts as stuck. +constexpr Milliseconds RUN_LIMIT = 120000ms; + +// " fainted!" with its HP box. +const BattleTextScreen DEFEATED{.text_box = true, .hp_box = true, .arrow = true}; +// The screen blacked out between the battle and the map, or the black at +// a map's edge as the text box's frame check reads it: nothing. +const BattleTextScreen BLACK{}; +const BattleTextScreen OVERWORLD{}; +// "What? SQUIRTLE is evolving!" +const BattleTextScreen EVOLVING{.text_box = true}; +// "Congratulations! ..." with its arrow. +const BattleTextScreen CONGRATULATIONS{.text_box = true, .arrow = true}; +// The black at a map's edge as the blue-only check read it before the +// frame check: the text box with nothing else. +const BattleTextScreen EDGE_AS_BOX{.text_box = true}; + +// What the loop did after the last turn. +struct ExitRun{ + std::vector holds; + size_t b_presses = 0; + // When START opened the menu, from the start; none if it never did. + std::optional left = std::nullopt; +}; + +// The loop from the last turn (one that attacked) on `script`, START +// opening the menu from `overworld_from` on. +ExitRun run_exit(std::vector> script, Milliseconds overworld_from, Supplies& supplies){ + ScriptedReadings source(std::move(script), T0); + EvolutionHold hold(supplies.lead); + hold.turn_taken(TurnChoice::MOVE); + BattleEndWatch watch; + ExitRun run; + while (source.elapsed() < RUN_LIMIT){ + const std::optional screen = source.read(); + if (screen && hold.holds(*screen)){ + const Milliseconds held = wait_out_evolution(source, hold, POLL); + hold.held(held, false); + run.holds.push_back(held); + continue; + } + const bool battle = screen && (screen->hp_box || screen->yes_no); + watch.seen(battle ? BattleSeen::BATTLE : BattleSeen::NONE, source.now()); + if (watch.check_overworld(source.now()) && source.elapsed() >= overworld_from){ + run.left = source.elapsed(); + return run; + } + run.b_presses++; + source.wait(POLL); + } + return run; +} + + +struct EvolutionWaitInput{ + std::vector> script = {}; + // From when START opens the menu. + Milliseconds overworld_from = Milliseconds(0); +}; + +// A hold's length, within a poll or two. +struct HoldRange{ + Milliseconds min = Milliseconds(0); + Milliseconds max = Milliseconds(0); +}; + +struct EvolutionWaitExpectation{ + std::vector holds = {}; + // START opens the menu by then, from the start. + Milliseconds left_by = Milliseconds(0); + // Whether the loop took the lead for evolved; none: not checked. + std::optional evolved = std::nullopt; +}; + +struct EvolutionWaitCase{ + std::string name = {}; + EvolutionWaitInput input = {}; + EvolutionWaitExpectation expectation = {}; +}; + +UnitTestResult check_run(const ExitRun& run, const EvolutionWaitExpectation& expected, const Supplies& supplies){ + if (run.holds.size() != expected.holds.size()){ + return std::format("expected {} holds, got {}", expected.holds.size(), run.holds.size()); + } + for (size_t c = 0; c < run.holds.size(); c++){ + if (run.holds[c] < expected.holds[c].min || run.holds[c] > expected.holds[c].max){ + return std::format( + "hold {}: {} ms, expected {} to {} ms", + c + 1, run.holds[c].count(), expected.holds[c].min.count(), expected.holds[c].max.count() + ); + } + } + if (!run.left){ + return std::format("START never opened the menu in {} s ({} B presses)", RUN_LIMIT.count() / 1000, run.b_presses); + } + if (*run.left > expected.left_by){ + return std::format("START opened the menu at {} ms, expected by {} ms", run.left->count(), expected.left_by.count()); + } + const bool evolved = supplies.lead.species != "squirtle"; + if (expected.evolved && evolved != *expected.evolved){ + return std::format("expected the lead {}evolved, it is {}", *expected.evolved ? "" : "not ", supplies.lead.species); + } + return true; +} + +class Test_EvolutionWait : public UnitTest{ +public: + explicit Test_EvolutionWait(EvolutionWaitCase test_case) + : UnitTest("PokemonFRLG::AutoStoryEvolutionWait - " + test_case.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + Supplies supplies; + const ExitRun run = run_exit(m_case.input.script, m_case.input.overworld_from, supplies); + return check_run(run, m_case.expectation, supplies); + } + +private: + EvolutionWaitCase m_case; +}; + +const EvolutionWaitCase EVOLUTION_WAIT_CASES[] = { + { + .name = "text, black, overworld: B, then START", + .input = { + .script = {{1500ms, DEFEATED}, {1000ms, BLACK}, {60000ms, OVERWORLD}}, + .overworld_from = 2500ms, + }, + .expectation = {.holds = {}, .left_by = 5000ms, .evolved = false}, + }, + { + .name = "text, black, evolution: held through it, then START", + .input = { + .script = { + {1500ms, DEFEATED}, {600ms, BLACK}, {9000ms, EVOLVING}, + {1500ms, CONGRATULATIONS}, {600ms, BLACK}, {60000ms, OVERWORLD}, + }, + .overworld_from = 13200ms, + }, + .expectation = {.holds = {{8700ms, 9600ms}}, .left_by = 16500ms, .evolved = true}, + }, + { + .name = "a look with no frame holds on through an evolution", + .input = { + .script = { + {1500ms, DEFEATED}, {3000ms, EVOLVING}, {2000ms, std::nullopt}, {3000ms, EVOLVING}, + {1500ms, CONGRATULATIONS}, {60000ms, OVERWORLD}, + }, + .overworld_from = 11000ms, + }, + .expectation = {.holds = {{7700ms, 8600ms}}, .left_by = 14500ms, .evolved = true}, + }, + { + // 16.05: the map was there all along, black at its edge. + .name = "black at a map's edge read as the text box: one full hold, then START", + .input = { + .script = {{1500ms, DEFEATED}, {120000ms, EDGE_AS_BOX}}, + .overworld_from = 1500ms, + }, + .expectation = { + .holds = {{EvolutionHold::MAX_HOLD, EvolutionHold::MAX_HOLD + POLL}}, + .left_by = 1500ms + EvolutionHold::MAX_HOLD + 5000ms, + // A full hold counts as an evolution; not this test's point. + .evolved = std::nullopt, + }, + }, + { + .name = "black at a map's edge as the frame check reads it: no hold", + .input = { + .script = {{1500ms, DEFEATED}, {120000ms, BLACK}}, + .overworld_from = 1500ms, + }, + .expectation = {.holds = {}, .left_by = 5000ms, .evolved = false}, + }, +}; + +} // namespace + + +void add_tests_AutoStoryEvolutionWait(UnitTestDatabase& database){ + for (const EvolutionWaitCase& test_case : EVOLUTION_WAIT_CASES){ + database.add(test_case); + } +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp index 94f4516628..56765a71a1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp @@ -539,6 +539,25 @@ class Test_EvolutionHold : public ImageUnitTest{ } }; +// read_text_screen()'s text box on console B's captures. +class Test_ReadTextBox : public ImageUnitTest{ +public: + Test_ReadTextBox(const std::string& image, bool expected) + : ImageUnitTest("PokemonFRLG::AutoStoryBattle read_text_screen - " + image, {image}) + , m_expected(expected) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + return read_text_screen(images[0]).text_box == m_expected; + } + +private: + bool m_expected; +}; + // repel_wore_off_shows() on console B's captures named // _.png, in English. class Test_RepelWoreOff : public ImageUnitTest{ @@ -898,6 +917,7 @@ void add_tests_AutoStoryItems(UnitTestDatabase& database){ // 16.05, Silph Co. (console B, 2026-11-15): black at the map's edge // read as the battle's text box after a win; three holds, a stop. database.add("PokemonFRLG/AutoStoryBattle/SilphCo_AfterWin_BlackBelow_False.png"); + database.add("PokemonFRLG/AutoStoryBattle/SilphCo_AfterWin_BlackBelow_False.png", false); // 12.03 on Route 21 (console B, 2026-11-13 10:06 and 11:02): "REPEL's // effect wore off..." still being typed; Spencer's battle text isn't it. database.add("PokemonFRLG/AutoStoryRepel/Route21_RepelWoreOff_True.png"); @@ -908,6 +928,7 @@ void add_tests_AutoStoryItems(UnitTestDatabase& database){ database.add("PokemonFRLG/AutoStoryBattle/SSAnneRival_Raticate_FoeHp_7of48.png"); // The battle's choices, registered here to keep add_tests() within its size. add_tests_AutoStoryParty(database); + add_tests_AutoStoryEvolutionWait(database); add_tests_AutoStoryBattlePolicy(database); } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 9bfa1408d1..0ad870e4dd 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1658,6 +1658,9 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_SceneJudges.h Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.h + Source/Pokemon/AutoStory/Pokemon_ScriptedReadings.h + Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.h Source/Pokemon/AutoStory/Pokemon_StoryActions.cpp Source/Pokemon/AutoStory/Pokemon_StoryActions.h Source/Pokemon/AutoStory/Pokemon_StoryActions_Tests.cpp @@ -2195,6 +2198,7 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCheckpoints.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFrameReplay_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFrameReplay_Tests.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLegs.cpp From 47a54f5c81a04cb3b47a77a86cfed26d6f922f09 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 13:13:21 -0800 Subject: [PATCH 10/14] FRLG AutoStory: EvolutionHold::wait_out; console readings in Pokemon/AutoStory The console's ReadingSource is generic (a wait on the controller, a frame through a game's reader), so it moves to Pokemon_ConsoleReadings.h; the wait is a method of EvolutionHold, and the test suite has its own header. --- .../AutoStory/Pokemon_ConsoleReadings.h | 63 +++++++++++++++++++ .../Pokemon_ScriptedReadings_Tests.cpp | 12 ++-- .../AutoStory/PokemonFRLG_AutoStoryBattle.cpp | 36 +---------- .../PokemonFRLG_AutoStoryBattleDecisions.cpp | 10 ++- .../PokemonFRLG_AutoStoryBattleDecisions.h | 13 ++-- ...kemonFRLG_AutoStoryEvolutionWait_Tests.cpp | 15 ++--- ...PokemonFRLG_AutoStoryEvolutionWait_Tests.h | 25 ++++++++ .../PokemonFRLG_AutoStoryItems_Tests.cpp | 1 + SerialPrograms/cmake/SourceFiles.cmake | 2 + 9 files changed, 116 insertions(+), 61 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ConsoleReadings.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ConsoleReadings.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ConsoleReadings.h new file mode 100644 index 0000000000..2c72b06ab4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ConsoleReadings.h @@ -0,0 +1,63 @@ +/* Console Readings + * + * From: https://github.com/PokemonAutomation/ + * + * The program's ReadingSource (Pokemon_ScriptedReadings.h): a wait on the + * controller, then the console's latest frame through a game's reader. + * + */ + +#ifndef PokemonAutomation_Pokemon_ConsoleReadings_H +#define PokemonAutomation_Pokemon_ConsoleReadings_H + +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "Pokemon_ScriptedReadings.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// `reader` on the console's frames, on the wall clock. +template +class ConsoleReadings : public ReadingSource{ +public: + using Reader = Reading (*)(const ImageViewRGB32& screen); + + ConsoleReadings(NintendoSwitch::ConsoleHandle& console, NintendoSwitch::ProControllerContext& context, Reader reader) + : m_console(console) + , m_context(context) + , m_reader(reader) + {} + + virtual void wait(Milliseconds duration) override{ + NintendoSwitch::pbf_wait(m_context, duration); + m_context.wait_for_all_requests(); + } + virtual std::optional read() override{ + VideoSnapshot screen = m_console.video().snapshot(); + if (!screen){ + return std::nullopt; + } + return m_reader(screen); + } + virtual WallClock now() const override{ + return current_time(); + } + +private: + NintendoSwitch::ConsoleHandle& m_console; + NintendoSwitch::ProControllerContext& m_context; + Reader m_reader; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.cpp index 5ec9b7cfb6..82c7976b13 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.cpp @@ -27,20 +27,20 @@ using namespace std::chrono_literals; const WallClock T0 = WallClock() + std::chrono::hours(1); struct ScriptedReadingsInput{ - std::vector> script = {}; + std::vector> script; // Waited before each read. - std::vector waits = {}; + std::vector waits; }; struct ScriptedReadingsExpectation{ // One per wait. - std::vector> readings = {}; + std::vector> readings; }; struct ScriptedReadingsCase{ - std::string name = {}; - ScriptedReadingsInput input = {}; - ScriptedReadingsExpectation expectation = {}; + std::string name; + ScriptedReadingsInput input; + ScriptedReadingsExpectation expectation; }; std::string reading_name(const std::optional& reading){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp index 38f3c2e26c..e40499b8e7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp @@ -8,13 +8,13 @@ #include #include #include -#include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/ImageTools/ImageStats.h" #include "CommonFramework/Tools/DebugDumper.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "Pokemon/AutoStory/Pokemon_ConsoleReadings.h" #include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_BattleDialogs.h" #include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h" #include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleFlashWatcher.h" @@ -319,36 +319,6 @@ void send_member(ProControllerContext& context, size_t slot){ } -// The screen as the evolution check reads it: a wait on the controller, -// then a snapshot through read_text_screen(). -class ConsoleTextReadings : public Pokemon::ReadingSource{ -public: - ConsoleTextReadings(ConsoleHandle& console, ProControllerContext& context) - : m_console(console) - , m_context(context) - {} - - virtual void wait(Milliseconds duration) override{ - pbf_wait(m_context, duration); - m_context.wait_for_all_requests(); - } - virtual std::optional read() override{ - VideoSnapshot screen = m_console.video().snapshot(); - if (!screen){ - return std::nullopt; - } - return read_text_screen(screen); - } - virtual WallClock now() const override{ - return current_time(); - } - -private: - ConsoleHandle& m_console; - ProControllerContext& m_context; -}; - - class BattleLoop{ public: BattleLoop( @@ -482,8 +452,8 @@ class BattleLoop{ // Nothing pressed while the screen may be an evolution, up to MAX_HOLD. void hold_for_evolution(){ - ConsoleTextReadings readings(m_console, m_context); - const Milliseconds held = wait_out_evolution(readings, m_evolution, POLL); + Pokemon::ConsoleReadings readings(m_console, m_context, read_text_screen); + const Milliseconds held = m_evolution.wait_out(POLL, readings); const WallClock end = current_time(); if (held >= 2000ms){ m_console.log(std::format("Battle: no B for {} s, for an evolution.", held.count() / 1000)); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp index 63e13f3bd5..80943c4292 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp @@ -510,15 +510,13 @@ void EvolutionHold::held(Milliseconds held, bool lead_fainted){ evolve(m_lead); } -Milliseconds wait_out_evolution( - Pokemon::ReadingSource& source, const EvolutionHold& hold, Milliseconds poll -){ +Milliseconds EvolutionHold::wait_out(Milliseconds poll, Pokemon::ReadingSource& source) const{ const WallClock start = source.now(); - bool holds = true; - while (holds && source.now() - start < EvolutionHold::MAX_HOLD){ + bool still = true; + while (still && source.now() - start < MAX_HOLD){ source.wait(poll); const std::optional screen = source.read(); - holds = !screen || hold.holds(*screen); + still = !screen || holds(*screen); } return std::chrono::duration_cast(source.now() - start); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h index 9a04126075..06e10490bd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h @@ -173,6 +173,10 @@ class EvolutionHold{ // Such a screen showed for `held` with nothing pressed: long enough, // and the lead evolved (unless it had fainted). void held(Milliseconds held, bool lead_fainted); + // Nothing pressed while it holds for what `source` reads, a look every + // `poll`, up to MAX_HOLD; a look with no frame holds on. How long it + // held, for held(). + Milliseconds wait_out(Milliseconds poll, Pokemon::ReadingSource& source) const; private: PartyMember& m_lead; @@ -183,13 +187,6 @@ class EvolutionHold{ Milliseconds m_held = Milliseconds::zero(); }; -// Nothing pressed while `hold` holds for what `source` reads, a look -// every `poll`, up to EvolutionHold::MAX_HOLD; a look with no frame holds -// on. How long it held, for EvolutionHold::held(). -Milliseconds wait_out_evolution( - Pokemon::ReadingSource& source, const EvolutionHold& hold, Milliseconds poll -); - // What the battle loop saw of the battle at a poll. enum class BattleSeen{ @@ -278,8 +275,6 @@ class LeadFaintWatch{ // Registers AutoStory party management unit tests. void add_tests_AutoStoryParty(UnitTestDatabase& database); -// Registers the battle loop's evolution waits on scripted readings. -void add_tests_AutoStoryEvolutionWait(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp index 8c6e5be710..8cff87d5e9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp @@ -3,7 +3,7 @@ * From: https://github.com/PokemonAutomation/ * * The battle loop after its last turn, on scripted readings - * (Pokemon_ScriptedReadings.h): wait_out_evolution() for each hold, B + * (Pokemon_ScriptedReadings.h): EvolutionHold::wait_out() for each hold, B * otherwise, and START once the battle is gone, as between_turns(), * battle_gone() and no_battle_screen() in PokemonFRLG_AutoStoryBattle.cpp * run them. Regression for 16.05 (console B, 2026-11-15): black at Silph @@ -21,6 +21,7 @@ #include "Pokemon/AutoStory/Pokemon_ScriptedReadings.h" #include "PokemonFRLG_AutoStoryBattleDecisions.h" #include "PokemonFRLG_AutoStoryBattleTurns.h" +#include "PokemonFRLG_AutoStoryEvolutionWait_Tests.h" #include "PokemonFRLG_AutoStorySupplies.h" namespace PokemonAutomation{ @@ -72,7 +73,7 @@ ExitRun run_exit(std::vector> script, Millisec while (source.elapsed() < RUN_LIMIT){ const std::optional screen = source.read(); if (screen && hold.holds(*screen)){ - const Milliseconds held = wait_out_evolution(source, hold, POLL); + const Milliseconds held = hold.wait_out(POLL, source); hold.held(held, false); run.holds.push_back(held); continue; @@ -91,7 +92,7 @@ ExitRun run_exit(std::vector> script, Millisec struct EvolutionWaitInput{ - std::vector> script = {}; + std::vector> script; // From when START opens the menu. Milliseconds overworld_from = Milliseconds(0); }; @@ -103,7 +104,7 @@ struct HoldRange{ }; struct EvolutionWaitExpectation{ - std::vector holds = {}; + std::vector holds; // START opens the menu by then, from the start. Milliseconds left_by = Milliseconds(0); // Whether the loop took the lead for evolved; none: not checked. @@ -111,9 +112,9 @@ struct EvolutionWaitExpectation{ }; struct EvolutionWaitCase{ - std::string name = {}; - EvolutionWaitInput input = {}; - EvolutionWaitExpectation expectation = {}; + std::string name; + EvolutionWaitInput input; + EvolutionWaitExpectation expectation; }; UnitTestResult check_run(const ExitRun& run, const EvolutionWaitExpectation& expected, const Supplies& supplies){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.h new file mode 100644 index 0000000000..3b71ce0ac7 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.h @@ -0,0 +1,25 @@ +/* AutoStory Evolution Wait Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryEvolutionWait_Tests_H +#define PokemonAutomation_PokemonFRLG_AutoStoryEvolutionWait_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The battle loop's evolution holds after the last turn, on scripted readings. +void add_tests_AutoStoryEvolutionWait(UnitTestDatabase& database); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp index 56765a71a1..3ced64d171 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp @@ -38,6 +38,7 @@ #include "PokemonFRLG_AutoStoryBattleDecisions.h" #include "PokemonFRLG_AutoStoryBattlePolicy.h" #include "PokemonFRLG_AutoStoryBattleTurns.h" +#include "PokemonFRLG_AutoStoryEvolutionWait_Tests.h" #include "PokemonFRLG_AutoStoryItemUse.h" #include "PokemonFRLG_AutoStoryMartPlan.h" #include "PokemonFRLG_AutoStoryNewMembers.h" diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 0ad870e4dd..39f0508f97 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1643,6 +1643,7 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.h + Source/Pokemon/AutoStory/Pokemon_ConsoleReadings.h Source/Pokemon/AutoStory/Pokemon_FadeProfiles.h Source/Pokemon/AutoStory/Pokemon_FrameReplay.cpp Source/Pokemon/AutoStory/Pokemon_FrameReplay.h @@ -2199,6 +2200,7 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFrameReplay_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFrameReplay_Tests.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLegs.cpp From 06f0233af54e0f175f31f8b77a651b82f8c26e8f Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 13:13:43 -0800 Subject: [PATCH 11/14] FRLG AutoStory tests: std::format for the new test names --- .../Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.cpp | 4 ++-- .../AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp | 2 +- .../Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.cpp index 82c7976b13..d2f434ad52 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScriptedReadings_Tests.cpp @@ -44,13 +44,13 @@ struct ScriptedReadingsCase{ }; std::string reading_name(const std::optional& reading){ - return reading ? std::to_string(*reading) : "no frame"; + return reading ? std::format("{}", *reading) : "no frame"; } class Test_ScriptedReadings : public UnitTest{ public: explicit Test_ScriptedReadings(ScriptedReadingsCase test_case) - : UnitTest("Pokemon::ScriptedReadings - " + test_case.name) + : UnitTest(std::format("Pokemon::ScriptedReadings - {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp index 8cff87d5e9..eb9caf50ea 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp @@ -145,7 +145,7 @@ UnitTestResult check_run(const ExitRun& run, const EvolutionWaitExpectation& exp class Test_EvolutionWait : public UnitTest{ public: explicit Test_EvolutionWait(EvolutionWaitCase test_case) - : UnitTest("PokemonFRLG::AutoStoryEvolutionWait - " + test_case.name) + : UnitTest(std::format("PokemonFRLG::AutoStoryEvolutionWait - {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp index 3ced64d171..bf1a30af1b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryItems_Tests.cpp @@ -544,7 +544,7 @@ class Test_EvolutionHold : public ImageUnitTest{ class Test_ReadTextBox : public ImageUnitTest{ public: Test_ReadTextBox(const std::string& image, bool expected) - : ImageUnitTest("PokemonFRLG::AutoStoryBattle read_text_screen - " + image, {image}) + : ImageUnitTest(std::format("PokemonFRLG::AutoStoryBattle read_text_screen - {}", image), {image}) , m_expected(expected) {} From 5b9be5e844912aa7dce640aab379a9e8d0b5c5f6 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 13:23:25 -0800 Subject: [PATCH 12/14] FRLG AutoStory sim: walker from the believed facing; 16.05 president walk test OverworldSimWalker::run takes the facing the program believes, as the console's walk_route() does. The new Silph case walks below_president from where Giovanni's battle leaves the player, (5, 15) facing east (SilphCo_11F_Movement_PlayerFaceGiovanni), from the giovanni step's declared pose. Red: that pose says north, so the first hold turns the player and the walk ends off (console B, 2026-11-15 13:02, 16.05). --- .../Overworld/PokemonFRLG_OverworldSim.cpp | 4 +- .../Overworld/PokemonFRLG_OverworldSim.h | 13 ++++- .../PokemonFRLG_AutoStorySilph_Tests.cpp | 58 +++++++++++++++++++ 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp index 2393cf8600..fe734dc20b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp @@ -994,10 +994,10 @@ RouteCalls OverworldSimWalker::calls(){ return calls; } -std::string OverworldSimWalker::run(const std::vector& route, const RoutePlanning& planning){ +std::string OverworldSimWalker::run(const std::vector& route, const RoutePlanning& planning, std::optional believed_facing){ m_at_known = planning.start.has_value(); try{ - run_route(route, m_sim.state().facing, &planning, calls()); + run_route(route, believed_facing.value_or(m_sim.state().facing), &planning, calls()); }catch (OperationFailedException& e){ return e.message(); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h index 868feb065a..959790383b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h @@ -440,9 +440,16 @@ class OverworldSimWalker{ // console's does. RouteCalls calls(); - // run_route() from the sim's facing, planned with `planning`. Returns - // the failure's message, or empty if the route was walked. - std::string run(const std::vector& route, const RoutePlanning& planning); + // run_route() from the facing the program believes, `believed_facing` + // (the sim's own if none), planned with `planning`. A belief that is + // not the sim's facing walks as the console did: the first hold turns + // the player and loses a tile (console B, 2026-11-15 13:02, 16.05 + // below_president). Returns the failure's message, or empty if the + // route was walked. + std::string run( + const std::vector& route, const RoutePlanning& planning, + std::optional believed_facing = std::nullopt + ); const std::vector& log() const{ return m_log; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp index 6119734cad..6fc145a939 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp @@ -381,6 +381,60 @@ UnitTestResult test_giovanni_row(){ return true; } +// 16.05's step named `name`; none if it has none. +const StoryStep* giovanni_row_step(std::string_view name){ + for (const StoryStep& step : checkpoint("16.05").steps){ + if (step.name == name){ + return &step; + } + } + return nullptr; +} + +// 16.05 below_president from where Giovanni's battle leaves the player: +// (5, 15) facing east, to Giovanni, who walked down to (6, 15) (pokefirered +// data/maps/SilphCo_11F/scripts.inc, SilphCo_11F_Movement_GiovanniApproachLeft +// and _PlayerFaceGiovanni). The walk is planned from the giovanni step's +// declared pose. With that pose facing north, "north 5" was held with no +// turn first: the hold turned the player, went 4 tiles to (5, 11), and +// "east 4" stopped on the table at (6, 11); the walker found the player +// again at the wrong tile and ended below the table, where the president's +// talk opened no message (console B, 2026-11-15 13:02, 16.05). +UnitTestResult test_president_walk_after_giovanni(){ + const StoryStep* giovanni = giovanni_row_step("giovanni"); + const StoryStep* below = giovanni_row_step("below_president"); + if (giovanni == nullptr || below == nullptr || !giovanni->pose_after){ + return "16.05 has no giovanni step with a pose after it, or no below_president."; + } + const StoryPose at = *story_pose(Pokemon::pose_of(*giovanni->pose_after)); + const std::optional target = story_pose(Pokemon::pose_of(Pokemon::story_arg(below->args, "target"))); + if (!at.facing || !target){ + return "16.05: the giovanni step's pose has no facing, or below_president has no target."; + } + const std::set flags{std::string(SILPH_CLEARED_FLAG)}; + const std::vector& maps = run_story_maps(flags); + const MapPosition start{at.map, at.tile}; + const std::optional> route = plan_overworld_route( + maps, start, RouteGoal{{target->map, target->tile}, target->facing}, story_route_costs(), *at.facing + ); + if (!route || route->empty()){ + return std::format("No route from {} to the president.", to_string(start)); + } + OverworldSim sim(maps, MapPosition{"SilphCo_11F", {5, 15}}, Direction::EAST); + const RoutePlanning planning{.maps = &maps, .costs = story_route_costs(), .start = start, .label = "below_president"}; + if (const std::string failure = OverworldSimWalker(sim).run(*route, planning, at.facing); !failure.empty()){ + return std::format("16.05: ended on {}; {}", to_string(sim.state().position), failure); + } + const MapPosition president_talk{"SilphCo_11F", {9, 10}}; + if (!(sim.state().position == president_talk) || sim.state().facing != Direction::NORTH){ + return std::format( + "16.05: ended on {} facing {}, not {} facing north.", + to_string(sim.state().position), direction_name(sim.state().facing), to_string(president_talk) + ); + } + return true; +} + // 16.06: the walk back down to Saffron's Center on the maps with no Rocket in // Silph Co. or Saffron City. UnitTestResult test_return_row(){ @@ -632,6 +686,10 @@ const AutoStorySilphCase AUTO_STORY_SILPH_CASES[] = { .input = {.name = "16.05 11F's Card Key door and Giovanni", .body = test_giovanni_row}, .expectation = {.description = "one Rocket fought, 11F's door opened, ends below the president"}, }, + { + .input = {.name = "16.05 to the president from where Giovanni's battle leaves the player", .body = test_president_walk_after_giovanni}, + .expectation = {.description = "from (5, 15) facing east, ends on (9, 10) facing north"}, + }, { .input = {.name = "16.06 back to Saffron's Center with no Rockets", .body = test_return_row}, .expectation = {.description = "no trainer met, ends below the Center's door"}, From 1a81337cc1f76e4f8a948bc75f26baef9fcf7335 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 13:23:49 -0800 Subject: [PATCH 13/14] FRLG AutoStory 16.05: Giovanni's battle leaves the player facing east He walks down to (6, 15) and the player turns to him (SilphCo_11F_Movement_PlayerFaceGiovanni). With the pose saying north, below_president held "north 5" with no turn tap: the hold turned the player and went 4 tiles, "east 4" stopped on the table and the talk to the president opened no message (console B, 2026-11-15 13:02). --- .../Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden16.h | 2 +- .../Programs/AutoStory/StoryData/frlg-16.05.plan.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden16.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden16.h index 09eacc435c..094198e143 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden16.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden16.h @@ -74,7 +74,7 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN_16[] = { "16.05 walk east 3 -> SilphCo_11F (5, 18)", "16.05 open gate north -> SilphCo_11F (5, 18)", "16.05 walk north 2 -> SilphCo_11F (5, 16)", - "16.05 below_president from SilphCo_11F (5, 15) facing north to SilphCo_11F (9, 10) facing north", + "16.05 below_president from SilphCo_11F (5, 15) facing east to SilphCo_11F (9, 10) facing north", "16.05 walk north 5 -> SilphCo_11F (5, 10)", "16.05 walk east 4 -> SilphCo_11F (9, 10)", "16.05 face north -> SilphCo_11F (9, 10)", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.05.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.05.plan.json index 18b63286e2..95e4dda500 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.05.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.05.plan.json @@ -120,12 +120,12 @@ 15 ] ], - "facing": "north" + "facing": "east" }, "flags_after": [ "silph_cleared" ], - "why": "Giovanni (TRAINER_BOSS_GIOVANNI_2): Nidorino Lv37, Kangaskhan Lv35, Rhyhorn Lv37, Nidoqueen Lv41; then he and the Rockets leave Silph Co. and Saffron City (FLAG_HIDE_SAFFRON_ROCKETS), the story's maps from here have none" + "why": "Giovanni (TRAINER_BOSS_GIOVANNI_2): Nidorino Lv37, Kangaskhan Lv35, Rhyhorn Lv37, Nidoqueen Lv41; then he and the Rockets leave Silph Co. and Saffron City (FLAG_HIDE_SAFFRON_ROCKETS), the story's maps from here have none. He walks down to (6, 15) and the player turns east to him (SilphCo_11F_Movement_GiovanniApproachLeft, _PlayerFaceGiovanni), so the battle leaves the player facing east" }, { "id": "below_president", From 1ab56ca76f1e4aaf0792ce35b35c302a64552093 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 13:55:13 -0800 Subject: [PATCH 14/14] FRLG AutoStory battle: stop at once when the lead faints When the game asks for the next member ("Choose a POKeMON." with the lead's HP bar empty), the battle loop sends none and returns BattleEnd::LEAD_FAINTED; a trainer step fails with "lead_fainted" (class battle_lost), and the checkpoint reloads under its attempts. Console B, 2026-11-15, 16.05: Blastoise fainted to Giovanni and the loop pressed B at the party list for 60 s, then failed as operation_failed. The party list is read first at every poll, battle confirmed or not, so a step run again on that screen stops too. Sending the next member (next_member_slot) is gone. Tests: a scripted-readings case (fainted text, then the party list: a stop within a poll, no hold) and a frame test on the 16.05 capture; CommandLineTests pin moved to 5ea4c10. --- .../workflows/cpp-ci-serial-programs-base.yml | 4 +- .../AutoStory/PokemonFRLG_AutoStoryBattle.cpp | 99 +++++++++++-------- .../AutoStory/PokemonFRLG_AutoStoryBattle.h | 17 ++-- .../PokemonFRLG_AutoStoryBattleDecisions.cpp | 13 --- .../PokemonFRLG_AutoStoryBattleDecisions.h | 13 +-- .../PokemonFRLG_AutoStoryBattleTurns.h | 9 +- .../AutoStory/PokemonFRLG_AutoStoryEvents.cpp | 4 +- ...kemonFRLG_AutoStoryEvolutionWait_Tests.cpp | 72 +++++++++++++- ...PokemonFRLG_AutoStoryEvolutionWait_Tests.h | 3 +- .../PokemonFRLG_AutoStoryFailureClasses.cpp | 1 + ...emonFRLG_AutoStoryFailureClasses_Tests.cpp | 1 + .../AutoStory/PokemonFRLG_AutoStoryGrind.cpp | 11 ++- .../PokemonFRLG_AutoStoryParty_Tests.cpp | 32 ++---- .../PokemonFRLG_AutoStorySlopeRun.cpp | 4 + 14 files changed, 184 insertions(+), 99 deletions(-) diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index ed28c076c2..54fd7032fb 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -385,9 +385,9 @@ jobs: uses: actions/checkout@v7 if: inputs.run-tests 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 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, the Pidgey known-moves frame, and the Silph Co. fainted-lead party list). repository: 'dayfine/CommandLineTests' - ref: '6e1d3bb33a0365d295280fcce9a01194d0df01a9' + ref: '5ea4c10bc1ef518a69395f12d6c53207a7f8a75c' path: 'CommandLineTests' # The unit tests assume the downloadable resources they need are already downloaded diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp index e40499b8e7..dd6ab16052 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp @@ -13,6 +13,7 @@ #include "CommonFramework/ImageTools/ImageStats.h" #include "CommonFramework/Tools/DebugDumper.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Images/SolidColorTest.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "Pokemon/AutoStory/Pokemon_ConsoleReadings.h" #include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_BattleDialogs.h" @@ -75,6 +76,21 @@ constexpr Milliseconds MOVE_LIST_TIMEOUT = 5000ms; // badge left of the HP numbers. The box bobs by a pixel; this keeps a // pixel inside. const ImageFloatBox STATUS_BADGE_BOX(146.0 / 240, 99.0 / 160, 12.0 / 240, 4.0 / 160); +// In the party list, the left end of the lead's HP bar: its color with any +// HP left, empty grey (121, 121, 121) at none (console B, 2026-11-15, 16.05). +const ImageFloatBox PARTY_LEAD_HP_BAR_LEFT_BOX(32.2 / 240, 60.2 / 160, 1.6 / 240, 1.6 / 160); + +// The party list with the lead's HP bar empty: in a battle, the lead +// fainted and the game asks for the next member ("Choose a POKeMON."). +bool party_lead_fainted(const ImageViewRGB32& screen){ + if (!PartyMenuDetector(COLOR_RED).detect(screen)){ + return false; + } + const ImageViewRGB32 game = extract_box_reference(screen, GameSettings::instance().GAME_BOX); + const ImageStats bar = image_stats(extract_box_reference(game, PARTY_LEAD_HP_BAR_LEFT_BOX)); + // Grey: not the bar's colors, nor its white edge or dark frame. + return is_solid(bar, {0.3333, 0.3333, 0.3334}, 0.1, 20) && bar.average.sum() > 300 && bar.average.sum() < 450; +} enum class BattleScreen{ @@ -133,6 +149,7 @@ BattleTextScreen read_text_screen(const ImageViewRGB32& screen){ .arrow = AdvanceBattleDialogDetector(COLOR_RED).detect(screen), .yes_no = battle_yes_no_shows(screen) || yes_no_cursor(screen).has_value(), .dark_cave = OverworldWalkSteps::dark_cave_screen(screen), + .lead_fainted = party_lead_fainted(screen), }; } @@ -308,16 +325,6 @@ TurnResult item_result(bool used){ return used ? TurnResult::DONE : TurnResult::NOT_DONE; } -// The party list in battle opens on the lead: down to `slot`, then A, and -// SHIFT, the top of its menu. -void send_member(ProControllerContext& context, size_t slot){ - for (size_t c = 0; c < slot; c++){ - pbf_press_dpad(context, DPAD_DOWN, 160ms, 400ms); - } - pbf_press_button(context, BUTTON_A, 160ms, 800ms); - pbf_press_button(context, BUTTON_A, 160ms, 1500ms); -} - class BattleLoop{ public: @@ -352,6 +359,9 @@ class BattleLoop{ m_console.log(std::format( "Battle: over after {} turns{}.", m_decisions.turns(), m_decisions.lost() ? ", the starter fainted" : "" )); + if (m_lead_fainted){ + return BattleEnd::LEAD_FAINTED; + } return m_decisions.lost() ? BattleEnd::LOST : BattleEnd::WON; } @@ -366,15 +376,15 @@ class BattleLoop{ pbf_wait(m_context, POLL); return false; } - if (!m_confirmed && BattleScreenDetector(COLOR_RED).detect(screen)){ - m_console.log("Battle: the battle menu or an HP box shows."); - m_confirmed = true; + const BattleTextScreen text = read_text_screen(screen); + if (text.lead_fainted){ + stop_for_fainted_lead(screen); + return true; } - if (!m_confirmed && now - m_start > NOT_A_BATTLE){ - m_console.log("Battle: no battle menu or HP box in 15 s: not a battle.", COLOR_ORANGE); + if (not_a_battle(screen, now)){ return true; } - if (between_turns(screen, now)){ + if (between_turns(screen, text, now)){ return false; } if (battle_gone(screen, now)){ @@ -406,10 +416,22 @@ class BattleLoop{ return false; } - // The screens between the turns that need more than B: "no PP left", - // the party list once the lead fainted, and an evolution after the - // battle. True if one was handled. - bool between_turns(const ImageViewRGB32& screen, WallClock now){ + // No battle menu or HP box in NOT_A_BATTLE: no battle. One confirms it. + bool not_a_battle(const ImageViewRGB32& screen, WallClock now){ + if (!m_confirmed && BattleScreenDetector(COLOR_RED).detect(screen)){ + m_console.log("Battle: the battle menu or an HP box shows."); + m_confirmed = true; + } + if (!m_confirmed && now - m_start > NOT_A_BATTLE){ + m_console.log("Battle: no battle menu or HP box in 15 s: not a battle.", COLOR_ORANGE); + return true; + } + return false; + } + + // The screens between the turns that need more than B: "no PP left" + // and an evolution after the battle. True if one was handled. + bool between_turns(const ImageViewRGB32& screen, const BattleTextScreen& text, WallClock now){ if (!m_confirmed){ return false; } @@ -423,31 +445,23 @@ class BattleLoop{ m_last_press = now; return true; } - if (m_decisions.lost() && PartyMenuDetector(COLOR_RED).detect(screen)){ - send_next_member(now); - return true; - } - if (m_evolution.holds(read_text_screen(screen))){ + if (m_evolution.holds(text)){ hold_for_evolution(); return true; } return false; } - // The party list after the lead fainted: the next member allowed in, or - // a stop with only protected members left. - void send_next_member(WallClock now){ - const std::optional slot = m_decisions.next_member_slot(); - if (!slot){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::NO_ERROR_REPORT, - "fight_battle(): the starter fainted and only protected members are left; not sending one.", - m_console - ); - } - m_console.log(std::format("Battle: the lead fainted; sending party slot {}.", *slot + 1), COLOR_ORANGE); - send_member(m_context, *slot); - m_last_press = now; + // The party list after the lead fainted, looked for first at each poll: + // no member is sent, and the battle stops for the checkpoint to reload + // (the user's rule; console B, 2026-11-15, 16.05: 60 s of B there). + void stop_for_fainted_lead(const ImageViewRGB32& screen){ + m_console.log(std::format( + "Battle: the lead fainted (turn {}) and the game asks for the next member; sending none.", m_decisions.turns() + ), COLOR_RED); + dump_debug_image(m_console, "AutoStory/battle", std::format("turn{}-lead-fainted", m_decisions.turns()), screen); + m_decisions.lead_fainted(); + m_lead_fainted = true; } // Nothing pressed while the screen may be an evolution, up to MAX_HOLD. @@ -705,6 +719,8 @@ class BattleLoop{ bool m_move_chosen = false; // RUN was chosen and no battle menu has shown since. bool m_ran = false; + // The party list showed with the lead fainted: the loop stopped there. + bool m_lead_fainted = false; }; @@ -756,6 +772,11 @@ void fail_on_blackout(ConsoleHandle& console, BattleEnd end){ ErrorReportMode::NO_ERROR_REPORT, "Story: blackout: the starter fainted in a battle on the way.", console ); } + if (end == BattleEnd::LEAD_FAINTED){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::NO_ERROR_REPORT, "Story: lead_fainted: the starter fainted in a battle on the way.", console + ); + } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h index 4d35ab056a..ab872c2c32 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h @@ -57,10 +57,13 @@ enum class BattleKind{ // Terminal state of a battle encounter. enum class BattleEnd{ WON, - // The starter fainted: a blackout, or a wild battle run from (or won) - // by the next member, except in the lab's rival battle, which the - // story goes on from. + // The starter fainted: a blackout, or a wild battle run from, except + // in the lab's rival battle, which the story goes on from. LOST, + // The starter fainted and the game asks for the next member: none is + // sent, and the loop stops there, in the battle, for the checkpoint to + // fail and reload its save (the user's rule, 2026-11-15). + LEAD_FAINTED, }; @@ -90,8 +93,9 @@ bool battle_shows(const ImageViewRGB32& screen); // `kind` says, using `supplies` as needed. Returns once START opens the // menu on the overworld: text after the battle (a gym leader's speech) is // read with B on the way. After 30 s of A and B for a screen nothing reads, -// returns anyway, for the walk to find the player. Throws -// OperationFailedException if it runs over 10 minutes. +// returns anyway, for the walk to find the player. Returns LEAD_FAINTED at +// once, at the party list, if the lead faints and the game asks for the +// next member. Throws OperationFailedException if it runs over 10 minutes. // THOROUGH: an item's name is read in the bag before it is used, and a // different one isn't used. BattleEnd fight_battle( @@ -104,7 +108,8 @@ BattleEnd fight_battle( // The interrupts for walking story routes. Wild battles are run from, or // fought if `fight_wild`. A message is advanced until the overworld is // back; a battle that starts after it (a trainer who saw the player) is -// fought. A battle lost (a blackout) throws OperationFailedException. +// fought. A battle lost (a blackout, or the lead fainted) throws +// OperationFailedException. OverworldInterruptSet story_interrupts(Supplies& supplies, bool fight_wild, Pokemon::StoryCheckLevel checks, Language language); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp index 80943c4292..5205a4ab9a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp @@ -21,7 +21,6 @@ namespace PokemonFRLG{ namespace{ -constexpr size_t PARTY_SLOTS = 6; // A wild Pokemon is fought (WILD_FIGHT) only with green HP and this many // Potions left; otherwise it is run from. constexpr int MIN_POTIONS_TO_FIGHT = 2; @@ -443,18 +442,6 @@ PromptAnswer BattleDecisions::prompt(bool lead_box_gone){ return PromptAnswer{.answer = YesNo::NO}; } -std::optional BattleDecisions::next_member_slot(){ - lead_fainted(); - // A slot sent before and still offered was empty or fainted: the next. - for (size_t slot = 1; slot < PARTY_SLOTS; slot++){ - const bool kept = std::ranges::find(m_supplies.protected_slots, slot) != m_supplies.protected_slots.end(); - if (!kept && !m_sent.contains(slot)){ - m_sent.insert(slot); - return slot; - } - } - return std::nullopt; -} void BattleDecisions::lead_fainted(){ m_lost = true; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h index 06e10490bd..b6c5db6125 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.h @@ -9,10 +9,10 @@ * * The party is the lead (the starter) and members that don't fight: the * lead is never switched out. If it faints, a wild battle is run from - * ("Use next POKEMON?" NO); a trainer's, or a failed run, sends the next - * member not in Supplies::protected_slots, which only runs or uses its - * first move; with only protected ones left, the program stops instead. - * The battle counts as lost either way. + * ("Use next POKEMON?" NO); when the game asks for the next member (a + * trainer's battle, or a failed run), the battle loop sends none and + * stops, for the checkpoint to reload (BattleEnd::LEAD_FAINTED). The + * battle counts as lost either way. * */ @@ -68,9 +68,6 @@ class BattleDecisions{ // is left as it was. PromptAnswer prompt(bool lead_box_gone); - // The party list opened with the lead fainted: the slot to send in, - // or none if every member left is protected. - std::optional next_member_slot(); // The lead's box gone for good: it fainted. void lead_fainted(); @@ -139,8 +136,6 @@ class BattleDecisions{ // The highest level a move offered can be for, from the lead's level // read before the battle; none if it wasn't read. std::optional m_level_cap; - // The members sent in after the lead fainted. - std::set m_sent; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h index 38ac3de5c9..41be5829ba 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h @@ -71,7 +71,8 @@ enum class YesNo{ NO, }; -// A battle screen as the loop reads it, for the evolution check. +// A battle screen as the loop reads it, for the evolution check and a +// fainted lead. struct BattleTextScreen{ // The battle's text box, its gold and white frame included: the // black below a small map or at a map's edge passes for its blue alone. @@ -85,9 +86,13 @@ struct BattleTextScreen{ // The overworld of a cave dark without Flash (dark_cave_screen()), // whose black reads as the text box. bool dark_cave = false; + // The party list with the lead's HP bar empty: the game asks for the + // next member after the lead fainted. + bool lead_fainted = false; }; -// `screen` as the battle loop reads it for the evolution check. +// `screen` as the battle loop reads it for the evolution check and a +// fainted lead. BattleTextScreen read_text_screen(const ImageViewRGB32& screen); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index e029891ae9..6cbca764fa 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -280,8 +280,8 @@ Outcome battle(StoryEnv& env, const StoryStep& step){ const BattleKind kind = arg(step, "run") ? BattleKind::WILD_RUN : BattleKind::TRAINER; const BattleEnd end = fight_battle(env.console, env.context, env.supplies, kind, env.checks, env.language); end_trainer_battle(env.supplies); - if (end == BattleEnd::LOST && !arg(step, "may_lose")){ - return Pokemon::outcome_failed("blackout"); + if (end == BattleEnd::LEAD_FAINTED || (end == BattleEnd::LOST && !arg(step, "may_lose"))){ + return Pokemon::outcome_failed(end == BattleEnd::LEAD_FAINTED ? "lead_fainted" : "blackout"); } mash_until_overworld(env.console, env.context, SCENE_TIMEOUT); // A trainer's script can go on after the battle, its black while the diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp index eb9caf50ea..eaaaf44092 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.cpp @@ -7,7 +7,12 @@ * otherwise, and START once the battle is gone, as between_turns(), * battle_gone() and no_battle_screen() in PokemonFRLG_AutoStoryBattle.cpp * run them. Regression for 16.05 (console B, 2026-11-15): black at Silph - * Co.'s map edge read as the text box, three holds, then a stop. + * Co.'s map edge read as the text box, three holds, then a stop. And the + * party list after the lead fainted, which ends the loop at once, as + * step() stops for it first: on 16.05 (console B, 2026-11-15 13:42) + * Blastoise fainted to Giovanni and the loop pressed B at "Choose a + * POKeMON." for 60 s. read_text_screen() reads that party list on the + * capture. * */ @@ -18,7 +23,9 @@ #include #include "Common/Cpp/TestRunners/UnitTest.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" #include "Pokemon/AutoStory/Pokemon_ScriptedReadings.h" +#include "Tests/ImageUnitTest.h" #include "PokemonFRLG_AutoStoryBattleDecisions.h" #include "PokemonFRLG_AutoStoryBattleTurns.h" #include "PokemonFRLG_AutoStoryEvolutionWait_Tests.h" @@ -53,6 +60,12 @@ const BattleTextScreen CONGRATULATIONS{.text_box = true, .arrow = true}; // The black at a map's edge as the blue-only check read it before the // frame check: the text box with nothing else. const BattleTextScreen EDGE_AS_BOX{.text_box = true}; +// "BLASTOISE fainted!" with the foe's HP box. +const BattleTextScreen LEAD_FAINTED_TEXT{.text_box = true, .hp_box = true, .arrow = true}; +// The party list, "Choose a POKeMON.", the lead's HP bar empty; its white +// text box taken for the battle's, so it would hold for an evolution if +// the fainted lead didn't come first. +const BattleTextScreen PARTY_LEAD_FAINTED{.text_box = true, .lead_fainted = true}; // What the loop did after the last turn. struct ExitRun{ @@ -60,6 +73,9 @@ struct ExitRun{ size_t b_presses = 0; // When START opened the menu, from the start; none if it never did. std::optional left = std::nullopt; + // When the loop stopped for a fainted lead, from the start; none if + // it didn't. + std::optional lead_fainted = std::nullopt; }; // The loop from the last turn (one that attacked) on `script`, START @@ -72,6 +88,10 @@ ExitRun run_exit(std::vector> script, Millisec ExitRun run; while (source.elapsed() < RUN_LIMIT){ const std::optional screen = source.read(); + if (screen && screen->lead_fainted){ + run.lead_fainted = source.elapsed(); + return run; + } if (screen && hold.holds(*screen)){ const Milliseconds held = hold.wait_out(POLL, source); hold.held(held, false); @@ -109,6 +129,9 @@ struct EvolutionWaitExpectation{ Milliseconds left_by = Milliseconds(0); // Whether the loop took the lead for evolved; none: not checked. std::optional evolved = std::nullopt; + // The loop stops for a fainted lead by then, from the start, and + // START never opens the menu; none: it never stops for one. + std::optional lead_fainted_by = std::nullopt; }; struct EvolutionWaitCase{ @@ -129,6 +152,18 @@ UnitTestResult check_run(const ExitRun& run, const EvolutionWaitExpectation& exp ); } } + if (expected.lead_fainted_by){ + if (!run.lead_fainted){ + return std::format("expected a stop for the fainted lead by {} ms; none ({} B presses)", expected.lead_fainted_by->count(), run.b_presses); + } + if (*run.lead_fainted > *expected.lead_fainted_by){ + return std::format("stopped for the fainted lead at {} ms, expected by {} ms", run.lead_fainted->count(), expected.lead_fainted_by->count()); + } + return true; + } + if (run.lead_fainted){ + return std::format("stopped for a fainted lead at {} ms", run.lead_fainted->count()); + } if (!run.left){ return std::format("START never opened the menu in {} s ({} B presses)", RUN_LIMIT.count() / 1000, run.b_presses); } @@ -211,6 +246,35 @@ const EvolutionWaitCase EVOLUTION_WAIT_CASES[] = { }, .expectation = {.holds = {}, .left_by = 5000ms, .evolved = false}, }, + { + // 16.05: Blastoise fainted to Giovanni at turn 7. + .name = "the lead fainted, then the party list: a stop at once, no hold", + .input = { + .script = {{2500ms, LEAD_FAINTED_TEXT}, {1000ms, BLACK}, {120000ms, PARTY_LEAD_FAINTED}}, + .overworld_from = RUN_LIMIT, + }, + .expectation = {.holds = {}, .left_by = RUN_LIMIT, .lead_fainted_by = 3500ms + POLL}, + }, +}; + + +// read_text_screen()'s fainted lead on console B's captures. +class Test_ReadLeadFainted : public ImageUnitTest{ +public: + Test_ReadLeadFainted(const std::string& image, bool expected) + : ImageUnitTest(std::format("PokemonFRLG::AutoStoryBattle lead fainted - {}", image), {image}) + , m_expected(expected) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + return read_text_screen(images[0]).lead_fainted == m_expected; + } + +private: + bool m_expected; }; } // namespace @@ -220,6 +284,12 @@ void add_tests_AutoStoryEvolutionWait(UnitTestDatabase& database){ for (const EvolutionWaitCase& test_case : EVOLUTION_WAIT_CASES){ database.add(test_case); } + // 16.05, Silph Co. (console B, 2026-11-15 13:42): the party list after + // Blastoise fainted to Giovanni, its HP bar empty; a party list with + // the lead's HP left; and a battle menu. + database.add("PokemonFRLG/AutoStoryBattle/SilphCo_GiovanniLeadFainted_True.png", true); + database.add("PokemonFRLG/Party/Party_1.png", false); + database.add("PokemonFRLG/AutoStoryBattle/Route21_SpencerMenu_False.png", false); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.h index 3b71ce0ac7..204d09bbbd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvolutionWait_Tests.h @@ -14,7 +14,8 @@ namespace NintendoSwitch{ namespace PokemonFRLG{ -// The battle loop's evolution holds after the last turn, on scripted readings. +// The battle loop's evolution holds and its stop for a fainted lead after +// the last turn, on scripted readings; and its read of that party list. void add_tests_AutoStoryEvolutionWait(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFailureClasses.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFailureClasses.cpp index eb045b3251..2a34ad4b6b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFailureClasses.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFailureClasses.cpp @@ -61,6 +61,7 @@ const std::map>& frlg_failure_n {"pc_failed", C::MENU_FAILED}, // Battles and what interrupts a step. {"blackout", C::BATTLE_LOST}, + {"lead_fainted", C::BATTLE_LOST}, {"dialog", C::INTERRUPT_FAILED}, {"encounter_requires_recovery", C::WILD_BATTLES}, // The row sim's own checks (sim runs only). diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFailureClasses_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFailureClasses_Tests.cpp index 3981bdccdf..e3bec978b1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFailureClasses_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFailureClasses_Tests.cpp @@ -41,6 +41,7 @@ const std::vector NAME_CASES = { {.name = "FRLG's own name", .input = {.failure = "no_pose"}, .expectation = {.failure_class = StoryFailureClass::POSE_LOST}}, {.name = "a trainer never seen", .input = {.failure = "no_trainer"}, .expectation = {.failure_class = StoryFailureClass::SCREEN_UNREAD}}, {.name = "a blackout", .input = {.failure = "blackout"}, .expectation = {.failure_class = StoryFailureClass::BATTLE_LOST}}, + {.name = "the lead fainted", .input = {.failure = "lead_fainted"}, .expectation = {.failure_class = StoryFailureClass::BATTLE_LOST}}, {.name = "a family by its prefix", .input = {.failure = "mart_no_money"}, .expectation = {.failure_class = StoryFailureClass::MENU_FAILED}}, {.name = "a Fly failure", .input = {.failure = "fly_no_map"}, .expectation = {.failure_class = StoryFailureClass::MENU_FAILED}}, {.name = "an exact name over its prefix", .input = {.failure = "mart_cancel"}, .expectation = {.failure_class = StoryFailureClass::SIM_MISMATCH}}, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryGrind.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryGrind.cpp index a8ea6aac2d..e71903279f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryGrind.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryGrind.cpp @@ -268,7 +268,16 @@ EncounterSeen ConsoleGrindActions::read_encounter(){ GrindFight ConsoleGrindActions::fight(){ try{ const BattleEnd end = fight_battle(m_console, m_context, m_supplies, BattleKind::WILD_FIGHT, m_checks, m_language); - return end == BattleEnd::WON ? GrindFight::OVER : GrindFight::LOST; + switch (end){ + case BattleEnd::WON: + return GrindFight::OVER; + case BattleEnd::LOST: + return GrindFight::LOST; + case BattleEnd::LEAD_FAINTED: + m_console.log("Grind: the lead fainted and the game asks for the next member.", COLOR_RED); + return GrindFight::FAILED; + } + return GrindFight::FAILED; }catch (OperationFailedException& e){ m_console.log("Grind: " + e.message(), COLOR_RED); return GrindFight::FAILED; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty_Tests.cpp index f9ce7bfacb..1f3b76cd1d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty_Tests.cpp @@ -207,32 +207,17 @@ UnitTestResult test_prompt_answers(Logger&){ return true; } -// The lead fainted: a wild battle is run from, and a member sent only -// runs; in a trainer's, the next member is sent and uses its first move. -// Protected members are never sent: with only those left there is no -// slot, and the loop stops. Either way the -// battle is lost, and no item goes to the fainted lead. +// The lead fainted: a wild battle is run from ("Use next POKEMON?" NO), +// and whoever is out after it only runs; in a trainer's, only uses its +// first move. Either way the battle is lost, and no item goes to the +// fainted lead. UnitTestResult test_fainted_lead(Logger&){ - Supplies protected_party; - protected_party.protected_slots = {1, 2, 3, 4, 5}; - BattleDecisions only_protected(protected_party, BattleKind::WILD_FIGHT); - only_protected.turn(GREEN); - if (only_protected.prompt(true).answer != YesNo::NO || !only_protected.lost()){ - return "Expected NO to \"Use next POKEMON?\" in a wild battle, and the battle lost."; - } - if (only_protected.next_member_slot()){ - return "Expected no member sent with every member after the lead protected."; - } - - // Slot 1 protected, the rest not. Supplies wild_supplies; wild_supplies.potions = 3; - wild_supplies.protected_slots = {1}; BattleDecisions wild(wild_supplies, BattleKind::WILD_FIGHT); wild.turn(GREEN); - wild.prompt(true); - if (wild.next_member_slot() != std::optional(2) || wild.next_member_slot() != std::optional(3)){ - return "Expected slot 2 past the protected slot 1, then slot 3 if 2 couldn't go."; + if (wild.prompt(true).answer != YesNo::NO || !wild.lost()){ + return "Expected NO to \"Use next POKEMON?\" in a wild battle, and the battle lost."; } const PartyStatus red{.hp_seen = true, .hp_yellow = false, .hp_red = true, .status = StatusCondition::NONE}; for (int c = 0; c < 10; c++){ @@ -244,8 +229,9 @@ UnitTestResult test_fainted_lead(Logger&){ Supplies trainer_supplies; BattleDecisions trainer(trainer_supplies, BattleKind::TRAINER); trainer.turn(GREEN); - if (trainer.next_member_slot() != std::optional(1) || !trainer.lost()){ - return "Expected the member after the lead sent in a trainer's battle, and the battle lost."; + trainer.lead_fainted(); + if (!trainer.lost()){ + return "Expected the battle lost once the lead fainted in a trainer's battle."; } const TurnAction action = trainer.turn(GREEN); if (action.choice != TurnChoice::MOVE || action.slot != TOP_LEFT_MOVE){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp index 188f5c8ccb..a5df044d02 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp @@ -120,6 +120,10 @@ Pokemon::Outcome ride(SlopeEnv& env, const StoryStep& step, const std::vector