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 eb935fdfb9..7a28034337 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.cpp @@ -5,6 +5,7 @@ */ #include "Common/Cpp/PrettyPrint.h" #include "Pokemon/Pokemon_Types.h" +#include "VideoGames/PokemonSwSh/Resources/PokemonSwSh_MaxLairDatabase.h" #include "PokemonSwSh_MaxLair_State.h" namespace PokemonAutomation{ @@ -13,6 +14,24 @@ namespace PokemonSwSh{ namespace MaxLairInternal{ +bool GlobalState::infer_boss_from_opponent(){ + if (opponent.size() != 1){ + return false; + } + const std::string& slug = *opponent.begin(); + if (is_boss(slug) != (wins == 3)){ + return false; + } + if (wins == 3 && !is_boss(boss)){ + boss = slug; + } + return true; +} +std::string GlobalState::caught_boss(size_t caught_count) const{ + // A failed adventure can still have an identified boss, but no boss catch. + return caught_count == 4 && is_boss(boss) ? boss : std::string(); +} + std::string PlayerState::dump() const{ std::string str; str += console_id < 0 ? "NPC" : "Switch " + std::to_string(console_id); 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 37e0134457..00f716c52e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h @@ -168,6 +168,13 @@ struct GlobalState{ size_t find_player_index(size_t console_id) const; void clear_battle_state(); + // Accept a single opponent for the current battle stage and infer the boss. + // Returns false when the read needs to be retried; rejected reads do not change the boss. + bool infer_boss_from_opponent(); + + // Return the caught boss slug, or an empty string if no boss was caught. + std::string caught_boss(size_t caught_count) const; + std::string dump() const; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Options/PokemonSwSh_MaxLair_Options.h b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Options/PokemonSwSh_MaxLair_Options.h index c6ce85547a..28fc60e5d7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Options/PokemonSwSh_MaxLair_Options.h +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Options/PokemonSwSh_MaxLair_Options.h @@ -61,6 +61,7 @@ class EndBattleDecider{ virtual const std::string& boss_ball( size_t console_index, const std::string& boss_slug ) const = 0; + // boss_slug is empty when the adventure ended without catching the boss. virtual CaughtScreenAction end_adventure_action( size_t console_index, const std::string& boss_slug, const PathStats& path_stats, 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 ef8c42b9b7..0e115ba47b 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 @@ -75,12 +75,9 @@ 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)){ + // The fourth battle must be a boss; earlier battles must be rentals. + if (!state.infer_boss_from_opponent()){ retry = true; } @@ -97,7 +94,7 @@ bool read_battle_menu( pbf_mash_button(context, BUTTON_B, 3000ms); state.opponent = std::move(mon); - if (state.wins == 3 && !state.boss.empty()){ + if (state.wins == 3 && is_boss(state.boss)){ if (!state.opponent.empty() && *state.opponent.begin() != state.boss){ stream.log( "Inconsistent Boss: Expected " + state.boss + ", Read: " + *state.opponent.begin(), @@ -106,25 +103,16 @@ bool read_battle_menu( } state.opponent = {state.boss}; } - - opponent = state.opponent.empty() - ? "" - : *state.opponent.begin(); } context.wait_for_all_requests(); - if (state.wins != 3 && is_boss(opponent)){ - stream.log("Boss found before 3 wins. Something is seriously out-of-sync.", COLOR_RED); - dump_image(stream.logger(), MODULE_NAME, "BossBeforeEnd", snapshot); + if (!state.infer_boss_from_opponent()){ + stream.log("Opponent does not match the battle stage after summary read.", COLOR_RED); + dump_image(stream.logger(), MODULE_NAME, "UnexpectedBattleOpponent", snapshot); return false; } - // Infer the boss if we don't know it. - if (state.boss.empty() && state.wins == 3){ - state.boss = opponent; - } - // Read misc. VideoSnapshot screen = stream.video().snapshot(); state.opponent_hp = reader.read_opponent_hp(stream.logger(), screen); diff --git a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_CaughtScreen.cpp b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_CaughtScreen.cpp index dc2ba76de5..40262944c8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_CaughtScreen.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_CaughtScreen.cpp @@ -202,7 +202,7 @@ StateMachineAction run_caught_screen( } - const std::string& boss = state_tracker[console_index].boss; + const std::string boss = state_tracker[console_index].caught_boss(tracker.total()); CaughtScreenAction action = decider.end_adventure_action( console_index, boss, diff --git a/SerialPrograms/Source/VideoGames/PokemonSwSh/PokemonSwSh_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSwSh/PokemonSwSh_Tests.cpp index 704212d3d6..4bb1285463 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/PokemonSwSh_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/PokemonSwSh_Tests.cpp @@ -4,7 +4,9 @@ * */ +#include #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h" #include "VideoGames/PokemonSwSh/Inference/PokemonSwSh_DialogBoxDetector.h" #include "VideoGames/PokemonSwSh/Inference/PokemonSwSh_DialogTriangleDetector.h" #include "VideoGames/PokemonSwSh/Inference/PokemonSwSh_BoxGenderDetector.h" @@ -21,7 +23,81 @@ namespace PokemonSwSh{ +namespace{ +using MaxLairInternal::GlobalState; + +struct BossStateCase{ + std::string name; + uint8_t wins; + std::set opponent; + std::string boss; + size_t caught_count; + bool expected_accepted; + std::string expected_boss; + std::string expected_caught_boss; +}; + +class Test_MaxLairBossState : public UnitTest{ +public: + explicit Test_MaxLairBossState(BossStateCase scenario) + : UnitTest("PokemonSwSh::MaxLairBossState - " + scenario.name) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + GlobalState state; + state.wins = m_scenario.wins; + state.opponent = m_scenario.opponent; + state.boss = m_scenario.boss; + const bool accepted = state.infer_boss_from_opponent(); + if (accepted != m_scenario.expected_accepted){ + return std::format("Expected opponent accepted={}, got {}", m_scenario.expected_accepted, accepted); + } + if (state.boss != m_scenario.expected_boss){ + return std::format("Expected boss '{}', got '{}'", m_scenario.expected_boss, state.boss); + } + const std::string caught = state.caught_boss(m_scenario.caught_count); + if (caught != m_scenario.expected_caught_boss){ + return std::format("Expected caught boss '{}', got '{}'", m_scenario.expected_caught_boss, caught); + } + return true; + } + +private: + BossStateCase m_scenario; +}; + +void add_tests_MaxLairBossState(UnitTestDatabase& database){ + const BossStateCase cases[] = { + {.name = "rental before three wins", .wins = 2, .opponent = {"mimikyu-disguised"}, .boss = "", .caught_count = 3, + .expected_accepted = true, .expected_boss = "", .expected_caught_boss = ""}, + {.name = "Mimikyu at boss stage must retry", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "", .caught_count = 3, + .expected_accepted = false, .expected_boss = "", .expected_caught_boss = ""}, + {.name = "Kyurem after Mimikyu misidentification", .wins = 3, .opponent = {"kyurem"}, .boss = "mimikyu-disguised", .caught_count = 3, + .expected_accepted = true, .expected_boss = "kyurem", .expected_caught_boss = ""}, + {.name = "identified boss lost", .wins = 3, .opponent = {"kyurem"}, .boss = "kyurem", .caught_count = 3, + .expected_accepted = true, .expected_boss = "kyurem", .expected_caught_boss = ""}, + {.name = "identified boss caught", .wins = 3, .opponent = {"kyurem"}, .boss = "", .caught_count = 4, + .expected_accepted = true, .expected_boss = "kyurem", .expected_caught_boss = "kyurem"}, + {.name = "boss before three wins must retry", .wins = 2, .opponent = {"kyurem"}, .boss = "", .caught_count = 2, + .expected_accepted = false, .expected_boss = "", .expected_caught_boss = ""}, + {.name = "empty boss-stage read must retry", .wins = 3, .opponent = {}, .boss = "", .caught_count = 3, + .expected_accepted = false, .expected_boss = "", .expected_caught_boss = ""}, + {.name = "ambiguous boss-stage read must retry", .wins = 3, .opponent = {"kyurem", "mimikyu-disguised"}, .boss = "", .caught_count = 3, + .expected_accepted = false, .expected_boss = "", .expected_caught_boss = ""}, + {.name = "rejected read preserves known boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "kyurem", .caught_count = 3, + .expected_accepted = false, .expected_boss = "kyurem", .expected_caught_boss = ""}, + {.name = "four catches cannot legitimize rental boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "mimikyu-disguised", .caught_count = 4, + .expected_accepted = false, .expected_boss = "mimikyu-disguised", .expected_caught_boss = ""}, + }; + for (const BossStateCase& scenario : cases){ + database.add(scenario); + } +} +} + void add_tests(UnitTestDatabase& database){ + add_tests_MaxLairBossState(database); NintendoSwitch::PokemonSwSh::add_tests_DialogBoxDetector(database); NintendoSwitch::PokemonSwSh::add_tests_DialogTriangleDetector(database); NintendoSwitch::PokemonSwSh::add_tests_BoxGenderDetector(database);