diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp index 0fb3c9cc5d..9c3fd32ed3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp @@ -13,8 +13,22 @@ namespace PokemonSV{ void BBQPointsLedger::record(int read, WallClock when){ 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; @@ -27,6 +41,36 @@ void BBQPointsLedger::record(int read, WallClock when){ 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(last_read - first_read).count(); if (starting_balance < 0 || span_ms <= 0){ diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h index 48937d1fae..eb65cd9c98 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h @@ -8,6 +8,7 @@ #define PokemonAutomation_PokemonSV_BBQPointsLedger_H #include +#include #include "Common/Cpp/Time.h" namespace PokemonAutomation{ @@ -30,6 +31,24 @@ struct BBQPointsLedger{ // 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; } @@ -37,6 +56,10 @@ struct BBQPointsLedger{ // 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; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 36a22e8dd0..044a2bbb32 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -336,6 +336,7 @@ class BBQSoloFarmerGameActions : public BBQGameActions{ m_bp_ledger.record(BP, current_time()); update_BP_stats(); } + const BBQPointsLedger& bp_ledger() const{ return m_bp_ledger; } private: void update_BP_stats(){ @@ -355,6 +356,29 @@ class BBQSoloFarmerGameActions : public BBQGameActions{ 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){ @@ -386,10 +410,7 @@ void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerCo */ 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){ diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index dbfd67c69c..9ab38a05da 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -1012,11 +1012,14 @@ static void add_tests_BBQReroll(UnitTestDatabase& database){ 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 reads = {}; @@ -1025,6 +1028,8 @@ struct BBQPointsLedgerScenario{ 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{ @@ -1038,6 +1043,11 @@ class Test_BBQPointsLedger : public UnitTest{ 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> checks[] = { @@ -1046,6 +1056,10 @@ class Test_BBQPointsLedger : public UnitTest{ {"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){ @@ -1062,6 +1076,70 @@ class Test_BBQPointsLedger : public UnitTest{ } +// Reroll windows where the next read does not show what the rerolls should have cost. +static void add_tests_BBQPointsLedgerMismatches(UnitTestDatabase& database){ + 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(scenario); + } +} + +// Reroll windows: what the rerolls should have cost against the next read. +static void add_tests_BBQPointsLedgerRerolls(UnitTestDatabase& database){ + 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(scenario); + } +} + static void add_tests_BBQPointsLedger(UnitTestDatabase& database){ const BBQPointsLedgerScenario scenarios[] = { { @@ -1108,6 +1186,8 @@ static void add_tests_BBQPointsLedger(UnitTestDatabase& database){ for (const BBQPointsLedgerScenario& scenario : scenarios){ database.add(scenario); } + add_tests_BBQPointsLedgerRerolls(database); + add_tests_BBQPointsLedgerMismatches(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 6af83bb490..e72ec9a2f1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -203,10 +203,10 @@ class ConsoleBBQRerollActions : public BBQRerollActions{ 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{ @@ -246,6 +246,9 @@ class ConsoleBBQRerollActions : public BBQRerollActions{ 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); @@ -265,17 +268,27 @@ class ConsoleBBQRerollActions : public BBQRerollActions{ 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( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, - size_t slots + const std::vector& 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); @@ -346,7 +359,7 @@ void reroll_quest_slot( 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; @@ -487,7 +500,7 @@ std::vector process_quest_list( } 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, diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 5c6c776f06..fb0bc3238a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -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, @@ -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 process_quest_list( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context,