diff --git a/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.cpp b/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.cpp index 8f4e94ab65..cbb210b0b7 100644 --- a/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.cpp +++ b/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.cpp @@ -86,6 +86,7 @@ void SingleSwitchProgramInstance::program(SingleSwitchProgramEnvironment& env, C }; try{ + start_program_game_checks(env, context); program(env, context); }catch (FatalProgramException&){ record_debug_video(); diff --git a/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h b/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h index 9c464ca3a7..b44bb655a1 100644 --- a/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h +++ b/SerialPrograms/Source/NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h @@ -86,6 +86,15 @@ class SingleSwitchProgramInstance : public GameConsole::ConsoleProgramInstance{ FeedbackType feedback_type ); + // Called with the controller ready, just before program(env, context). + // Override to check that the game is set up the way the program needs + // (e.g. a free party slot) and throw UserSetupError if not, so a setup + // mistake stops the program before it starts its work. + virtual void start_program_game_checks( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context + ){} + private: virtual void program(GameConsole::ConsoleProgramEnvironment& env, CancellableScope& scope) override; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.cpp index 970080ca26..8de1341438 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.cpp @@ -839,18 +839,15 @@ void open_party_menu_from_overworld(ConsoleHandle& console, ProControllerContext } PartySlot detect_last_occupied_party_slot(ConsoleHandle& console){ - const auto snapshot = console.video().snapshot(); - constexpr std::array slots{ - PartySlot::SIX, - PartySlot::FIVE, - PartySlot::FOUR, - PartySlot::THREE, - PartySlot::TWO, - }; + return detect_last_occupied_party_slot(console.video().snapshot()); +} + +PartySlot detect_last_occupied_party_slot(const ImageViewRGB32& screen){ + constexpr std::array slots{PartySlot::SIX, PartySlot::FIVE, PartySlot::FOUR, PartySlot::THREE, PartySlot::TWO}; for (PartySlot slot : slots){ PartyEmptySlotDetector empty_slot_detector(COLOR_RED, slot); - if (!empty_slot_detector.detect(snapshot)){ + if (!empty_slot_detector.detect(screen)){ return slot; } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h index e802a4be6d..b19ead8d00 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h @@ -10,6 +10,7 @@ #define PokemonAutomation_PokemonFRLG_Navigation_H #include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" #include "CommonFramework/Language.h" #include "CommonFramework/Tools/VideoStream.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" @@ -136,6 +137,8 @@ void open_party_menu_from_overworld(ConsoleHandle& console, ProControllerContext // Starting from the party menu, detect the last occupied party slot PartySlot detect_last_occupied_party_slot(ConsoleHandle& console); +// Detect the last occupied party slot on a screenshot of the party menu +PartySlot detect_last_occupied_party_slot(const ImageViewRGB32& screen); // Starting from the start menu, a sub-screen of the start menu, or the overworld, navigate to the bag void open_bag_from_overworld(ConsoleHandle& console, ProControllerContext& context, StartMenuContext menu_context = StartMenuContext::STANDARD); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index 49d9b56265..f8bcb64246 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -4,8 +4,16 @@ * */ +#include #include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" #include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/Json/JsonValue.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/StaticGlobals.h" #include "Inference/Box/PokemonFRLG_BoxCellOccupiedDetector.h" @@ -28,7 +36,16 @@ #include "Inference/PokemonFRLG_TrainerIdReader.h" #include "Inference/PokemonFRLG_WildEncounterReader.h" #include "Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.h" +#include "Programs/RngManipulation/PokemonFRLG_EggRng.h" +#include "Programs/RngManipulation/PokemonFRLG_GiftRng.h" +#include "Programs/RngManipulation/PokemonFRLG_RngHelper.h" +#include "Programs/RngManipulation/PokemonFRLG_RngProgram.h" +#include "Programs/RngManipulation/PokemonFRLG_RoamingLegendaryRng.h" +#include "Programs/RngManipulation/PokemonFRLG_StarterRng.h" +#include "Programs/RngManipulation/PokemonFRLG_StaticRng.h" +#include "Programs/RngManipulation/PokemonFRLG_WildRng.h" #include "PokemonFRLG_Navigation.h" +#include "Tests/ImageUnitTest.h" #include "Tests/TestUtils.h" #include "PokemonFRLG_Tests.h" @@ -261,6 +278,277 @@ void add_tests_ExitWildBattle(UnitTestDatabase& database){ } } + +std::string slot_name(PartySlot slot){ + return std::to_string(static_cast(slot) + 1); +} + +// Reads the last occupied slot from a party menu screenshot, as the RNG programs' party check does. +class Test_LastOccupiedPartySlot : public ImageUnitTest{ +public: + Test_LastOccupiedPartySlot(std::string_view image, PartySlot expected) + : ImageUnitTest("PokemonFRLG::LastOccupiedPartySlot - " + std::string(image), {std::string(image)}) + , m_expected(expected) + {} + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + const PartySlot slot = detect_last_occupied_party_slot(images[0]); + if (slot != m_expected){ + return std::format("Expected last occupied slot {}, got {}", slot_name(m_expected), slot_name(slot)); + } + return true; + } + +private: + const PartySlot m_expected; +}; + +void add_tests_LastOccupiedPartySlot(UnitTestDatabase& database){ + for (unsigned occupied = 1; occupied <= 6; occupied++){ + database.add( + "PokemonFRLG/Party/Party_" + std::to_string(occupied) + ".png", + static_cast(occupied - 1) + ); + } + database.add("PokemonFRLG/Party/Party_6_With_Held_Items.png", PartySlot::SIX); + database.add("PokemonFRLG/Party/JPN_HeldItem.png", PartySlot::SIX); + database.add("PokemonFRLG/Party/JPN_Party.png", PartySlot::FOUR); + database.add("PokemonFRLG/Party/SPA_HeldItem.png", PartySlot::SIX); +} + +// Steps that return scripted reads, and record what check_party_free_slots() asked for. +class ScriptedPartyCheck : public PartyCheckActions{ +public: + bool menu_opens = true; + PartySlot last_occupied = PartySlot::ONE; + std::string steps; + + virtual bool open_party_menu() override{ + steps += steps.empty() ? "open" : " open"; + return menu_opens; + } + virtual PartySlot read_last_occupied_slot() override{ + steps += steps.empty() ? "read" : " read"; + return last_occupied; + } +}; + +struct PartyCheckInput{ + std::string name; + size_t free_slots_needed = 1; + bool menu_opens = true; + PartySlot last_occupied = PartySlot::ONE; +}; + +struct PartyCheckExpectation{ + std::string steps = ""; + // Start of the UserSetupError message; empty if the check passes. + std::string error = ""; +}; + +struct PartyCheckCase{ + PartyCheckInput input; + PartyCheckExpectation expectation; +}; + +class Test_PartyCheck : public UnitTest{ +public: + Test_PartyCheck(PartyCheckCase test_case) + : UnitTest("PokemonFRLG::RngPartyCheck - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const PartyCheckInput& input = m_case.input; + const PartyCheckExpectation& expected = m_case.expectation; + ScriptedPartyCheck actions; + actions.menu_opens = input.menu_opens; + actions.last_occupied = input.last_occupied; + + NullLogger logger; + std::string error; + try{ + check_party_free_slots(logger, input.free_slots_needed, actions); + }catch (UserSetupError& e){ + error = e.message(); + } + const bool error_matches = expected.error.empty() + ? error.empty() + : error.rfind(expected.error, 0) == 0; + if (!error_matches){ + return std::format("Expected error: \"{}\"\nActual error: \"{}\"", expected.error, error); + } + if (actions.steps != expected.steps){ + return std::format("Expected steps: {}\nActual steps: {}", expected.steps, actions.steps); + } + return true; + } + +private: + const PartyCheckCase m_case; +}; + +void add_tests_PartyCheck(UnitTestDatabase& database){ + const PartyCheckCase cases[] = { + { + // Programs that don't add a Pokemon to the party don't open the menu. + .input = {.name = "no free slot needed, full party", .free_slots_needed = 0, .last_occupied = PartySlot::SIX}, + .expectation = {}, + }, + { + .input = {.name = "one free slot needed, one Pokemon"}, + .expectation = {.steps = "open read"}, + }, + { + .input = {.name = "one free slot needed, five Pokemon", .last_occupied = PartySlot::FIVE}, + .expectation = {.steps = "open read"}, + }, + { + .input = {.name = "one free slot needed, full party", .last_occupied = PartySlot::SIX}, + .expectation = {.steps = "open read", .error = "Your party is full. Leave a party slot free"}, + }, + { + .input = {.name = "three free slots needed, three Pokemon", .free_slots_needed = 3, .last_occupied = PartySlot::THREE}, + .expectation = {.steps = "open read"}, + }, + { + .input = {.name = "three free slots needed, four Pokemon", .free_slots_needed = 3, .last_occupied = PartySlot::FOUR}, + .expectation = {.steps = "open read", .error = "Your party has 4 "}, + }, + { + // A menu that can't be opened skips the check rather than stopping the program. + .input = {.name = "party menu not opened", .menu_opens = false, .last_occupied = PartySlot::SIX}, + .expectation = {.steps = "open"}, + }, + }; + for (const PartyCheckCase& test_case : cases){ + database.add(test_case); + } +} + + +std::string menu_context_name(StartMenuContext context){ + switch (context){ + case StartMenuContext::STANDARD: return "STANDARD"; + case StartMenuContext::SAFARI_ZONE: return "SAFARI_ZONE"; + case StartMenuContext::NO_DEX: return "NO_DEX"; + } + return "?"; +} + +template +std::unique_ptr make_rng_program(){ + return std::make_unique(); +} + +struct RngProgramPartyInput{ + std::string name; + std::function()> make_program; + // Program settings by option name, as in the saved settings JSON. + std::map settings = {}; +}; + +struct RngProgramPartyExpectation{ + size_t free_slots; + StartMenuContext menu_context = StartMenuContext::STANDARD; +}; + +struct RngProgramPartyCase{ + RngProgramPartyInput input; + RngProgramPartyExpectation expectation; +}; + +// Builds the program with the given settings and checks what its start check asks for. The +// check itself runs for every RNG program: RngProgramInstance's start hook is final. +class Test_RngProgramPartyRequirement : public UnitTest{ +public: + Test_RngProgramPartyRequirement(RngProgramPartyCase test_case) + : UnitTest("PokemonFRLG::RngProgramPartyRequirement - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const RngProgramPartyInput& input = m_case.input; + const RngProgramPartyExpectation& expected = m_case.expectation; + std::unique_ptr program = input.make_program(); + JsonObject settings; + for (const auto& [option, value] : input.settings){ + settings[option] = value; + } + program->load_json(JsonValue(std::move(settings))); + + const size_t free_slots = program->free_party_slots_needed(); + if (free_slots != expected.free_slots){ + return std::format("Expected {} free party slots needed, got {}", expected.free_slots, free_slots); + } + const StartMenuContext menu_context = program->start_menu_context(); + if (menu_context != expected.menu_context){ + return std::format( + "Expected start menu {}, got {}", menu_context_name(expected.menu_context), menu_context_name(menu_context) + ); + } + return true; + } + +private: + const RngProgramPartyCase m_case; +}; + +const RngProgramPartyCase RNG_PROGRAM_PARTY_CASES[] = { + { + .input = {.name = "StarterRng", .make_program = make_rng_program}, + .expectation = {.free_slots = 0}, + }, + { + // The egg and the two wild Pokemon caught to confirm the seed. + .input = {.name = "EggRng", .make_program = make_rng_program}, + .expectation = {.free_slots = 3}, + }, + { + .input = {.name = "GiftRng", .make_program = make_rng_program}, + .expectation = {.free_slots = 1}, + }, + { + .input = {.name = "StaticRng", .make_program = make_rng_program}, + .expectation = {.free_slots = 1}, + }, + { + .input = {.name = "RoamingLegendaryRng", .make_program = make_rng_program}, + .expectation = {.free_slots = 1}, + }, + { + .input = { + .name = "WildRng on Route 1", + .make_program = make_rng_program, + .settings = {{"GAME_LOCATION", "route_1"}}, + }, + .expectation = {.free_slots = 1}, + }, + { + // The Safari Zone start menu has no POKEMON entry in the usual place. + .input = { + .name = "WildRng in the Safari Zone", + .make_program = make_rng_program, + .settings = {{"GAME_LOCATION", "safari_zone_entrance"}}, + }, + .expectation = {.free_slots = 1, .menu_context = StartMenuContext::SAFARI_ZONE}, + }, + // RngHelper is the manual tool: whatever the target, it trusts the user's party. + { + .input = {.name = "RngHelper lapras", .make_program = make_rng_program, .settings = {{"TARGET", "lapras"}}}, + .expectation = {.free_slots = 0}, + }, +}; + +void add_tests_RngProgramPartyRequirement(UnitTestDatabase& database){ + for (const RngProgramPartyCase& test_case : RNG_PROGRAM_PARTY_CASES){ + database.add(test_case); + } +} + } @@ -279,12 +567,15 @@ void add_tests(UnitTestDatabase& database){ add_tests_EliteFourRoomDetector(database); add_tests_ExitWildBattle(database); add_tests_HallOfFameSavingDetector(database); + add_tests_LastOccupiedPartySlot(database); + add_tests_PartyCheck(database); add_tests_PartyEmptySlotDetector(database); add_tests_PartyHeldItemDetector(database); add_tests_PartyLevelUpReader(database); add_tests_PartyMenuDetector(database); add_tests_PrizeSelectDetector(database); add_tests_PartySlotDetector(database); + add_tests_RngProgramPartyRequirement(database); add_tests_StatsReader(database); add_tests_SelectionDialogDetector(database); add_tests_ShinySymbolDetector(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_EggRng.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_EggRng.h index 5462ee2eac..6adb83effc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_EggRng.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_EggRng.h @@ -14,12 +14,12 @@ #include "CommonFramework/Notifications/EventNotificationsTable.h" #include "CommonFramework/Options/LabelCellOption.h" #include "CommonTools/Options/LanguageOCROption.h" -#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" #include "Pokemon/Options/Pokemon_NameSelectOption.h" #include "Pokemon/Pokemon_AdvRng.h" #include "PokemonFRLG_BlindNavigation.h" #include "PokemonFRLG_RngCalibration.h" +#include "PokemonFRLG_RngProgram.h" #include "PokemonFRLG_RngStatsDatabase.h" #include "PokemonFRLG_RngDisplays.h" #include "PokemonFRLG_RngLoopRoutines.h" @@ -56,9 +56,12 @@ class EggRng_Descriptor : public SingleSwitchProgramDescriptor{ virtual std::unique_ptr make_stats() const override; }; -class EggRng : public SingleSwitchProgramInstance, public ConfigOption::Listener{ +// Calibrates timings to hit an RNG target for a daycare egg. +class EggRng : public RngProgramInstance, public ConfigOption::Listener{ public: using Descriptor = EggRng_Descriptor; + // The egg and the two wild Pokemon caught to confirm the seed. + virtual size_t free_party_slots_needed() const override{ return 3; } ~EggRng(); EggRng(); virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext &context) override; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_GiftRng.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_GiftRng.h index 3df8e600ac..fc18d1b111 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_GiftRng.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_GiftRng.h @@ -12,10 +12,10 @@ #include "Common/Cpp/Options/StaticTextOption.h" #include "CommonFramework/Notifications/EventNotificationsTable.h" #include "CommonTools/Options/LanguageOCROption.h" -#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" #include "PokemonFRLG_BlindNavigation.h" #include "PokemonFRLG_RngDisplays.h" +#include "PokemonFRLG_RngProgram.h" #include "PokemonFRLG_SeedsDatabase.h" namespace PokemonAutomation{ @@ -29,9 +29,11 @@ class GiftRng_Descriptor : public SingleSwitchProgramDescriptor{ virtual std::unique_ptr make_stats() const override; }; -class GiftRng : public SingleSwitchProgramInstance, private ConfigOption::Listener{ +// Calibrates timings to hit an RNG target for a gift Pokemon. +class GiftRng : public RngProgramInstance, private ConfigOption::Listener{ public: using Descriptor = GiftRng_Descriptor; + virtual size_t free_party_slots_needed() const override{ return 1; } ~GiftRng(); GiftRng(); virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext &context) override; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngHelper.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngHelper.h index 3f4e694065..cbecd81a9e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngHelper.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngHelper.h @@ -13,9 +13,9 @@ #include "Common/Cpp/Options/StaticTextOption.h" #include "CommonFramework/Notifications/EventNotificationsTable.h" #include "CommonTools/Options/LanguageOCROption.h" -#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" #include "PokemonFRLG_BlindNavigation.h" +#include "PokemonFRLG_RngProgram.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -28,9 +28,12 @@ class RngHelper_Descriptor : public SingleSwitchProgramDescriptor{ virtual std::unique_ptr make_stats() const override; }; -class RngHelper : public SingleSwitchProgramInstance{ +// Soft resets with given timings to hit a target seed and frame. +class RngHelper : public RngProgramInstance{ public: using Descriptor = RngHelper_Descriptor; + // The manual tool: the user sets the target and the party, so no check. + virtual size_t free_party_slots_needed() const override{ return 0; } RngHelper(); virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext &context) override; virtual void start_program_border_check( diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp new file mode 100644 index 0000000000..573e991687 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp @@ -0,0 +1,95 @@ +/* RNG Program + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "Pokemon/Pokemon_Strings.h" +#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_PartySlot.h" +#include "PokemonFRLG_RngProgram.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +class ConsolePartyCheckActions : public PartyCheckActions{ +public: + ConsolePartyCheckActions(ConsoleHandle& console, ProControllerContext& context, StartMenuContext menu_context) + : m_console(console), m_context(context), m_menu_context(menu_context) + {} + + virtual bool open_party_menu() override{ + try{ + open_party_menu_from_overworld(m_console, m_context, m_menu_context); + }catch (OperationFailedException&){ + return false; + } + m_context.wait_for_all_requests(); + return true; + } + virtual PartySlot read_last_occupied_slot() override{ + return detect_last_occupied_party_slot(m_console); + } + +private: + ConsoleHandle& m_console; + ProControllerContext& m_context; + StartMenuContext m_menu_context; +}; + +std::string not_enough_free_slots_message(size_t party_size, size_t free_slots_needed){ + if (free_slots_needed == 1){ + return std::format( + "Your party is full. Leave a party slot free so the program can check the {} it catches or receives.", + Pokemon::STRING_POKEMON + ); + } + return std::format( + "Your party has {} {}. Leave at least {} party slots free for the {} the program catches or receives.", + party_size, Pokemon::STRING_POKEMON, free_slots_needed, Pokemon::STRING_POKEMON + ); +} + +} // namespace + + +void check_party_free_slots( + Logger& logger, + size_t free_slots_needed, + PartyCheckActions& actions +){ + if (free_slots_needed == 0){ + return; + } + logger.log("Checking for " + std::to_string(free_slots_needed) + " free party slot(s)..."); + if (!actions.open_party_menu()){ + logger.log("Unable to open the party menu. Skipping the free party slot check.", COLOR_RED); + return; + } + constexpr size_t MAX_PARTY_SIZE = 6; + const size_t party_size = static_cast(actions.read_last_occupied_slot()) + 1; + if (MAX_PARTY_SIZE - party_size < free_slots_needed){ + throw UserSetupError(logger, not_enough_free_slots_message(party_size, free_slots_needed)); + } + logger.log("Party has enough free slots."); +} + + +void RngProgramInstance::start_program_game_checks( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context +){ + ConsolePartyCheckActions actions(env.console, context, start_menu_context()); + check_party_free_slots(env.console.logger(), free_party_slots_needed(), actions); +} + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h new file mode 100644 index 0000000000..8ec4935ef9 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h @@ -0,0 +1,79 @@ +/* RNG Program + * + * From: https://github.com/PokemonAutomation/ + * + * Base class for the FRLG RNG programs. Each program states what the game + * must look like when it starts, and the base class checks it before the + * program runs, so a setup mistake stops the run at once instead of failing + * in a misleading way many resets later. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_RngProgram_H +#define PokemonAutomation_PokemonFRLG_RngProgram_H + +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_PartySlot.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The game steps that check_party_free_slots() takes. They are injected so +// that its decisions run in tests without a Switch. +class PartyCheckActions{ +public: + virtual ~PartyCheckActions() = default; + + // Open the party menu from the overworld. Returns false if it can't. + virtual bool open_party_menu() = 0; + + // Read the last occupied slot on the open party menu. + virtual PartySlot read_last_occupied_slot() = 0; +}; + +// Checks that the party has at least `free_slots_needed` free slots, +// starting from the overworld. Throws UserSetupError if it doesn't. If the +// party menu can't be opened, logs that and returns. May leave the party +// menu open. +void check_party_free_slots( + Logger& logger, + size_t free_slots_needed, + PartyCheckActions& actions +); + + +// Base class of the RNG programs. Before program() runs, it checks that the +// party has the free slots the program needs. +class RngProgramInstance : public SingleSwitchProgramInstance{ +public: + using SingleSwitchProgramInstance::SingleSwitchProgramInstance; + + // How many free party slots the program needs: one for each Pokemon it + // catches or receives and then reads from the end of the party. In a full + // party that Pokemon would go to the PC instead. + virtual size_t free_party_slots_needed() const = 0; + + // The start menu the player sees where the program starts. + virtual StartMenuContext start_menu_context() const{ + return StartMenuContext::STANDARD; + } + + // Runs with the game on the overworld where the player saved. May leave a + // menu open: the program's first reset closes the game. Final so that + // every RNG program runs the check free_party_slots_needed() asks for. + virtual void start_program_game_checks( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context + ) override final; +}; + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RoamingLegendaryRng.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RoamingLegendaryRng.h index f53f60a6ce..2b331ac54d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RoamingLegendaryRng.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RoamingLegendaryRng.h @@ -12,10 +12,10 @@ #include "Common/Cpp/Options/StaticTextOption.h" #include "CommonFramework/Notifications/EventNotificationsTable.h" #include "CommonTools/Options/LanguageOCROption.h" -#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" #include "PokemonFRLG_BlindNavigation.h" #include "PokemonFRLG_RngDisplays.h" +#include "PokemonFRLG_RngProgram.h" #include "PokemonFRLG_SeedsDatabase.h" namespace PokemonAutomation{ @@ -29,9 +29,11 @@ class RoamingLegendaryRng_Descriptor : public SingleSwitchProgramDescriptor{ virtual std::unique_ptr make_stats() const override; }; -class RoamingLegendaryRng : public SingleSwitchProgramInstance, private ConfigOption::Listener{ +// Calibrates timings to hit an RNG target for a roaming legendary. +class RoamingLegendaryRng : public RngProgramInstance, private ConfigOption::Listener{ public: using Descriptor = RoamingLegendaryRng_Descriptor; + virtual size_t free_party_slots_needed() const override{ return 1; } ~RoamingLegendaryRng(); RoamingLegendaryRng(); virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext &context) override; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_StarterRng.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_StarterRng.h index 4e99f55bda..ea5fa11c06 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_StarterRng.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_StarterRng.h @@ -12,11 +12,11 @@ #include "Common/Cpp/Options/StaticTextOption.h" #include "CommonFramework/Notifications/EventNotificationsTable.h" #include "CommonTools/Options/LanguageOCROption.h" -#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" #include "Pokemon/Pokemon_StatsCalculation.h" #include "Pokemon/Pokemon_AdvRng.h" #include "PokemonFRLG_RngDisplays.h" +#include "PokemonFRLG_RngProgram.h" #include "PokemonFRLG_SeedsDatabase.h" namespace PokemonAutomation{ @@ -30,9 +30,12 @@ class StarterRng_Descriptor : public SingleSwitchProgramDescriptor{ virtual std::unique_ptr make_stats() const override; }; -class StarterRng : public SingleSwitchProgramInstance, private ConfigOption::Listener{ +// Calibrates timings to hit an RNG target for a starter. +class StarterRng : public RngProgramInstance, private ConfigOption::Listener{ public: using Descriptor = StarterRng_Descriptor; + // The party is empty before the starter, so there is nothing to check. + virtual size_t free_party_slots_needed() const override{ return 0; } ~StarterRng(); StarterRng(); virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext &context) override; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_StaticRng.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_StaticRng.h index 29e117f7fa..3a274e79e8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_StaticRng.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_StaticRng.h @@ -14,13 +14,13 @@ #include "Common/Cpp/Options/TextEditOption.h" #include "CommonFramework/Notifications/EventNotificationsTable.h" #include "CommonTools/Options/LanguageOCROption.h" -#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" #include "Pokemon/Pokemon_StatsCalculation.h" #include "Pokemon/Pokemon_AdvRng.h" #include "PokemonFRLG_BlindNavigation.h" #include "PokemonFRLG_RngCalibration.h" #include "PokemonFRLG_RngDisplays.h" +#include "PokemonFRLG_RngProgram.h" #include "PokemonFRLG_SeedsDatabase.h" namespace PokemonAutomation{ @@ -34,9 +34,11 @@ class StaticRng_Descriptor : public SingleSwitchProgramDescriptor{ virtual std::unique_ptr make_stats() const override; }; -class StaticRng : public SingleSwitchProgramInstance, private ConfigOption::Listener{ +// Calibrates timings to hit an RNG target for a static encounter. +class StaticRng : public RngProgramInstance, private ConfigOption::Listener{ public: using Descriptor = StaticRng_Descriptor; + virtual size_t free_party_slots_needed() const override{ return 1; } ~StaticRng(); StaticRng(); virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext &context) override; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_WildRng.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_WildRng.h index 1fa342063a..968f68119a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_WildRng.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_WildRng.h @@ -13,11 +13,11 @@ #include "CommonFramework/Notifications/EventNotificationsTable.h" #include "CommonTools/Options/LanguageOCROption.h" #include "CommonTools/Options/StringSelectOption.h" -#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" #include "Pokemon/Pokemon_AdvRng.h" #include "PokemonFRLG_BlindNavigation.h" #include "PokemonFRLG_RngDisplays.h" +#include "PokemonFRLG_RngProgram.h" #include "PokemonFRLG_SeedsDatabase.h" namespace PokemonAutomation{ @@ -31,9 +31,14 @@ class WildRng_Descriptor : public SingleSwitchProgramDescriptor{ virtual std::unique_ptr make_stats() const override; }; -class WildRng : public SingleSwitchProgramInstance, private ConfigOption::Listener{ +// Calibrates timings to hit an RNG target for a random wild encounter. +class WildRng : public RngProgramInstance, private ConfigOption::Listener{ public: using Descriptor = WildRng_Descriptor; + virtual size_t free_party_slots_needed() const override{ return 1; } + virtual StartMenuContext start_menu_context() const override{ + return in_safari_zone() ? StartMenuContext::SAFARI_ZONE : StartMenuContext::STANDARD; + } ~WildRng(); WildRng(); virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext &context) override; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 247470c0db..0d7cd42842 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1823,6 +1823,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngLoopRoutines.h Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngNavigation.cpp Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngNavigation.h + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp + Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngStatsDatabase.cpp Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngStatsDatabase.h Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RoamingLegendaryRng.cpp