diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 891221b0d3..3a4fd0dce2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -91,9 +91,45 @@ void forget_quests_gone_from_panel(std::set& failed_quests, const std: }); } +// When nothing on the panel is doable, the farmer rerolls it and reads it +// again. If no blue quest can ever be doable, that loop never ends, so the +// settings are checked before starting. +// A quest is done if any exclusion row says run, matching process_quest_list(). +bool exclusions_leave_a_blue_quest(const BBQOption& options){ + const std::vector> exclusions = options.QUEST_EXCLUSIONS.copy_snapshot(); + for (BBQuests quest : blue_quests){ + if (not_possible_quests.contains(quest)){ + continue; + } + bool in_table = false; + bool run = false; + for (const std::unique_ptr& row : exclusions){ + if (row->quest == quest){ + in_table = true; + run = run || row->action == BBQAction::run; + } + } + if (!in_table || run){ + return true; + } + } + return false; +} + +// Valid settings can still produce a panel with nothing doable, which the +// next reroll fixes. If only 3 of the 31 blue quests are doable, a panel has +// nothing doable 73% of the time, and 30 such panels in a row happen with +// probability 0.008%. More than that means rerolling does not help: the game +// language is wrong so no quest is read, or there is no BP left to reroll. +const size_t MAX_READS_WITHOUT_DOABLE_QUEST = 30; + } void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game){ + if (!exclusions_leave_a_blue_quest(options)){ + throw UserSetupError(logger, "The quest exclusions table skips or rerolls every blue quest the program can do."); + } + std::vector quest_list; //all quests std::vector quests_to_do; //do-able quests uint8_t eggs_hatched = 0; //Track eggs @@ -116,7 +152,15 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga } //Get and reroll quests until we can at least one + size_t reads_without_doable_quest = 0; while (quests_to_do.size() < 1){ + if (reads_without_doable_quest++ == MAX_READS_WITHOUT_DOABLE_QUEST){ + throw UserSetupError( + logger, + "No doable quest after " + std::to_string(MAX_READS_WITHOUT_DOABLE_QUEST) + + " quest panel reads. Check the game language and that you have BP to reroll." + ); + } quest_list = read_panel(); quests_to_do = game.process_quest_list(quest_list, eggs_hatched); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h index 1afa74e6dc..40daefa8bf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h @@ -56,6 +56,8 @@ class BBQGameActions{ // Run rounds of quests until NUM_QUESTS have been completed, or until // eggs run out when OUT_OF_EGGS is Stop, or until every doable quest on the // panel is one that already failed all its attempts. +// Throws UserSetupError if the exclusions leave no blue quest to do, or if +// the panel shows nothing doable for many reads in a row. void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 6b3a92ad39..ff90ca3503 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -6,6 +6,7 @@ #include #include +#include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "Tests/ImageUnitTest.h" @@ -253,6 +254,7 @@ struct BBQRoundsScenario{ bool out_of_bp = false; size_t max_reads = 1000; std::vector new_quests = {}; + std::vector> exclusions = {}; uint64_t expected_completed = 0; std::string expected; }; @@ -272,6 +274,12 @@ class Test_BBQFarmerRounds : public UnitTest{ options.NUM_EGGS.set(0); options.OUT_OF_EGGS.set(BBQOption::OOEggs::Stop); } + for (const auto& [quest, action] : m_scenario.exclusions){ + auto row = std::make_unique(options.QUEST_EXCLUSIONS); + row->quest.set(quest); + row->action.set(action); + options.QUEST_EXCLUSIONS.append_row(std::move(row)); + } SimulatedBBQGameActions game; game.panel = m_scenario.panel; @@ -287,6 +295,8 @@ class Test_BBQFarmerRounds : public UnitTest{ NullLogger logger; try{ run_bbq_rounds(logger, options, game); + }catch (UserSetupError&){ + game.events.push_back("error"); }catch (std::runtime_error&){ game.events.push_back("stuck"); } @@ -306,8 +316,25 @@ class Test_BBQFarmerRounds : public UnitTest{ const BBQRoundsScenario m_scenario; }; +// Every blue quest the program can do, each with the given action. +std::vector> every_doable_blue_quest(BBQAction action){ + std::vector> ret; + for (BBQuests quest : blue_quests){ + if (!not_possible_quests.contains(quest)){ + ret.emplace_back(quest, action); + } + } + return ret; +} + static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ const std::string round_of_3 = "read process read do completed read do completed read do completed"; + std::string read_30_times; + for (int c = 0; c < 30; c++){ + read_30_times += "read process "; + } + std::vector> all_skipped_but_make_tm = every_doable_blue_quest(BBQAction::skip); + all_skipped_but_make_tm.emplace_back(BBQuests::make_tm, BBQAction::run); const BBQRoundsScenario scenarios[] = { { // The goal (4) is reached in the middle of round 2. The loop only @@ -383,15 +410,13 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .expected = "read process read do end read process", }, { - // Today: rerolls change nothing (out of BP), so the farmer reads - // and rerolls the panel forever. - .name = "today: rerolls that do not help repeat forever", + // Rerolls change nothing, so the panel never becomes doable. + .name = "gives up when rerolling does not help", .num_quests = 3, .panel = {BBQuests::pickup_10, BBQuests::catch_flying, BBQuests::UnableToDetect}, .out_of_bp = true, - .max_reads = 4, .expected_completed = 0, - .expected = "read process read process read process read process read stuck", + .expected = read_30_times + "error", }, { // Today: the save check only looks for exact multiples, so going @@ -443,6 +468,23 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ "read process read do completed read do completed end " "read process read do completed end", }, + { + // Rerolling could never produce a doable panel, so the run + // fails before reading the panel. + .name = "fails at start when every blue quest is excluded", + .num_quests = 3, + .exclusions = every_doable_blue_quest(BBQAction::reroll), + .expected_completed = 0, + .expected = "error", + }, + { + // A run row keeps the quest doable even when another row skips it. + .name = "a quest with a run row is not excluded", + .num_quests = 3, + .exclusions = all_skipped_but_make_tm, + .expected_completed = 3, + .expected = round_of_3 + " end", + }, }; for (const BBQRoundsScenario& scenario : scenarios){ database.add(scenario);