Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,24 @@
namespace PokemonSV{


void BBQPointsLedger::record(int read, WallClock when){

Check warning on line 14 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::BBQPointsLedger::record( int read , WallClock when): function_lines=29 (advisory above 25, limit 50)
if (read < 0){
// The next read may include quest income, so this window can't be checked.
m_pending_unchecked |= m_pending_spend > 0;
return;
}
if (m_pending_spend > 0 && starting_balance >= 0 && !m_pending_unchecked){
checked_windows++;
const int change = read - balance;
if (change != -m_pending_spend){
mismatched_windows++;
last_mismatch = "rerolls should have cost " + std::to_string(m_pending_spend)
+ " BP, but the balance went from " + std::to_string(balance)
+ " to " + std::to_string(read);
}
}
m_pending_spend = 0;
m_pending_unchecked = false;
if (starting_balance < 0){
starting_balance = read;
first_read = when;
Expand All @@ -27,6 +41,36 @@
last_read = when;
}

void BBQPointsLedger::expect_spend(int bp){
m_pending_spend += bp;
expected_spent += bp;
}
void BBQPointsLedger::skip_spend_check(){
m_pending_unchecked = true;
}

bool BBQPointsLedger::reconciled() const{
return starting_balance >= 0
&& (int64_t)balance - starting_balance == net()
&& mismatched_windows == 0;
}
std::string BBQPointsLedger::reconciliation() const{
if (starting_balance < 0){
return "BP check: no balance was read.";
}
std::string str = "BP check: " + std::string(reconciled() ? "OK" : "MISMATCH")
+ ". Balance " + std::to_string(starting_balance) + " -> " + std::to_string(balance)
+ " (change " + std::to_string((int64_t)balance - starting_balance)
+ "), stats net " + std::to_string(net())
+ ". Rerolls expected to cost " + std::to_string(expected_spent)
+ " BP; " + std::to_string(checked_windows - mismatched_windows) + "/"
+ std::to_string(checked_windows) + " reroll windows matched the reads.";
if (mismatched_windows > 0){
str += " Last mismatch: " + last_mismatch + ".";
}
return str;
}

int64_t BBQPointsLedger::net_per_hour() const{
const int64_t span_ms = std::chrono::duration_cast<std::chrono::milliseconds>(last_read - first_read).count();
if (starting_balance < 0 || span_ms <= 0){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define PokemonAutomation_PokemonSV_BBQPointsLedger_H

#include <stdint.h>
#include <string>
#include "Common/Cpp/Time.h"

namespace PokemonAutomation{
Expand All @@ -19,7 +20,7 @@
// A rise between two reads is income (quest rewards) and a drop is an
// expense (rerolls), so income and expense are only split correctly when
// they don't fall between the same two reads. The net is always exact.
struct BBQPointsLedger{

Check warning on line 23 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::BBQPointsLedger: public_methods=7 (advisory above 5, limit 7)

Check failure on line 23 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h

View workflow job for this annotation

GitHub Actions / code-health

Code health

PokemonAutomation::NintendoSwitch::PokemonSV::BBQPointsLedger: public_data=10, limit=0, allowed 6; make data private, or split it into a plain struct without methods
int starting_balance = -1; // -1 until the first successful read.
int balance = -1;
WallClock first_read;
Expand All @@ -30,13 +31,35 @@
// Record a balance read at "when". Negative reads (OCR failures) are ignored.
void record(int read, WallClock when);

// The program paid "bp" for a reroll it believes went through. The next
// successful read must show exactly that drop, since the farmer reads the
// panel again before doing any quest. A mismatch means a reroll that didn't
// happen, an unexpected BP change or a misread balance.
void expect_spend(int bp);
// A reroll whose cost is unknown (an unreadable slot): don't check this window.
void skip_spend_check();

uint64_t expected_spent = 0; // Sum of every expect_spend().
uint64_t checked_windows = 0;
uint64_t mismatched_windows = 0;
std::string last_mismatch;

// End-of-run reconciliation, one line for the log: the balance change
// against the stats' net, and the reroll spends against the reads.
bool reconciled() const;
std::string reconciliation() const;

int64_t net() const{
return (int64_t)earned - (int64_t)spent;
}

// Net BP per hour between the first and the latest read. 0 until two reads
// are at least a millisecond apart.
int64_t net_per_hour() const;

private:
int m_pending_spend = 0;
bool m_pending_unchecked = false;
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@

namespace{

class BBQSoloFarmerGameActions : public BBQGameActions{

Check failure on line 268 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::BBQSoloFarmerGameActions: public_methods=8, limit=7, allowed 7; split the class or move helpers into free functions
public:
BBQSoloFarmerGameActions(
SingleSwitchProgramEnvironment& env, ProControllerContext& context,
Expand Down Expand Up @@ -336,6 +336,7 @@
m_bp_ledger.record(BP, current_time());
update_BP_stats();
}
const BBQPointsLedger& bp_ledger() const{ return m_bp_ledger; }

private:
void update_BP_stats(){
Expand All @@ -355,9 +356,32 @@
BBQPointsLedger m_bp_ledger;
};

void log_BP_check(SingleSwitchProgramEnvironment& env, const BBQPointsLedger& ledger, const std::string& note){
env.log(ledger.reconciliation() + note, ledger.reconciled() ? COLOR_BLACK : COLOR_RED);
}

//Run the quest rounds, then check the BP stats against the rerolls. The check is logged even
//when the run stops early.
void run_rounds_and_check_BP(
SingleSwitchProgramEnvironment& env, ProControllerContext& context,
const BBQOption& options, BBQSoloFarmerGameActions& game
){
try{
run_bbq_rounds(env.logger(), options, game);
}catch (...){
//The balance isn't read again, since the game may be on any screen.
log_BP_check(env, game.bp_ledger(), " Stopped early, so this is as of the last panel read.");
throw;
}

//No quest panel is read after the last batch, so read the balance once more.
game.record_BP(read_BP(env.program_info(), env.console, context));
log_BP_check(env, game.bp_ledger(), "");
}

}

void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){

Check warning on line 384 in SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSV::BBQSoloFarmer::program( SingleSwitchProgramEnvironment & env , ProControllerContext & context): function_lines=47 (advisory above 25, limit 50)
StartProgramChecks::check_performance_class_wired_or_wireless(context);
assert_16_9_720p_min(env.logger(), env.console);

Expand Down Expand Up @@ -386,10 +410,7 @@
*/

BBQSoloFarmerGameActions game(env, context, BBQ_OPTIONS, stats, NOTIFICATION_STATUS_UPDATE);
run_bbq_rounds(env.logger(), BBQ_OPTIONS, game);

//No quest panel is read after the last batch, so read the balance once more.
game.record_BP(read_BP(env.program_info(), env.console, context));
run_rounds_and_check_BP(env, context, BBQ_OPTIONS, game);
env.update_stats();

if (BBQ_OPTIONS.FIX_TIME_WHEN_DONE){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Pokemon SV BBQ Tests

Check failure 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

<file>: file_lines=1217, limit=1000, allowed 1137
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -1012,11 +1012,14 @@
namespace{

// Balance reads in order, each at a minute offset from the first, and the
// ledger they must produce. A negative read is an OCR failure.
// ledger they must produce. A negative read is an OCR failure. "spend_before"
// is what the program paid for rerolls just before the read (-1: a reroll of
// unknown cost).
struct BBQPointsLedgerScenario{
struct Read{
int balance;
int minute;
int spend_before = 0;
};
std::string name;
std::vector<Read> reads = {};
Expand All @@ -1025,6 +1028,8 @@
uint64_t expected_spent = 0;
int64_t expected_net = 0;
int64_t expected_per_hour = 0;
uint64_t expected_checked = 0;
uint64_t expected_mismatched = 0;
};

class Test_BBQPointsLedger : public UnitTest{
Expand All @@ -1034,10 +1039,15 @@
, m_scenario(std::move(scenario))
{}

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

Check warning on line 1042 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_BBQPointsLedger::run( Logger & , CancellableScope &) const: function_lines=30 (advisory above 25, limit 50)
const WallClock start = current_time();
BBQPointsLedger ledger;
for (const BBQPointsLedgerScenario::Read& read : m_scenario.reads){
if (read.spend_before < 0){
ledger.skip_spend_check();
}else if (read.spend_before > 0){
ledger.expect_spend(read.spend_before);
}
ledger.record(read.balance, start + std::chrono::minutes(read.minute));
}
const std::pair<const char*, std::pair<int64_t, int64_t>> checks[] = {
Expand All @@ -1046,6 +1056,10 @@
{"spent", {(int64_t)m_scenario.expected_spent, (int64_t)ledger.spent}},
{"net", {m_scenario.expected_net, ledger.net()}},
{"net per hour", {m_scenario.expected_per_hour, ledger.net_per_hour()}},
{"checked reroll windows", {(int64_t)m_scenario.expected_checked, (int64_t)ledger.checked_windows}},
{"mismatched reroll windows", {(int64_t)m_scenario.expected_mismatched, (int64_t)ledger.mismatched_windows}},
//Reconciled means a balance was read and no reroll window mismatched.
{"reconciled", {m_scenario.expected_start >= 0 && m_scenario.expected_mismatched == 0, ledger.reconciled()}},
};
for (const auto& [field, values] : checks){
if (values.first != values.second){
Expand All @@ -1060,9 +1074,73 @@
const BBQPointsLedgerScenario m_scenario;
};

}

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

View workflow job for this annotation

GitHub Actions / code-health

Code health

namespace-close: close this namespace with // namespace

// Reroll windows where the next read does not show what the rerolls should have cost.
static void add_tests_BBQPointsLedgerMismatches(UnitTestDatabase& database){

Check warning on line 1080 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::add_tests_BBQPointsLedgerMismatches( UnitTestDatabase & database): function_lines=26 (advisory above 25, limit 50)
const BBQPointsLedgerScenario mismatches[] = {
{
// The game didn't take the BP: the reroll didn't happen.
.name = "a reroll that cost nothing is a mismatch",
.reads = {{6750, 0}, {6750, 1, 10}},
.expected_start = 6750,
.expected_checked = 1,
.expected_mismatched = 1,
},
{
// 6740 misread as 6140.
.name = "a misread after a reroll is a mismatch",
.reads = {{6750, 0}, {6140, 1, 10}},
.expected_start = 6750,
.expected_spent = 610,
.expected_net = -610,
.expected_per_hour = -36600,
.expected_checked = 1,
.expected_mismatched = 1,
},
};
for (const BBQPointsLedgerScenario& scenario : mismatches){
database.add<Test_BBQPointsLedger>(scenario);
}
}

// Reroll windows: what the rerolls should have cost against the next read.
static void add_tests_BBQPointsLedgerRerolls(UnitTestDatabase& database){

Check warning on line 1108 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::add_tests_BBQPointsLedgerRerolls( UnitTestDatabase & database): function_lines=34 (advisory above 25, limit 50)
const BBQPointsLedgerScenario scenarios[] = {
{
// A blue and a red reroll, each followed by a read, as the farmer does.
.name = "rerolls that match the reads reconcile",
.reads = {{6750, 0}, {6740, 1, 10}, {6800, 10}, {6750, 11, 50}},
.expected_start = 6750,
.expected_earned = 60,
.expected_spent = 60,
.expected_per_hour = 0,
.expected_checked = 2,
},
{
// The next read may include quest income, so the window isn't checked.
.name = "a failed read after a reroll skips the check",
.reads = {{6750, 0}, {-1, 1, 10}, {6790, 10}},
.expected_start = 6750,
.expected_earned = 40,
.expected_net = 40,
.expected_per_hour = 240,
},
{
.name = "a reroll of unknown cost skips the check",
.reads = {{6750, 0}, {6700, 1, -1}},
.expected_start = 6750,
.expected_spent = 50,
.expected_net = -50,
.expected_per_hour = -3000,
},
};
for (const BBQPointsLedgerScenario& scenario : scenarios){
database.add<Test_BBQPointsLedger>(scenario);
}
}

static void add_tests_BBQPointsLedger(UnitTestDatabase& database){

Check warning on line 1143 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::add_tests_BBQPointsLedger( UnitTestDatabase & database): function_lines=49 (advisory above 25, limit 50)
const BBQPointsLedgerScenario scenarios[] = {
{
.name = "no reads",
Expand Down Expand Up @@ -1108,6 +1186,8 @@
for (const BBQPointsLedgerScenario& scenario : scenarios){
database.add<Test_BBQPointsLedger>(scenario);
}
add_tests_BBQPointsLedgerRerolls(database);
add_tests_BBQPointsLedgerMismatches(database);
}


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=1535, limit=1000, allowed 1522
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -203,10 +203,10 @@
const ProgramInfo& info,
VideoStream& stream, ProControllerContext& context,
const BBQOption& BBQ_OPTIONS,
BBQPointsLedger* ledger
BBQPointsLedger* ledger, int reroll_cost
)
: m_info(info), m_stream(stream), m_context(context), m_options(BBQ_OPTIONS)
, m_ledger(ledger)
, m_ledger(ledger), m_reroll_cost(reroll_cost)
{}

virtual bool open_panel() override{
Expand Down Expand Up @@ -246,6 +246,9 @@
pbf_press_button(m_context, BUTTON_A, 160ms, 400ms);
pbf_wait(m_context, 800ms);
m_context.wait_for_all_requests();
if (m_ledger != nullptr){
m_ledger->expect_spend(m_reroll_cost);
}
}
virtual void close_panel() override{
press_Bs_to_back_to_overworld(m_info, m_stream, m_context);
Expand All @@ -265,17 +268,27 @@
ProControllerContext& m_context;
const BBQOption& m_options;
BBQPointsLedger* m_ledger;
int m_reroll_cost;
};

//This does not handle out of BP.
void reroll_all_slots(

Check warning on line 275 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_all_slots( const ProgramInfo & info , VideoStream & stream , ProControllerContext & context , const std :: vector<BBQuests> & quests , BBQPointsLedger * ledger): function_lines=28 (advisory above 25, limit 50)
const ProgramInfo& info,
VideoStream& stream, ProControllerContext& context,
size_t slots
const std::vector<BBQuests>& quests,
BBQPointsLedger* ledger
){
open_quest_panel(stream, context);

for (size_t i = 0; i < slots; i++){
for (BBQuests quest : quests){
if (ledger != nullptr){
//An unreadable slot may be empty or a quest of unknown cost.
if (quest == BBQuests::UnableToDetect){
ledger->skip_spend_check();
}else{
ledger->expect_spend(reroll_cost(quest));
}
}
pbf_press_button(context, BUTTON_A, 160ms, 400ms);
pbf_press_button(context, BUTTON_A, 160ms, 400ms); //Yes.
pbf_wait(context, 800ms);
Expand Down Expand Up @@ -346,7 +359,7 @@
size_t slot,
BBQPointsLedger* ledger
){
ConsoleBBQRerollActions actions(info, stream, context, BBQ_OPTIONS, ledger);
ConsoleBBQRerollActions actions(info, stream, context, BBQ_OPTIONS, ledger, reroll_cost(quest_list[slot]));
switch (reroll_quest(stream.logger(), actions, quest_list, slot)){
case BBQRerollResult::Rerolled:
return;
Expand Down Expand Up @@ -487,7 +500,7 @@
}
if (plan.reroll_all){
stream.log("No possible quests! Rerolling all quests.");
reroll_all_slots(info, stream, context, quest_list.size());
reroll_all_slots(info, stream, context, quest_list, ledger);
}

//Disabled since 2025-05, when the exclusions table gained its reroll action. Before that,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ BBQRerollResult reroll_quest(
);

//Reroll the quest in `slot` of `quests` on the console, with reroll_quest(). Stops the program
//if the reroll fails. If ledger is not null, it records the BP of each panel read.
//if the reroll fails. If ledger is not null, it records the BP of each panel read and what the
//reroll should cost.
void reroll_quest_slot(
const ProgramInfo& info,
VideoStream& stream, ProControllerContext& context,
Expand All @@ -145,7 +146,7 @@ void reroll_quest_slot(

//Determine which quests are possible and which quests are not. (ex. no eggs, reroll egg hatcher.)
//Quests not possible are removed from the list. If the list is empty, then reroll all items.
//If ledger is not null, it records every BP read here.
//If ledger is not null, it records every BP read here and what each reroll should cost.
std::vector<BBQuests> process_quest_list(
const ProgramInfo& info,
VideoStream& stream, ProControllerContext& context,
Expand Down
Loading