Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
*/

#include <set>
#include "Common/Cpp/Exceptions.h"
#include "CommonFramework/Notifications/ProgramNotifications.h"
#include "CommonFramework/ProgramStats/StatsTracking.h"
Expand Down Expand Up @@ -74,12 +75,40 @@
PA_ADD_OPTION(NOTIFICATIONS);
}

namespace{

// Forget the failed quests that are gone from the panel, so a quest that
// comes back later gets its attempts again. The first 3 slots are always
// read, as UnableToDetect when OCR fails, but a 4th slot is only there when
// OCR reads it. So only a read of 4 known quests proves a quest is gone; a
// missed read must not give a failing quest new attempts.
void forget_quests_gone_from_panel(std::set<BBQuests>& failed_quests, const std::vector<BBQuests>& panel){
if (panel.size() < 4 || std::find(panel.begin(), panel.end(), BBQuests::UnableToDetect) != panel.end()){
return;
}
std::erase_if(failed_quests, [&](BBQuests quest){
return std::find(panel.begin(), panel.end(), quest) == panel.end();
});
}

}

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

View workflow job for this annotation

GitHub Actions / code-health

Code health

namespace-close: close this namespace with // namespace

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

Check failure on line 96 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=71, limit=50, allowed 50
std::vector<BBQuests> quest_list; //all quests
std::vector<BBQuests> quests_to_do; //do-able quests
uint8_t eggs_hatched = 0; //Track eggs
uint64_t num_completed_quests = 0;

// Quests that failed every attempt do_quest() made. A failed quest stays
// 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;
auto read_panel = [&](){
std::vector<BBQuests> panel = game.read_quests();
forget_quests_gone_from_panel(failed_quests, panel);
return panel;
};

while (num_completed_quests < options.NUM_QUESTS){
if (options.OUT_OF_EGGS == BBQOption::OOEggs::Stop && eggs_hatched >= options.NUM_EGGS){
logger.log("Stop when out of eggs selected. Stopping program.");
Expand All @@ -88,16 +117,27 @@

//Get and reroll quests until we can at least one
while (quests_to_do.size() < 1){
quest_list = game.read_quests();
quest_list = read_panel();
quests_to_do = game.process_quest_list(quest_list, eggs_hatched);

// Rerolling costs BP and the failed quests may be doable another
// time, so stop instead of spending BP to replace them.
const bool found_doable = !quests_to_do.empty();
std::erase_if(quests_to_do, [&](BBQuests quest){
return failed_quests.contains(quest);
});
if (found_doable && quests_to_do.empty()){
logger.log("Every doable quest on the panel failed all its attempts. Stopping program.", COLOR_RED);
return;
}

//Clear out the regular quest list.
quest_list.clear();
}

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 = game.read_quests();
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 All @@ -107,6 +147,7 @@
num_completed_quests++;
}else{
logger.log("Quest did not complete successfully.");
failed_quests.insert(current_quest);
}
}else{
//Note: This doesn't account for case such as "sneak up" being added and then completed alongside the next quest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class BBQGameActions{
};

// Run rounds of quests until NUM_QUESTS have been completed, or until
// eggs run out when OUT_OF_EGGS is Stop.
// 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.
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=836 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -153,6 +153,13 @@
std::set<BBQuests> never_completes = {};
bool out_of_bp = false;
size_t max_reads = 1000;
// New quests are drawn from this list in turn, skipping any that are
// already on the panel.
std::vector<BBQuests> new_quests = {
BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar,
BBQuests::photo_savanna, BBQuests::catch_any, BBQuests::catch_normal,
BBQuests::catch_fire, BBQuests::catch_water, BBQuests::catch_grass,
};

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

Expand Down Expand Up @@ -209,16 +216,9 @@
}
}

// New quests come from a fixed rotation of doable blue quests, skipping
// any that are already on the panel.
BBQuests next_new_quest(){
static const BBQuests ROTATION[] = {
BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar,
BBQuests::photo_savanna, BBQuests::catch_any, BBQuests::catch_normal,
BBQuests::catch_fire, BBQuests::catch_water, BBQuests::catch_grass,
};
while (true){
BBQuests quest = ROTATION[m_next_new_quest++ % std::size(ROTATION)];
BBQuests quest = new_quests[m_next_new_quest++ % new_quests.size()];
if (std::find(panel.begin(), panel.end(), quest) == panel.end()){
return quest;
}
Expand Down Expand Up @@ -252,6 +252,7 @@
std::set<BBQuests> never_completes = {};
bool out_of_bp = false;
size_t max_reads = 1000;
std::vector<BBQuests> new_quests = {};
uint64_t expected_completed = 0;
std::string expected;
};
Expand All @@ -263,7 +264,7 @@
, m_scenario(std::move(scenario))
{}

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

Check warning on line 267 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=37 (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 @@ -279,6 +280,9 @@
game.never_completes = m_scenario.never_completes;
game.out_of_bp = m_scenario.out_of_bp;
game.max_reads = m_scenario.max_reads;
if (!m_scenario.new_quests.empty()){
game.new_quests = m_scenario.new_quests;
}

NullLogger logger;
try{
Expand All @@ -302,7 +306,7 @@
const BBQRoundsScenario m_scenario;
};

static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){

Check failure on line 309 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=142, limit=50, allowed 119
const std::string round_of_3 = "read process read do completed read do completed read do completed";
const BBQRoundsScenario scenarios[] = {
{
Expand Down Expand Up @@ -360,29 +364,23 @@
// "today:" state the current behavior, so that each fix shows up as a
// change to one of them.
{
// Today: sneak-up never completes and is attempted again in every
// round it stays on the panel.
.name = "today: a failed quest is attempted every round",
// sneak-up fails in round 1 and is still on the panel in round 2,
// where only the two new quests are attempted.
.name = "failed quest is not attempted again",
.num_quests = 4,
.never_completes = {BBQuests::sneak_up},
.expected_completed = 4,
.expected =
"read process read do completed read do completed read do end "
"read process read do completed read do completed read do end",
"read process read do completed read do completed end",
},
{
// Today: the only doable quest never completes, so the farmer
// attempts it every round and never finishes.
.name = "today: a panel of failed quests is retried forever",
.name = "stops when only failed quests are doable",
.num_quests = 3,
.panel = {BBQuests::sneak_up, BBQuests::pickup_10, BBQuests::catch_flying},
.never_completes = {BBQuests::sneak_up},
.max_reads = 6,
.expected_completed = 0,
.expected =
"read process read do end "
"read process read do end "
"read process read do end read stuck",
.expected = "read process read do end read process",
},
{
// Today: rerolls change nothing (out of BP), so the farmer reads
Expand All @@ -406,15 +404,44 @@
},
{
// Today: round 1 completes nothing, but 0 is a multiple of 2, so
// it saves anyway.
// it saves anyway. Every quest failed, so the farmer then stops.
.name = "today: a round without completions saves",
.num_quests = 3,
.save_num_quests = 2,
.outcomes = {false, false, false},
.expected_completed = 3,
.expected_completed = 0,
.expected = "read process read do read do read do end(save) read process",
},
{
// sneak-up fails, then leaves the panel along with make-tm. Round 2
// reads a full panel without it, and its replacement for
// photo-canyon brings it back. Round 3 attempts it again.
.name = "failed quest that leaves a full panel is attempted again",
.num_quests = 5,
.panel = {BBQuests::sneak_up, BBQuests::make_tm, BBQuests::pickup_10, BBQuests::catch_flying},
.outcomes = {false},
.also_completes = {{BBQuests::make_tm, BBQuests::sneak_up}},
.new_quests = {BBQuests::auto_10, BBQuests::photo_canyon, BBQuests::sneak_up},
.expected_completed = 5,
.expected =
"read process read do read do completed end "
"read process read do completed read do completed end "
"read process read do completed read do completed end",
},
{
// As above with 3 slots. A 4th quest could have been missed by
// OCR, so the read does not prove sneak-up left: it stays failed.
.name = "failed quest missing from a 3-slot panel stays failed",
.num_quests = 4,
.panel = {BBQuests::sneak_up, BBQuests::make_tm, BBQuests::pickup_10},
.outcomes = {false},
.also_completes = {{BBQuests::make_tm, BBQuests::sneak_up}},
.new_quests = {BBQuests::auto_10, BBQuests::photo_canyon, BBQuests::sneak_up},
.expected_completed = 4,
.expected =
"read process read do read do read do end(save) "
+ round_of_3 + " end",
"read process read do read do completed end "
"read process read do completed read do completed end "
"read process read do completed end",
},
};
for (const BBQRoundsScenario& scenario : scenarios){
Expand Down
Loading