From 0ea932152fed29b04fcf37f162ab306b192f8f33 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 23:04:09 -0800 Subject: [PATCH] Recover the boss from the summary when header read is not a boss Prevent an OCR misread from making a non-boss as boss during boss battle. - Leave the boss empty in header read if not GlobalState::opponent_fits_stage() in process_header_read - Retry the OCR in summary read, which makes a valid boss string more likely - accept_opponent() then only records a valid boss - boss_if_caught() returns the boss only after the fourth win - If the boss is still empty, throw (same as before) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Framework/PokemonSwSh_MaxLair_State.cpp | 31 +++--- .../Framework/PokemonSwSh_MaxLair_State.h | 13 ++- .../PokemonSwSh_MaxLair_State_Tests.cpp | 95 ++++++++++++++++--- .../PokemonSwSh_MaxLair_Run_Battle.cpp | 18 +--- 4 files changed, 117 insertions(+), 40 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.cpp b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.cpp index 53150be1bf..b00840ef4a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.cpp @@ -24,24 +24,33 @@ bool is_boss_battle(const GlobalState& state){ } // namespace -bool GlobalState::accept_opponent(){ +bool GlobalState::opponent_fits_stage() const{ const std::string opponent_slug = opponent.empty() ? "" : *opponent.begin(); - // A boss before the end means something is out-of-sync. - if (!is_boss_battle(*this) && is_boss(opponent_slug)){ - return false; + if (!is_boss_battle(*this)){ + return !is_boss(opponent_slug); + } + if (!boss.empty()){ + return opponent.size() == 1 && opponent_slug == boss; + } + // A rental read here is a misread (Kyurem read as Mimikyu, 2026-11-02). + return opponent.size() == 1 && is_boss(opponent_slug); +} + +bool GlobalState::accept_opponent(){ + const bool fits = opponent_fits_stage(); + if (!is_boss_battle(*this)){ + // A boss before the end means something is out-of-sync. + return fits; } - // Infer the boss if we don't know it. Only a single boss species counts: - // a rental read here is a misread (Kyurem read as Mimikyu, 2026-11-02) - // and must not become the boss. A later turn's read can still record it. - if (is_boss_battle(*this) && boss.empty() && opponent.size() == 1 && is_boss(opponent_slug)){ - boss = opponent_slug; + if (boss.empty() && fits){ + boss = *opponent.begin(); } return true; } std::string GlobalState::boss_if_caught() const{ - // The identified boss is reported whether or not it was caught. - return boss; + // The boss is caught only if the fourth battle was won. + return wins == 4 ? boss : ""; } std::string PlayerState::dump() const{ diff --git a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h index 4b34bfecec..e1ec843268 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h @@ -172,10 +172,14 @@ struct GlobalState{ size_t find_player_index(size_t console_id) const; void clear_battle_state(); - // Returns false when the opponent read doesn't fit this battle stage (a - // boss before the fourth battle), so the caller retries the read. On an - // accepted read at the fourth battle, records the boss if none is known - // and the read is a single boss species. + // Whether the opponent read fits this battle stage: before the fourth + // battle, any non-boss; at the fourth battle, exactly the known boss, or a + // single boss species when no boss is known yet. + bool opponent_fits_stage() const; + + // Returns false when the read doesn't fit a battle before the fourth (a + // boss there), so the caller retries the read. At the fourth battle, + // records the boss if none is known and the read fits. bool accept_opponent(); // The same boss, but only if the adventure caught it, else empty: for the @@ -183,7 +187,6 @@ struct GlobalState{ // the boss-caught rules after a loss. Decided from the battles won (the // boss is the fourth battle), not from how many Pokemon were caught, so it // stays right if non-boss catches are ever skipped. - // For now this returns boss whatever the battles won. std::string boss_if_caught() const; std::string dump() const; diff --git a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.cpp index fbd162aa6e..fd6651d6ee 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.cpp @@ -68,7 +68,6 @@ class Test_MaxLairBossState : public UnitTest{ BossStateTestCase m_scenario; }; -// Cases marked TODO record the current, wrong behaviour. const BossStateTestCase CASES[] = { { .input = {.name = "rental before three wins", .wins = 2, .opponent = {"mimikyu-disguised"}, .boss = "", .final_wins = 3}, @@ -86,12 +85,11 @@ const BossStateTestCase CASES[] = { // is already known is kept. { .input = {.name = "Kyurem after Mimikyu misidentification", .wins = 3, .opponent = {"kyurem"}, .boss = "mimikyu-disguised", .final_wins = 3}, - .expectation = {.accepted = true, .boss = "mimikyu-disguised", .boss_if_caught = "mimikyu-disguised"}, + .expectation = {.accepted = true, .boss = "mimikyu-disguised", .boss_if_caught = ""}, }, - // TODO: to be fixed: the boss is reported caught without a fourth win. { .input = {.name = "identified boss lost", .wins = 3, .opponent = {"kyurem"}, .boss = "kyurem", .final_wins = 3}, - .expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = "kyurem"}, + .expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = ""}, }, { .input = {.name = "identified boss caught", .wins = 3, .opponent = {"kyurem"}, .boss = "", .final_wins = 4}, @@ -101,7 +99,6 @@ const BossStateTestCase CASES[] = { .input = {.name = "boss before three wins must retry", .wins = 2, .opponent = {"kyurem"}, .boss = "", .final_wins = 2}, .expectation = {.accepted = false, .boss = "", .boss_if_caught = ""}, }, - // TODO: to be fixed: a boss stage with no opponent read is accepted. { .input = {.name = "empty boss-stage read", .wins = 3, .opponent = {}, .boss = "", .final_wins = 3}, .expectation = {.accepted = true, .boss = "", .boss_if_caught = ""}, @@ -110,24 +107,100 @@ const BossStateTestCase CASES[] = { .input = {.name = "ambiguous boss-stage read is not the boss", .wins = 3, .opponent = {"kyurem", "mimikyu-disguised"}, .boss = "", .final_wins = 3}, .expectation = {.accepted = true, .boss = "", .boss_if_caught = ""}, }, - // TODO: to be fixed: a rental read at the boss stage is accepted, and the boss is reported caught without a fourth win. { .input = {.name = "rental read after known boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "kyurem", .final_wins = 3}, - .expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = "kyurem"}, + .expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = ""}, + }, +}; + +struct FitsStageTestCaseInput{ + std::string name; + uint8_t wins; + std::set opponent; + std::string boss; +}; + +struct FitsStageTestCaseExpectation{ + bool fits; +}; + +struct FitsStageTestCase{ + FitsStageTestCaseInput input; + FitsStageTestCaseExpectation expectation; +}; + +class Test_MaxLairFitsStage : public UnitTest{ +public: + explicit Test_MaxLairFitsStage(FitsStageTestCase scenario) + : UnitTest(std::format("PokemonSwSh::MaxLairFitsStage - {}", scenario.input.name)) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const FitsStageTestCaseInput& input = m_scenario.input; + const FitsStageTestCaseExpectation& expected = m_scenario.expectation; + GlobalState state; + state.wins = input.wins; + state.opponent = input.opponent; + state.boss = input.boss; + const bool fits = state.opponent_fits_stage(); + if (fits != expected.fits){ + return std::format("Expected fits={}, got {}", expected.fits, fits); + } + return true; + } + +private: + FitsStageTestCase m_scenario; +}; + +const FitsStageTestCase FITS_STAGE_CASES[] = { + { + .input = {.name = "rental before three wins", .wins = 2, .opponent = {"mimikyu-disguised"}, .boss = ""}, + .expectation = {.fits = true}, + }, + { + .input = {.name = "boss before three wins", .wins = 2, .opponent = {"kyurem"}, .boss = ""}, + .expectation = {.fits = false}, + }, + { + .input = {.name = "rental at boss stage with unknown boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = ""}, + .expectation = {.fits = false}, }, - // TODO: a rental boss recorded before this fix is still reported as caught. { - .input = {.name = "fourth win after rental boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "mimikyu-disguised", .final_wins = 4}, - .expectation = {.accepted = true, .boss = "mimikyu-disguised", .boss_if_caught = "mimikyu-disguised"}, + .input = {.name = "empty read at boss stage with unknown boss", .wins = 3, .opponent = {}, .boss = ""}, + .expectation = {.fits = false}, + }, + { + .input = {.name = "ambiguous read at boss stage with unknown boss", .wins = 3, .opponent = {"kyurem", "mimikyu-disguised"}, .boss = ""}, + .expectation = {.fits = false}, + }, + { + .input = {.name = "boss at boss stage", .wins = 3, .opponent = {"kyurem"}, .boss = ""}, + .expectation = {.fits = true}, + }, + { + .input = {.name = "rental at boss stage with known boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "kyurem"}, + .expectation = {.fits = false}, + }, + { + .input = {.name = "known boss at boss stage", .wins = 3, .opponent = {"kyurem"}, .boss = "kyurem"}, + .expectation = {.fits = true}, + }, + { + .input = {.name = "other boss at boss stage with known boss", .wins = 3, .opponent = {"zekrom"}, .boss = "kyurem"}, + .expectation = {.fits = false}, }, }; - } void add_tests_MaxLairBossState(UnitTestDatabase& database){ for (const BossStateTestCase& scenario : CASES){ database.add(scenario); } + for (const FitsStageTestCase& scenario : FITS_STAGE_CASES){ + database.add(scenario); + } } } diff --git a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_Battle.cpp b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_Battle.cpp index 3685caa3a5..2715609322 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_Battle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_Battle.cpp @@ -32,7 +32,7 @@ bool process_header_read( ){ if (mon.size() == 1){ state.opponent = std::move(mon); - return true; + return state.opponent_fits_stage(); } if (mon.size() > 1){ logger.log("Ambiguous Read Result: " + set_to_str(mon), COLOR_PURPLE); @@ -41,7 +41,7 @@ bool process_header_read( "Using previous known value to disambiguate: " + set_to_str(mon), COLOR_PURPLE ); - return true; + return state.opponent_fits_stage(); } } if (state.opponent.size() == 1){ @@ -49,7 +49,7 @@ bool process_header_read( "Failed to read opponent from battle. Using previously known value: " + set_to_str(state.opponent), COLOR_ORANGE ); - return true; + return state.opponent_fits_stage(); } return false; @@ -75,14 +75,6 @@ bool read_battle_menu( std::set mon = reader.read_opponent(stream.logger(), context, stream.video()); bool retry = !process_header_read(stream.logger(), state, std::move(mon)); - std::string opponent = state.opponent.empty() - ? "" - : *state.opponent.begin(); - - // If we see a boss before the end. Something is wrong. Retry. - if (state.wins != 3 && is_boss(opponent)){ - retry = true; - } VideoSnapshot snapshot; if (retry){ @@ -363,14 +355,14 @@ StateMachineAction throw_balls( state.players[2].health.value.dead = 0; state.players[3].health.value.dead = 0; - GlobalState inferred = state_tracker.synchronize(stream.logger(), console_index); - std::string ball; bool boss = inferred.wins == 4; if (boss){ + // This will crash if `inferred.boss` is still empty after battle-screen + // and the fallback summary read (OCR misses). ball = decider.boss_ball(console_index, inferred.boss); }else{ ball = decider.normal_ball(console_index);