Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,45 @@
});
}

// 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<std::unique_ptr<BBQuestTableRow>> 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<BBQuestTableRow>& 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){

Check failure on line 128 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

PokemonAutomation::NintendoSwitch::PokemonSV::run_bbq_rounds( Logger & logger , const BBQOption & options , BBQGameActions & game): function_lines=83, limit=50, allowed 71
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<BBQuests> quest_list; //all quests
std::vector<BBQuests> quests_to_do; //do-able quests
uint8_t eggs_hatched = 0; //Track eggs
Expand All @@ -116,7 +152,15 @@
}

//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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Pokemon SV BBQ Tests

Check warning on line 1 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=878 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand All @@ -6,6 +6,7 @@

#include <algorithm>
#include <set>
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "CommonFramework/ImageTypes/ImageRGB32.h"
#include "Tests/ImageUnitTest.h"
Expand Down Expand Up @@ -253,6 +254,7 @@
bool out_of_bp = false;
size_t max_reads = 1000;
std::vector<BBQuests> new_quests = {};
std::vector<std::pair<BBQuests, BBQAction>> exclusions = {};
uint64_t expected_completed = 0;
std::string expected;
};
Expand All @@ -264,7 +266,7 @@
, m_scenario(std::move(scenario))
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{

Check warning on line 269 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::Test_BBQFarmerRounds::run( Logger & , CancellableScope &) const: function_lines=45 (advisory above 25, limit 50)
BBQOption options;
options.NUM_QUESTS.set(m_scenario.num_quests);
options.SAVE_NUM_QUESTS.set(m_scenario.save_num_quests);
Expand All @@ -272,6 +274,12 @@
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<BBQuestTableRow>(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;
Expand All @@ -287,6 +295,8 @@
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");
}
Expand All @@ -306,8 +316,25 @@
const BBQRoundsScenario m_scenario;
};

// Every blue quest the program can do, each with the given action.
std::vector<std::pair<BBQuests, BBQAction>> every_doable_blue_quest(BBQAction action){
std::vector<std::pair<BBQuests, BBQAction>> 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){

Check failure on line 330 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

PokemonAutomation::NintendoSwitch::PokemonSV::add_tests_BBQFarmerRounds( UnitTestDatabase & database): function_lines=163, limit=50, allowed 142
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<std::pair<BBQuests, BBQAction>> 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
Expand Down Expand Up @@ -383,15 +410,13 @@
.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
Expand Down Expand Up @@ -443,6 +468,23 @@
"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<Test_BBQFarmerRounds>(scenario);
Expand Down
Loading