diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 9b48682bc1..ade4631c3c 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -28,6 +28,7 @@ #include "NintendoSwitch/Inference/NintendoSwitch_CheckOnlineDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_FailedToConnectDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.h" +#include "NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h" #include "NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.h" #include "NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h" #include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" @@ -73,6 +74,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ NintendoSwitch::add_tests_CheckOnlineDetector(ret); NintendoSwitch::add_tests_FailedToConnectDetector(ret); NintendoSwitch::add_tests_UpdatePopupDetector(ret); + NintendoSwitch::add_tests_Prechecks(ret); NintendoSwitch::Typewriter::add_tests_TypewriterTextMapping(ret); NintendoSwitch::PokemonBDSP::add_tests_DialogDetector(ret); NintendoSwitch::PokemonBDSP::add_tests_MenuDetector(ret); diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp new file mode 100644 index 0000000000..dc4d5986a5 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp @@ -0,0 +1,68 @@ +/* Prechecks + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "NintendoSwitch_Prechecks.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +Precheck::Precheck(std::string name, std::string needs, PrecheckPolicy policy) + : m_name(std::move(name)) + , m_needs(std::move(needs)) + , m_policy(policy) +{} + + +std::vector run_prechecks( + Logger& logger, + const PrecheckList& checks, + SkippablePrechecks skippable, + const std::function& run_check +){ + std::vector results; + for (const std::unique_ptr& check : checks){ + if (check->policy() == PrecheckPolicy::SKIPPABLE && skippable == SkippablePrechecks::SKIP){ + logger.log(std::format("Precheck {}: skipped, skippable prechecks are off.", check->name())); + results.emplace_back(PrecheckResult{check->name(), {PrecheckStatus::SKIPPED, "Skippable prechecks are off."}}); + continue; + } + logger.log(std::format("Precheck {}: {}", check->name(), check->needs())); + PrecheckOutcome outcome = run_check(*check); + if (outcome.status == PrecheckStatus::FAILED){ + throw UserSetupError(logger, std::move(outcome.message)); + } + if (outcome.status == PrecheckStatus::PASSED && !outcome.message.empty()){ + logger.log(std::format("Precheck {}: passed. {}", check->name(), outcome.message)); + } + if (outcome.status == PrecheckStatus::SKIPPED){ + logger.log(std::format("Precheck {}: skipped. {}", check->name(), outcome.message), COLOR_RED); + } + results.emplace_back(PrecheckResult{check->name(), std::move(outcome)}); + } + return results; +} + +std::vector run_prechecks( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const PrecheckList& checks, + SkippablePrechecks skippable +){ + return run_prechecks( + env.console.logger(), checks, skippable, + [&](const Precheck& check){ + return check.run(env, context); + } + ); +} + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h new file mode 100644 index 0000000000..4c360fa34c --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h @@ -0,0 +1,106 @@ +/* Prechecks + * + * From: https://github.com/PokemonAutomation/ + * + * What the console and the game must look like before a program starts. + * A program lists its prechecks in order and runs them first thing, so a + * setup mistake stops the run before it resets or saves, with a message that + * says what to fix. + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_Prechecks_H +#define PokemonAutomation_NintendoSwitch_Prechecks_H + +#include +#include +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// How one precheck ended. +enum class PrecheckStatus{ + PASSED, + FAILED, + SKIPPED, +}; + +// Whether a precheck can be turned off. +enum class PrecheckPolicy{ + ALWAYS_RUNS, + SKIPPABLE, // Left out when the program turns its skippable checks off. +}; + +// Whether run_prechecks() runs the SKIPPABLE checks. +enum class SkippablePrechecks{ + RUN, + SKIP, +}; + +// What one precheck found. On FAILED, the message tells the user what to fix. +struct PrecheckOutcome{ + PrecheckStatus status; + std::string message; +}; + +// One precheck of a run, as run_prechecks() reports it. +struct PrecheckResult{ + std::string name; + PrecheckOutcome outcome; +}; + + +// One thing a program needs before it starts. +class Precheck{ +public: + Precheck(std::string name, std::string needs, PrecheckPolicy policy); + virtual ~Precheck() = default; + + // Stable id, e.g. "free_party_slots". + const std::string& name() const{ return m_name; } + // One line for the user: what the program needs. + const std::string& needs() const{ return m_needs; } + PrecheckPolicy policy() const{ return m_policy; } + + // May navigate (open a menu, go Home) but must return to the screen it started on. + // Never resets, saves or changes options. + virtual PrecheckOutcome run(SingleSwitchProgramEnvironment& env, ProControllerContext& context) const = 0; + +private: + std::string m_name; + std::string m_needs; + PrecheckPolicy m_policy; +}; + +// A program's prechecks, in the order they run. Building the list never +// touches the console. +using PrecheckList = std::vector>; + + +// Runs the checks in order, each through run_check, leaving out the SKIPPABLE +// ones when skippable is SKIP. Throws UserSetupError with the check's message +// at the first FAILED one. Returns one result per check. +std::vector run_prechecks( + Logger& logger, + const PrecheckList& checks, + SkippablePrechecks skippable, + const std::function& run_check +); + +// Runs the checks on the console. Call first in program(). +std::vector run_prechecks( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const PrecheckList& checks, + SkippablePrechecks skippable +); + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp new file mode 100644 index 0000000000..fcf4ba3bd7 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp @@ -0,0 +1,161 @@ +/* Prechecks - Tests + * + * From: https://github.com/PokemonAutomation/ + * + * run_prechecks() with fake checks: no console, no GUI. + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "NintendoSwitch_Prechecks.h" +#include "NintendoSwitch_Prechecks_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + +namespace{ + + +// A check that only has a name and a policy. The tests run it through +// run_check, never through run(). +class FakePrecheck : public Precheck{ +public: + using Precheck::Precheck; + virtual PrecheckOutcome run(SingleSwitchProgramEnvironment&, ProControllerContext&) const override{ + return {PrecheckStatus::FAILED, "A fake check must not touch the console."}; + } +}; + +struct FakeCheck{ + std::string name; + PrecheckPolicy policy; + PrecheckOutcome outcome; +}; + +struct RunPrechecksInput{ + std::vector checks; + SkippablePrechecks skippable; +}; + +struct RunPrechecksExpectation{ + // Names of the checks run_prechecks() ran, in order. + std::vector ran; + // Set when run_prechecks() throws UserSetupError: its message. + std::optional error; + // Statuses run_prechecks() returned, in order, when it did not throw. + std::vector statuses; +}; + +struct RunPrechecksCase{ + std::string name; + RunPrechecksInput input; + RunPrechecksExpectation expectation; +}; + + +// Runs the case's fake checks; returns what run_prechecks() did. +RunPrechecksExpectation run_fake_prechecks(Logger& logger, const RunPrechecksInput& input){ + PrecheckList checks; + std::map outcomes; + for (const FakeCheck& check : input.checks){ + checks.emplace_back(std::make_unique(check.name, "Fake needs.", check.policy)); + outcomes.emplace(check.name, check.outcome); + } + RunPrechecksExpectation actual; + auto run_check = [&](const Precheck& check){ + actual.ran.emplace_back(check.name()); + return outcomes.at(check.name()); + }; + try{ + for (const PrecheckResult& result : run_prechecks(logger, checks, input.skippable, run_check)){ + actual.statuses.emplace_back(result.outcome.status); + } + }catch (UserSetupError& e){ + actual.error = e.message(); + } + return actual; +} + + +class Test_RunPrechecks : public UnitTest{ +public: + explicit Test_RunPrechecks(RunPrechecksCase test_case) + : UnitTest(std::format("NintendoSwitch::Prechecks::RunPrechecks - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + const RunPrechecksExpectation actual = run_fake_prechecks(logger, m_case.input); + const RunPrechecksExpectation& expected = m_case.expectation; + if (actual.ran != expected.ran){ + return "run_prechecks() ran the wrong checks or in the wrong order."; + } + if (actual.error != expected.error){ + return std::format("run_prechecks() threw the wrong error: \"{}\"", actual.error.value_or("(none)")); + } + if (actual.statuses != expected.statuses){ + return "run_prechecks() returned the wrong statuses."; + } + return true; + } + +private: + RunPrechecksCase m_case; +}; + + +using enum PrecheckPolicy; +using enum PrecheckStatus; + +const PrecheckOutcome PASS{PASSED, ""}; +const PrecheckOutcome PARTY_FULL{FAILED, "Your party is full."}; +const PrecheckOutcome NO_PARTY_MENU{SKIPPED, "Unable to open the party menu."}; + +const std::vector RUN_PRECHECKS_CASES{ + { + .name = "AllPassInOrder", + .input = {{{"first", ALWAYS_RUNS, PASS}, {"second", SKIPPABLE, PASS}}, SkippablePrechecks::RUN}, + .expectation = {{"first", "second"}, std::nullopt, {PASSED, PASSED}}, + }, + { + .name = "SkipLeavesOutSkippableChecks", + .input = {{{"first", SKIPPABLE, PASS}, {"second", ALWAYS_RUNS, PASS}}, SkippablePrechecks::SKIP}, + .expectation = {{"second"}, std::nullopt, {SKIPPED, PASSED}}, + }, + { + .name = "FirstFailureStopsWithItsMessage", + .input = {{{"first", ALWAYS_RUNS, PARTY_FULL}, {"second", ALWAYS_RUNS, PASS}}, SkippablePrechecks::RUN}, + .expectation = {{"first"}, "Your party is full.", {}}, + }, + { + .name = "SkippedCheckDoesNotStopTheRun", + .input = {{{"first", SKIPPABLE, NO_PARTY_MENU}, {"second", ALWAYS_RUNS, PASS}}, SkippablePrechecks::RUN}, + .expectation = {{"first", "second"}, std::nullopt, {SKIPPED, PASSED}}, + }, + { + .name = "EmptyListRunsNothing", + .input = {{}, SkippablePrechecks::RUN}, + .expectation = {{}, std::nullopt, {}}, + }, +}; + + +} // namespace + + +void add_tests_Prechecks(UnitTestDatabase& database){ + for (const RunPrechecksCase& test_case : RUN_PRECHECKS_CASES){ + database.add(test_case); + } +} + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h new file mode 100644 index 0000000000..d9bd4a511c --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h @@ -0,0 +1,22 @@ +/* Prechecks - Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_Prechecks_Tests_H +#define PokemonAutomation_NintendoSwitch_Prechecks_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// Registers the precheck unit tests. +void add_tests_Prechecks(UnitTestDatabase& database); + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp new file mode 100644 index 0000000000..10f830f46f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp @@ -0,0 +1,102 @@ +/* FRLG Prechecks + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "Pokemon/Pokemon_Strings.h" +#include "PokemonFRLG_Navigation.h" +#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_PartySlot.h" +#include "PokemonFRLG_Prechecks.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +namespace{ + +constexpr size_t MAX_PARTY_SIZE = 6; + + +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 + ); +} + + +class BlackBorderPrecheck : public Precheck{ +public: + BlackBorderPrecheck() + : Precheck( + "black_border", + "Screen size at 100% with no black border, and the controller connected.", + PrecheckPolicy::ALWAYS_RUNS + ) + {} + virtual PrecheckOutcome run(SingleSwitchProgramEnvironment& env, ProControllerContext& context) const override{ + home_black_border_check(env.console, context); + return {PrecheckStatus::PASSED, ""}; + } +}; + + +class FreePartySlotsPrecheck : public Precheck{ +public: + explicit FreePartySlotsPrecheck(size_t free_slots_needed) + : Precheck( + "free_party_slots", + std::format("At least {} free party slot(s), on the overworld where the game was saved.", free_slots_needed), + PrecheckPolicy::SKIPPABLE + ) + , m_free_slots_needed(free_slots_needed) + {} + virtual PrecheckOutcome run(SingleSwitchProgramEnvironment& env, ProControllerContext& context) const override{ + try{ + open_party_menu_from_overworld(env.console, context); + }catch (OperationFailedException&){ + return {PrecheckStatus::SKIPPED, "Unable to open the party menu."}; + } + context.wait_for_all_requests(); + const size_t party_size = static_cast(detect_last_occupied_party_slot(env.console)) + 1; + return free_party_slots_outcome(party_size, m_free_slots_needed); + } + +private: + size_t m_free_slots_needed; +}; + + +} // namespace + + +std::unique_ptr make_black_border_precheck(){ + return std::make_unique(); +} + +std::unique_ptr make_free_party_slots_precheck(size_t free_slots_needed){ + return std::make_unique(free_slots_needed); +} + +PrecheckOutcome free_party_slots_outcome(size_t party_size, size_t free_slots_needed){ + if (party_size + free_slots_needed > MAX_PARTY_SIZE){ + return {PrecheckStatus::FAILED, not_enough_free_slots_message(party_size, free_slots_needed)}; + } + return {PrecheckStatus::PASSED, "Party has enough free slots."}; +} + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h new file mode 100644 index 0000000000..6e8d30cd4d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h @@ -0,0 +1,36 @@ +/* FRLG Prechecks + * + * From: https://github.com/PokemonAutomation/ + * + * The prechecks FRLG programs share. See NintendoSwitch_Prechecks.h. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_Prechecks_H +#define PokemonAutomation_PokemonFRLG_Prechecks_H + +#include +#include +#include "NintendoSwitch/Programs/NintendoSwitch_Prechecks.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// "black_border": goes Home to check the screen size and the controller, +// then resumes the game. ALWAYS_RUNS. +std::unique_ptr make_black_border_precheck(); + +// "free_party_slots": opens the party menu from the overworld and checks +// that at least free_slots_needed slots are free. SKIPPABLE. +std::unique_ptr make_free_party_slots_precheck(size_t free_slots_needed); + +// The free_party_slots verdict for a party of party_size Pokemon. +PrecheckOutcome free_party_slots_outcome(size_t party_size, size_t free_slots_needed); + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.cpp new file mode 100644 index 0000000000..44b8b888c0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.cpp @@ -0,0 +1,163 @@ +/* FRLG Prechecks - Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The FRLG precheck lists and verdicts: no console, no GUI. + */ + +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Programs/RngManipulation/PokemonFRLG_RngProgram.h" +#include "PokemonFRLG_Prechecks.h" +#include "PokemonFRLG_Prechecks_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +namespace{ + + +struct ListedPrecheck{ + std::string name; + PrecheckPolicy policy; + + bool operator==(const ListedPrecheck&) const = default; +}; + +struct RngPrechecksInput{ + size_t free_party_slots_needed; +}; + +struct RngPrechecksExpectation{ + std::vector checks; +}; + +struct RngPrechecksCase{ + std::string name; + RngPrechecksInput input; + RngPrechecksExpectation expectation; +}; + +class Test_RngPrechecks : public UnitTest{ +public: + explicit Test_RngPrechecks(RngPrechecksCase test_case) + : UnitTest(std::format("PokemonFRLG::Prechecks::RngPrechecks - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::vector listed; + for (const std::unique_ptr& check : rng_prechecks(m_case.input.free_party_slots_needed)){ + if (check->needs().empty()){ + return std::format("Precheck {} must say what it needs.", check->name()); + } + listed.emplace_back(ListedPrecheck{check->name(), check->policy()}); + } + if (listed != m_case.expectation.checks){ + return "The RNG precheck list has the wrong checks, order or policies."; + } + return true; + } + +private: + RngPrechecksCase m_case; +}; + + +struct FreePartySlotsInput{ + size_t party_size; + size_t free_slots_needed; +}; + +struct FreePartySlotsExpectation{ + PrecheckStatus status; + // Checked only on FAILED: the message the user sees. + std::string message; +}; + +struct FreePartySlotsCase{ + std::string name; + FreePartySlotsInput input; + FreePartySlotsExpectation expectation; +}; + +class Test_FreePartySlotsOutcome : public UnitTest{ +public: + explicit Test_FreePartySlotsOutcome(FreePartySlotsCase test_case) + : UnitTest(std::format("PokemonFRLG::Prechecks::FreePartySlotsOutcome - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const PrecheckOutcome outcome = free_party_slots_outcome( + m_case.input.party_size, m_case.input.free_slots_needed + ); + if (outcome.status != m_case.expectation.status){ + return std::format("Wrong verdict: \"{}\"", outcome.message); + } + if (outcome.status == PrecheckStatus::FAILED && outcome.message != m_case.expectation.message){ + return std::format("Wrong message: \"{}\"", outcome.message); + } + return true; + } + +private: + FreePartySlotsCase m_case; +}; + + +const ListedPrecheck BLACK_BORDER{"black_border", PrecheckPolicy::ALWAYS_RUNS}; +const ListedPrecheck FREE_PARTY_SLOTS{"free_party_slots", PrecheckPolicy::SKIPPABLE}; + +const std::vector RNG_PRECHECKS_CASES{ + {.name = "NeedsNoSlots", .input = {0}, .expectation = {{BLACK_BORDER}}}, + {.name = "NeedsOneSlot", .input = {1}, .expectation = {{BLACK_BORDER, FREE_PARTY_SLOTS}}}, + {.name = "NeedsTwoSlots", .input = {2}, .expectation = {{BLACK_BORDER, FREE_PARTY_SLOTS}}}, +}; + +// The messages are the ones the party check showed before it moved onto the +// precheck list. +const std::vector FREE_PARTY_SLOTS_CASES{ + {.name = "FiveOfSixNeedsOne", .input = {5, 1}, .expectation = {PrecheckStatus::PASSED, ""}}, + { + .name = "FullPartyNeedsOne", + .input = {6, 1}, + .expectation = { + PrecheckStatus::FAILED, + "Your party is full. Leave a party slot free so the program can check the " + "Pokémon it catches or receives.", + }, + }, + {.name = "FourOfSixNeedsTwo", .input = {4, 2}, .expectation = {PrecheckStatus::PASSED, ""}}, + { + .name = "FiveOfSixNeedsTwo", + .input = {5, 2}, + .expectation = { + PrecheckStatus::FAILED, + "Your party has 5 Pokémon. Leave at least 2 party slots free for the " + "Pokémon the program catches or receives.", + }, + }, +}; + + +} // namespace + + +void add_tests_Prechecks(UnitTestDatabase& database){ + for (const RngPrechecksCase& test_case : RNG_PRECHECKS_CASES){ + database.add(test_case); + } + for (const FreePartySlotsCase& test_case : FREE_PARTY_SLOTS_CASES){ + database.add(test_case); + } +} + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.h new file mode 100644 index 0000000000..29d6a11284 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.h @@ -0,0 +1,24 @@ +/* FRLG Prechecks - Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_Prechecks_Tests_H +#define PokemonAutomation_PokemonFRLG_Prechecks_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// Registers the precheck unit tests. +void add_tests_Prechecks(UnitTestDatabase& database); + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index 94dc41dd85..0dd809965a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -30,6 +30,7 @@ #include "Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.h" #include "Tests/TestUtils.h" +#include "PokemonFRLG_Prechecks_Tests.h" #include "PokemonFRLG_Tests.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -54,6 +55,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_PartyHeldItemDetector(database); add_tests_PartyLevelUpReader(database); add_tests_PartyMenuDetector(database); + add_tests_Prechecks(database); add_tests_PrizeSelectDetector(database); add_tests_PartySlotDetector(database); add_tests_StatsReader(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp index d4257d7f37..3b95ab5762 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp @@ -4,13 +4,7 @@ * */ -#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 "VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h" #include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" #include "PokemonFRLG_RngProgram.h" @@ -19,59 +13,27 @@ namespace NintendoSwitch{ namespace PokemonFRLG{ -namespace{ - -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 - ); +PrecheckList rng_prechecks(size_t free_party_slots_needed){ + PrecheckList checks; + checks.emplace_back(make_black_border_precheck()); + if (free_party_slots_needed > 0){ + checks.emplace_back(make_free_party_slots_precheck(free_party_slots_needed)); } - 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 - ); + return checks; } -} // namespace - - -void RngProgramInstance::check_free_party_slots( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context -){ - const size_t free_slots_needed = free_party_slots_needed(); - if (free_slots_needed == 0){ - return; - } - Logger& logger = env.console.logger(); - logger.log("Checking for " + std::to_string(free_slots_needed) + " free party slot(s)..."); - try{ - open_party_menu_from_overworld(env.console, context); - }catch (OperationFailedException&){ - logger.log("Unable to open the party menu. Skipping the free party slot check.", COLOR_RED); - return; - } - context.wait_for_all_requests(); - constexpr size_t MAX_PARTY_SIZE = 6; - const size_t party_size = static_cast(detect_last_occupied_party_slot(env.console)) + 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."); +PrecheckList RngProgramInstance::prechecks() const{ + return rng_prechecks(free_party_slots_needed()); } void RngProgramInstance::run_precheck( SingleSwitchProgramEnvironment& env, ProControllerContext& context ){ - home_black_border_check(env.console, context); - if (!GameSettings::instance().RUN_RNG_START_CHECKS){ - env.console.log("Start checks are off in the FRLG Game Settings. Skipping them."); - return; - } - check_free_party_slots(env, context); + const SkippablePrechecks skippable = GameSettings::instance().RUN_RNG_START_CHECKS + ? SkippablePrechecks::RUN + : SkippablePrechecks::SKIP; + run_prechecks(env, context, prechecks(), skippable); } } // namespace PokemonFRLG diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h index 727ba9b099..8e8cc64055 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h @@ -14,12 +14,18 @@ #include #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "NintendoSwitch/Programs/NintendoSwitch_Prechecks.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonFRLG{ +// The prechecks of an RNG program that needs free_party_slots_needed free +// party slots: black_border, then free_party_slots when it needs any. +PrecheckList rng_prechecks(size_t free_party_slots_needed); + + // Base class of the RNG programs. class RngProgramInstance : public SingleSwitchProgramInstance{ public: @@ -30,21 +36,18 @@ class RngProgramInstance : public SingleSwitchProgramInstance{ // party that Pokemon would go to the PC instead. virtual size_t free_party_slots_needed() const = 0; + // What the program needs before it starts, in the order run_precheck() + // checks it. Does not touch the console. + PrecheckList prechecks() const; + protected: // Call first in program(), with the game on the overworld where the - // player saved: checks the party has the free slots - // free_party_slots_needed() asks for, then goes Home for the black - // border check. Throws UserSetupError if a check fails. + // player saved: runs prechecks(). "Run Start Checks" off in the FRLG Game + // Settings skips the SKIPPABLE ones. Throws UserSetupError if a check fails. void run_precheck( SingleSwitchProgramEnvironment& env, ProControllerContext& context ); - -private: - void check_free_party_slots( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context - ); }; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 5bde7d3e75..e85cd7987f 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1417,6 +1417,10 @@ file(GLOB LIBRARY_SOURCES Source/NintendoSwitch/Programs/NintendoSwitch_GameEntry.h Source/NintendoSwitch/Programs/NintendoSwitch_MenuStabilityTester.cpp Source/NintendoSwitch/Programs/NintendoSwitch_MenuStabilityTester.h + Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp + Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h + Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp + Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h Source/NintendoSwitch/Programs/NintendoSwitch_PreventSleep.cpp Source/NintendoSwitch/Programs/NintendoSwitch_PreventSleep.h Source/NintendoSwitch/Programs/NintendoSwitch_PushJoySticks.cpp @@ -1767,6 +1771,10 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h Source/VideoGames/PokemonFRLG/PokemonFRLG_Panels.cpp Source/VideoGames/PokemonFRLG/PokemonFRLG_Panels.h + Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp + Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h + Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.cpp + Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.h Source/VideoGames/PokemonFRLG/PokemonFRLG_Settings.cpp Source/VideoGames/PokemonFRLG/PokemonFRLG_Settings.h Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp