Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@

}

void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game){

Check failure on line 135 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=121, limit=50, allowed 96
if (!exclusions_leave_a_blue_quest(options)){
throw UserSetupError(logger, "The quest exclusions table skips or rerolls every blue quest the program can do.");
}
Expand All @@ -146,6 +146,8 @@
// on the panel, so without this it gets NUM_RETRIES more attempts every
// round, forever, e.g. a sneak-up whose target does not spawn.
std::set<BBQuests> failed_quests;
std::optional<std::vector<BBQuests>> observed_panel;
std::optional<std::vector<BBQuests>> dispatch_panel;
auto read_panel = [&](){
std::vector<BBQuests> panel = game.read_quests();
forget_quests_gone_from_panel(failed_quests, panel);
Expand All @@ -168,8 +170,22 @@
" 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);
const bool reused_panel = observed_panel.has_value();
if (observed_panel){
logger.log("Reusing verified quest panel for scheduling.");
quest_list = std::move(*observed_panel);
observed_panel.reset();
forget_quests_gone_from_panel(failed_quests, quest_list);
}else{
logger.log("Reading quest panel for scheduling.");
quest_list = read_panel();
}
BBQuestBatch batch = game.process_quest_list(quest_list, eggs_hatched);
quests_to_do = std::move(batch.to_do);
observed_panel = std::move(batch.observed_panel);
if (reused_panel && !quests_to_do.empty() && !observed_panel){
dispatch_panel = quest_list;
}

// Rerolling costs BP and the failed quests may be doable another
// time, so stop instead of spending BP to replace them.
Expand All @@ -186,10 +202,19 @@
quest_list.clear();
}

//Never carry an observation across quest actions or the round's date/reset work.
observed_panel.reset();
const uint64_t completed_before_round = num_completed_quests;
for (auto current_quest : quests_to_do){
//Check if quest was already completed (ex. 500 meters completed while navigating to take a photo)
quest_list = read_panel();
if (dispatch_panel){
logger.log("Reusing verified quest panel for first dispatch.");
quest_list = std::move(*dispatch_panel);
dispatch_panel.reset();
}else{
logger.log("Reading quest panel before dispatch.");
quest_list = read_panel();
}
if (std::find(quest_list.begin(), quest_list.end(), current_quest) != quest_list.end()){
logger.log("Current quest exists on list. Doing quest.");
bool questSuccess = game.do_quest(current_quest, eggs_hatched);
Expand Down Expand Up @@ -247,7 +272,7 @@
virtual std::vector<BBQuests> read_quests() override{
return PokemonSV::read_quests(m_env.program_info(), m_env.console, m_context, m_options);
}
virtual std::vector<BBQuests> process_quest_list(
virtual BBQuestBatch process_quest_list(
const std::vector<BBQuests>& quests, uint8_t eggs_hatched
) override{
return PokemonSV::process_quest_list(m_env.program_info(), m_env.console, m_context, m_options, quests, eggs_hatched);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "CommonFramework/Notifications/EventNotificationsTable.h"
#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h"
#include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h"
#include "VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h"
#include "VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand All @@ -33,8 +33,8 @@ class BBQGameActions{

// Decide which of the quests just read are worth attempting this round.
// May reroll quests in game, so the returned list can be empty even when
// `quests` is not; the loop then reads the panel again.
virtual std::vector<BBQuests> process_quest_list(
// `quests` is not; a complete post-reroll observation can seed selection.
virtual BBQuestBatch process_quest_list(
const std::vector<BBQuests>& quests, uint8_t eggs_hatched
) = 0;

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=977 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -161,6 +161,7 @@
BBQuests::photo_savanna, BBQuests::catch_any, BBQuests::catch_normal,
BBQuests::catch_fire, BBQuests::catch_water, BBQuests::catch_grass,
};
bool reuse_rerolled_panel = false;

std::vector<std::string> events;

Expand All @@ -171,7 +172,7 @@
}
return panel;
}
virtual std::vector<BBQuests> process_quest_list(
virtual BBQuestBatch process_quest_list(
const std::vector<BBQuests>& quests, uint8_t
) override{
events.push_back("process");
Expand All @@ -181,12 +182,13 @@
doable.push_back(quest);
}
}
if (doable.empty() && !out_of_bp){
const bool rerolled = doable.empty() && !out_of_bp;
if (rerolled){
for (BBQuests& slot : panel){
slot = next_new_quest();
}
}
return doable;
return {std::move(doable), rerolled && reuse_rerolled_panel && quests.size() == 4 ? std::optional(panel) : std::nullopt};
}
virtual bool do_quest(BBQuests quest, uint8_t&) override{
events.push_back("do");
Expand Down Expand Up @@ -263,6 +265,7 @@
std::vector<BBQuests> new_quests = {};
std::vector<std::pair<BBQuests, BBQAction>> exclusions = {};
BBQOption::OutOfRetries out_of_retries = BBQOption::OutOfRetries::Skip;
bool reuse_rerolled_panel = false;
uint64_t expected_completed = 0;
std::string expected;
};
Expand All @@ -274,7 +277,7 @@
, m_scenario(std::move(scenario))
{}

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

Check warning on line 280 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=47 (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 @@ -300,6 +303,7 @@
if (!m_scenario.new_quests.empty()){
game.new_quests = m_scenario.new_quests;
}
game.reuse_rerolled_panel = m_scenario.reuse_rerolled_panel;

NullLogger logger;
try{
Expand Down Expand Up @@ -336,7 +340,7 @@
return ret;
}

static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){

Check failure on line 343 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=197, limit=50, allowed 181
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++){
Expand All @@ -345,6 +349,22 @@
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[] = {
{
.name = "reuses reroll observation for selection and first dispatch only",
.num_quests = 1,
.panel = {BBQuests::pickup_10, BBQuests::catch_flying, BBQuests::UnableToDetect, BBQuests::photo_flying},
.reuse_rerolled_panel = true,
.expected_completed = 4,
.expected = "read process process do completed read do completed read do completed read do completed end",
},
{
.name = "three-slot reroll still reads the panel again",
.num_quests = 1,
.panel = {BBQuests::pickup_10, BBQuests::catch_flying, BBQuests::UnableToDetect},
.reuse_rerolled_panel = true,
.expected_completed = 3,
.expected = "read process read process read do completed read do completed read do completed end",
},
{
// The goal (4) is reached in the middle of round 2. The loop only
// checks the goal between rounds, so round 2 finishes and 6 quests
Expand Down Expand Up @@ -603,6 +623,7 @@
bool menu_seen = true;
bool out_of_bp = false;
int missed_reads = 0; // Reads that fail OCR on every slot.
std::optional<std::vector<BBQuests>> read_override;

bool panel_open = false;
bool menu_open = false;
Expand Down Expand Up @@ -642,7 +663,7 @@
missed_reads--;
return std::vector<BBQuests>(quests.size(), BBQuests::UnableToDetect);
}
return quests;
return read_override.value_or(quests);
}
};

Expand All @@ -656,9 +677,11 @@
bool menu_seen = true;
bool out_of_bp = false;
int missed_reads = 0;
std::optional<std::vector<BBQuests>> read_override = {};
BBQRerollResult expected;
std::vector<size_t> expected_rerolled_slots = {};
std::vector<BBQuests> expected_quests;
std::optional<std::vector<BBQuests>> expected_observed_panel = {};
};

std::string reroll_result_to_string(BBQRerollResult result){
Expand All @@ -678,7 +701,7 @@
, m_scenario(std::move(scenario))
{}

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

Check warning on line 704 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_BBQReroll::run( Logger & , CancellableScope &) const: function_lines=36 (advisory above 25, limit 50)
SimulatedQuestPanel panel;
panel.quests = m_scenario.quests;
panel.replacements = m_scenario.replacements;
Expand All @@ -687,10 +710,12 @@
panel.menu_seen = m_scenario.menu_seen;
panel.out_of_bp = m_scenario.out_of_bp;
panel.missed_reads = m_scenario.missed_reads;
panel.read_override = m_scenario.read_override;

NullLogger logger;
std::vector<BBQuests> quests = m_scenario.quests;
const BBQRerollResult result = reroll_quest(logger, panel, quests, m_scenario.slot);
std::optional<std::vector<BBQuests>> observed_panel;
const BBQRerollResult result = reroll_quest(logger, panel, quests, m_scenario.slot, observed_panel);
if (result != m_scenario.expected){
return "Expected " + reroll_result_to_string(m_scenario.expected)
+ ", got " + reroll_result_to_string(result);
Expand All @@ -703,6 +728,10 @@
return "Expected quests: " + quests_to_string(m_scenario.expected_quests)
+ "got: " + quests_to_string(quests);
}
if (observed_panel != m_scenario.expected_observed_panel){
return "Expected observed panel: " + quests_to_string(m_scenario.expected_observed_panel.value_or(std::vector<BBQuests>{}))
+ ", got: " + quests_to_string(observed_panel.value_or(std::vector<BBQuests>{}));
}
if (panel.panel_open || panel.menu_open){
return "Panel left open.";
}
Expand Down Expand Up @@ -786,9 +815,50 @@
}
}

static void add_tests_BBQReroll(UnitTestDatabase& database){

Check failure on line 818 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_BBQReroll( UnitTestDatabase & database): function_lines=135, limit=50, allowed 92
using enum BBQuests;
const BBQRerollScenario scenarios[] = {
{
.name = "unknown fourth slot is not reusable",
.quests = {make_tm, travel_500, sneak_up, hatch_egg},
.replacements = {catch_any},
.slot = 2,
.read_override = std::vector<BBQuests>{make_tm, travel_500, catch_any, UnableToDetect},
.expected = BBQRerollResult::Rerolled,
.expected_rerolled_slots = {2},
.expected_quests = {make_tm, travel_500, catch_any, hatch_egg},
},
{
.name = "missing fourth slot is not reusable",
.quests = {make_tm, travel_500, sneak_up, hatch_egg},
.replacements = {catch_any},
.slot = 2,
.read_override = std::vector<BBQuests>{make_tm, travel_500, catch_any},
.expected = BBQRerollResult::Rerolled,
.expected_rerolled_slots = {2},
.expected_quests = {make_tm, travel_500, catch_any, hatch_egg},
},
{
.name = "another changed quest is not reusable",
.quests = {make_tm, travel_500, sneak_up, hatch_egg},
.replacements = {catch_any},
.slot = 2,
.read_override = std::vector<BBQuests>{photo_canyon, travel_500, catch_any, hatch_egg},
.expected = BBQRerollResult::Rerolled,
.expected_rerolled_slots = {2},
.expected_quests = {make_tm, travel_500, catch_any, hatch_egg},
},
{
.name = "complete observation after missed reads is reusable",
.quests = {make_tm, travel_500, sneak_up, hatch_egg},
.replacements = {catch_any},
.slot = 2,
.missed_reads = 2,
.expected = BBQRerollResult::Rerolled,
.expected_rerolled_slots = {2},
.expected_quests = {make_tm, travel_500, catch_any, hatch_egg},
.expected_observed_panel = std::vector<BBQuests>{make_tm, travel_500, catch_any, hatch_egg},
},
{
.name = "cursor on the top quest",
.quests = {make_tm, sneak_up, travel_500},
Expand All @@ -809,6 +879,7 @@
.expected = BBQRerollResult::Rerolled,
.expected_rerolled_slots = {1},
.expected_quests = {make_tm, catch_any, sneak_up, hatch_egg},
.expected_observed_panel = std::vector<BBQuests>{make_tm, catch_any, sneak_up, hatch_egg},
},
{
.name = "fourth quest",
Expand All @@ -818,6 +889,7 @@
.expected = BBQRerollResult::Rerolled,
.expected_rerolled_slots = {3},
.expected_quests = {make_tm, travel_500, sneak_up, catch_any},
.expected_observed_panel = std::vector<BBQuests>{make_tm, travel_500, sneak_up, catch_any},
},
{
.name = "panel not opened spends nothing",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Blueberry Quests

Check failure on line 1 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

<file>: file_lines=1470, limit=1000, allowed 1452
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -253,6 +253,7 @@
press_Bs_to_back_to_overworld(m_info, m_stream, m_context);
}
virtual std::vector<BBQuests> read_quests() override{
m_stream.log("Reading quest panel to verify reroll.");
return PokemonSV::read_quests(m_info, m_stream, m_context, m_options);
}

Expand Down Expand Up @@ -286,12 +287,14 @@

}

BBQRerollResult reroll_quest(

Check failure on line 290 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

PokemonAutomation::NintendoSwitch::PokemonSV::reroll_quest( Logger & logger , BBQRerollActions & actions , std :: vector<BBQuests> & quests , size_t slot , std :: optional<std::vector<BBQuests>> & observed_panel): function_lines=55, limit=50, allowed 50
Logger& logger,
BBQRerollActions& actions,
std::vector<BBQuests>& quests,
size_t slot
size_t slot,
std::optional<std::vector<BBQuests>>& observed_panel
){
observed_panel.reset();
if (!actions.open_panel()){
return BBQRerollResult::PanelNotOpened;
}
Expand Down Expand Up @@ -328,23 +331,30 @@
if (slot < after.size() && after[slot] != quests[slot] && after[slot] != BBQuests::UnableToDetect){
logger.log("Quest " + std::to_string(slot + 1) + " rerolled.");
quests[slot] = after[slot];
//Three slots can mean a missed fourth quest. Reuse only a complete
//read whose other slots agree with the panel used for this reroll.
if (after.size() == 4 && after == quests
&& std::find(after.begin(), after.end(), BBQuests::UnableToDetect) == after.end()){
observed_panel = std::move(after);
}
return BBQRerollResult::Rerolled;
}
}
return BBQRerollResult::NotReplaced;
}

void reroll_quest_slot(
std::optional<std::vector<BBQuests>> reroll_quest_slot(

Check warning on line 346 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::reroll_quest_slot( const ProgramInfo & info , VideoStream & stream , ProControllerContext & context , const BBQOption & BBQ_OPTIONS , std :: vector<BBQuests> & quest_list , size_t slot): function_lines=34 (advisory above 25, limit 50)
const ProgramInfo& info,
VideoStream& stream, ProControllerContext& context,
const BBQOption& BBQ_OPTIONS,
std::vector<BBQuests>& quest_list,
size_t slot
){
ConsoleBBQRerollActions actions(info, stream, context, BBQ_OPTIONS);
switch (reroll_quest(stream.logger(), actions, quest_list, slot)){
std::optional<std::vector<BBQuests>> observed_panel;
switch (reroll_quest(stream.logger(), actions, quest_list, slot, observed_panel)){
case BBQRerollResult::Rerolled:
return;
return observed_panel;
case BBQRerollResult::PanelNotOpened:
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::SEND_ERROR_REPORT,
Expand All @@ -365,6 +375,7 @@
stream
);
}
throw InternalProgramError(&stream.logger(), PA_CURRENT_FUNCTION, "Invalid enum.");
}

BBQuestPlan plan_quests(
Expand Down Expand Up @@ -401,7 +412,7 @@
return plan;
}

std::vector<BBQuests> process_quest_list(
BBQuestBatch process_quest_list(

Check warning on line 415 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::process_quest_list( const ProgramInfo & info , VideoStream & stream , ProControllerContext & context , const BBQOption & BBQ_OPTIONS , const std :: vector<BBQuests> & quest_list , uint8_t eggs_hatched): function_lines=40 (advisory above 25, limit 50)
const ProgramInfo& info,
VideoStream& stream, ProControllerContext& context,
const BBQOption& BBQ_OPTIONS,
Expand All @@ -412,8 +423,9 @@

//Each reroll updates the panel the next one checks against.
std::vector<BBQuests> panel = quest_list;
std::optional<std::vector<BBQuests>> observed_panel;
for (size_t slot : plan.reroll_slots){
reroll_quest_slot(info, stream, context, BBQ_OPTIONS, panel, slot);
observed_panel = reroll_quest_slot(info, stream, context, BBQ_OPTIONS, panel, slot);
}
if (plan.reroll_all){
stream.log("No possible quests! Rerolling all quests.");
Expand All @@ -433,10 +445,15 @@
);
}
*/
return plan.to_do;
//A mutation invalidates queued work, even when other quests remain runnable.
//A complete replacement observation can seed the next selection instead.
if (!plan.reroll_slots.empty() || plan.reroll_all){
return {{}, std::move(observed_panel)};
}
return {std::move(plan.to_do), std::nullopt};
}

bool process_and_do_quest(

Check failure on line 456 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

PokemonAutomation::NintendoSwitch::PokemonSV::process_and_do_quest( ProgramEnvironment & env , ConsoleHandle & console , ProControllerContext & context , BBQOption & BBQ_OPTIONS , BBQuests current_quest , uint8_t & eggs_hatched): function_lines=132, limit=50, allowed 131
ProgramEnvironment& env,
ConsoleHandle& console, ProControllerContext& context,
BBQOption& BBQ_OPTIONS,
Expand Down Expand Up @@ -546,6 +563,7 @@
}

//Validate quest was completed by checking list
console.log("Reading quest panel after attempt.");
std::vector<BBQuests> quest_list = read_quests(env.program_info(), console, context, BBQ_OPTIONS);
if (std::find(quest_list.begin(), quest_list.end(), current_quest) != quest_list.end()){
console.log("Current quest exists on list. Quest did not complete.");
Expand Down
Loading
Loading