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..b6bd7a8349 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,34 @@ namespace PokemonSwSh{ namespace MaxLairInternal{ +namespace{ + +// wins is incremented after each battle is won: battles 1-3 (wins < 3) are +// against rental Pokemon, and battle 4 (wins == 3) is against the legendary boss. +bool is_boss_battle(const GlobalState& state){ + return state.wins == 3; +} + +} // namespace + +bool GlobalState::accept_opponent(){ + 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; + } + // Infer the boss if we don't know it. + if (is_boss_battle(*this) && boss.empty()){ + boss = opponent_slug; + } + return true; +} + +std::string GlobalState::boss_if_caught() const{ + // The identified boss is reported whether or not it was caught. + return boss; +} + 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..e6aa8e7050 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h @@ -146,6 +146,10 @@ struct GlobalState{ bool adventure_started = false; + // The adventure's boss as known from the start: the den's boss sprite, + // voted across consoles. Used all adventure long (path and move choice, + // the boss ball, the fourth battle's check, Save Path) and kept after a + // lost adventure. std::string boss; PathMap path; @@ -168,6 +172,19 @@ 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. + bool accept_opponent(); + + // The same boss, but only if the adventure caught it, else empty: for the + // end-of-adventure decision (end_adventure_action()), which must not apply + // 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 new file mode 100644 index 0000000000..b5b27ac65b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.cpp @@ -0,0 +1,133 @@ +/* Max Lair State Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h" +#include "PokemonSwSh_MaxLair_State_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSwSh{ +namespace MaxLairInternal{ +namespace{ + +struct BossStateTestCaseInput{ + std::string name; + uint8_t wins; + std::set opponent; + std::string boss; + uint8_t final_wins; // wins on the caught screen +}; + +struct BossStateTestCaseExpectation{ + bool accepted; + std::string boss; + std::string boss_if_caught; +}; + +struct BossStateTestCase{ + BossStateTestCaseInput input; + BossStateTestCaseExpectation expectation; +}; + +class Test_MaxLairBossState : public UnitTest{ +public: + explicit Test_MaxLairBossState(BossStateTestCase scenario) + : UnitTest("PokemonSwSh::MaxLairBossState - " + scenario.input.name) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const BossStateTestCaseInput& input = m_scenario.input; + const BossStateTestCaseExpectation& expected = m_scenario.expectation; + GlobalState state; + state.wins = input.wins; + state.opponent = input.opponent; + state.boss = input.boss; + const bool accepted = state.accept_opponent(); + if (accepted != expected.accepted){ + return std::format("Expected opponent accepted={}, got {}", expected.accepted, accepted); + } + if (state.boss != expected.boss){ + return std::format("Expected boss '{}', got '{}'", expected.boss, state.boss); + } + state.wins = input.final_wins; + const std::string caught = state.boss_if_caught(); + if (caught != expected.boss_if_caught){ + return std::format("Expected boss_if_caught '{}', got '{}'", expected.boss_if_caught, caught); + } + return true; + } + +private: + 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}, + .expectation = {.accepted = true, .boss = "", .boss_if_caught = ""}, + }, + // TODO: to be fixed: a rental at the boss stage is accepted as the boss and reported caught. + { + .input = {.name = "Mimikyu at boss stage", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "", .final_wins = 3}, + .expectation = {.accepted = true, .boss = "mimikyu-disguised", .boss_if_caught = "mimikyu-disguised"}, + }, + // TODO: to be fixed: a rental misread as the boss is kept after the real boss is read. + { + .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"}, + }, + // 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"}, + }, + { + .input = {.name = "identified boss caught", .wins = 3, .opponent = {"kyurem"}, .boss = "", .final_wins = 4}, + .expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = "kyurem"}, + }, + { + .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 = ""}, + }, + // TODO: to be fixed: a two-candidate read is accepted and the first is taken as the boss. + { + .input = {.name = "ambiguous boss-stage read", .wins = 3, .opponent = {"kyurem", "mimikyu-disguised"}, .boss = "", .final_wins = 3}, + .expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = "kyurem"}, + }, + // 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"}, + }, + // TODO: to be fixed: a rental read at the boss stage is accepted and the rental is reported as the caught boss. + { + .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"}, + }, +}; + +} + +void add_tests_MaxLairBossState(UnitTestDatabase& database){ + for (const BossStateTestCase& scenario : CASES){ + database.add(scenario); + } +} + +} +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.h b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.h new file mode 100644 index 0000000000..3044aa1792 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.h @@ -0,0 +1,25 @@ +/* Max Lair State Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonSwSh_MaxLair_State_Tests_H +#define PokemonAutomation_PokemonSwSh_MaxLair_State_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSwSh{ +namespace MaxLairInternal{ + + +void add_tests_MaxLairBossState(UnitTestDatabase& database); + + +} +} +} +} +#endif 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..ba8dcd4059 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 @@ -106,25 +106,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)){ + if (!state.accept_opponent()){ stream.log("Boss found before 3 wins. Something is seriously out-of-sync.", COLOR_RED); dump_image(stream.logger(), MODULE_NAME, "BossBeforeEnd", 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..6091b3daae 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].boss_if_caught(); 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..a6190002a1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/PokemonSwSh_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/PokemonSwSh_Tests.cpp @@ -12,6 +12,7 @@ #include "VideoGames/PokemonSwSh/Inference/PokemonSwSh_MainMenuDetector.h" #include "VideoGames/PokemonSwSh/Inference/PokemonSwSh_YCommDetector.h" #include "VideoGames/PokemonSwSh/Inference/PokemonSwSh_SelectionArrowFinder.h" +#include "VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.h" #include "VideoGames/PokemonSwSh/MaxLair/Inference/PokemonSwSh_MaxLair_Detect_BattleMenu.h" #include "PokemonSwSh_Tests.h" @@ -30,6 +31,7 @@ void add_tests(UnitTestDatabase& database){ NintendoSwitch::PokemonSwSh::add_tests_YCommDetector(database); NintendoSwitch::PokemonSwSh::add_tests_SelectionArrowFinder(database); NintendoSwitch::PokemonSwSh::MaxLairInternal::add_tests_MaxLairBattleMenuDetector(database); + NintendoSwitch::PokemonSwSh::MaxLairInternal::add_tests_MaxLairBossState(database); } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 8af8c7d6c7..32c8dbf362 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2876,6 +2876,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_Notifications.h Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.cpp Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h + Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.cpp + Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.h Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_StateMachine.cpp Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_StateMachine.h Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_StateTracker.cpp