diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp new file mode 100644 index 0000000000..0fb3c9cc5d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp @@ -0,0 +1,41 @@ +/* BBQ Points Ledger + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonSV_BBQPointsLedger.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +void BBQPointsLedger::record(int read, WallClock when){ + if (read < 0){ + return; + } + if (starting_balance < 0){ + starting_balance = read; + first_read = when; + }else if (read > balance){ + earned += read - balance; + }else{ + spent += balance - read; + } + balance = read; + last_read = when; +} + +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){ + return 0; + } + return net() * 3600000 / span_ms; +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h new file mode 100644 index 0000000000..48937d1fae --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h @@ -0,0 +1,46 @@ +/* BBQ Points Ledger + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonSV_BBQPointsLedger_H +#define PokemonAutomation_PokemonSV_BBQPointsLedger_H + +#include +#include "Common/Cpp/Time.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +// BP accounting from successive balance reads of the quest panel. +// 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{ + int starting_balance = -1; // -1 until the first successful read. + int balance = -1; + WallClock first_read; + WallClock last_read; + uint64_t earned = 0; + uint64_t spent = 0; + + // Record a balance read at "when". Negative reads (OCR failures) are ignored. + void record(int read, WallClock when); + + 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; +}; + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 84fd26495c..36a22e8dd0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -6,6 +6,7 @@ #include #include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/PrettyPrint.h" #include "CommonFramework/Notifications/ProgramNotifications.h" #include "CommonFramework/ProgramStats/StatsTracking.h" #include "CommonTools/StartupChecks/StartProgramChecks.h" @@ -21,6 +22,7 @@ #include "VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h" //#include "VideoGames/PokemonSV/PokemonSV_Settings.h" #include "VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h" +#include "VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h" #include "PokemonSV_BBQSoloFarmer.h" namespace PokemonAutomation{ @@ -49,14 +51,45 @@ struct BBQSoloFarmer_Descriptor::Stats : public StatsTracker{ : questsCompleted(m_stats["Quests Completed"]) , saves(m_stats["Saves"]) , errors(m_stats["Errors"]) + , bp_earned(m_stats["BP Earned"]) + , bp_spent(m_stats["BP Spent"]) { m_display_order.emplace_back("Quests Completed"); m_display_order.emplace_back("Saves"); m_display_order.emplace_back("Errors", HIDDEN_IF_ZERO); + m_display_order.emplace_back("BP Earned"); + m_display_order.emplace_back("BP Spent"); } + + void update_BP(const BBQPointsLedger& ledger){ + bp_earned.store(ledger.earned, std::memory_order_relaxed); + bp_spent.store(ledger.spent, std::memory_order_relaxed); + bp_per_hour.store(ledger.net_per_hour(), std::memory_order_relaxed); + has_bp_per_hour.store(true, std::memory_order_relaxed); + } + + // Net and BP/hour are derived, so they are shown but not saved. Past runs + // have no BP/hour since their time span isn't saved. + virtual std::string to_str(PrintMode mode) const override{ + std::string str = StatsTracker::to_str(mode); + if (mode == SAVE_TO_STATS_FILE){ + return str; + } + const int64_t net = (int64_t)bp_earned.load(std::memory_order_relaxed) - (int64_t)bp_spent.load(std::memory_order_relaxed); + str += " - Net BP: " + tostr_u_commas(net); + if (has_bp_per_hour.load(std::memory_order_relaxed)){ + str += " - BP/hour: " + tostr_u_commas(bp_per_hour.load(std::memory_order_relaxed)); + } + return str; + } + std::atomic& questsCompleted; std::atomic& saves; std::atomic& errors; + std::atomic& bp_earned; + std::atomic& bp_spent; + std::atomic bp_per_hour{0}; + std::atomic has_bp_per_hour{false}; }; std::unique_ptr BBQSoloFarmer_Descriptor::make_stats() const{ return std::unique_ptr(new Stats()); @@ -245,24 +278,32 @@ class BBQSoloFarmerGameActions : public BBQGameActions{ {} virtual std::vector read_quests() override{ - return PokemonSV::read_quests(m_env.program_info(), m_env.console, m_context, m_options); + int BP = -1; + std::vector quests = PokemonSV::read_quests(m_env.program_info(), m_env.console, m_context, m_options, &BP); + record_BP(BP); + return quests; } virtual std::vector process_quest_list( const std::vector& 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); + std::vector to_do = PokemonSV::process_quest_list( + m_env.program_info(), m_env.console, m_context, m_options, quests, eggs_hatched, &m_bp_ledger + ); + update_BP_stats(); + return to_do; } virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) override{ return process_and_do_quest(m_env, m_env.console, m_context, m_options, quest, eggs_hatched); } virtual void reroll_quest(BBQuests quest) override{ - std::vector quests = PokemonSV::read_quests(m_env.program_info(), m_env.console, m_context, m_options); + std::vector quests = read_quests(); auto iter = std::find(quests.begin(), quests.end(), quest); if (iter == quests.end()){ m_env.log("Failed quest is no longer on the panel. Not rerolling."); return; } - reroll_quest_slot(m_env.program_info(), m_env.console, m_context, m_options, quests, iter - quests.begin()); + reroll_quest_slot(m_env.program_info(), m_env.console, m_context, m_options, quests, iter - quests.begin(), &m_bp_ledger); + update_BP_stats(); } virtual void quest_completed() override{ m_stats.questsCompleted++; @@ -290,12 +331,28 @@ class BBQSoloFarmerGameActions : public BBQGameActions{ send_program_status_notification(m_env, m_status_update); } + //BP is read from the quest panel screenshots the program already takes. + void record_BP(int BP){ + m_bp_ledger.record(BP, current_time()); + update_BP_stats(); + } + private: + void update_BP_stats(){ + m_env.log( + "BP: " + std::to_string(m_bp_ledger.balance) + " (start " + std::to_string(m_bp_ledger.starting_balance) + + ", earned " + std::to_string(m_bp_ledger.earned) + ", spent " + std::to_string(m_bp_ledger.spent) + ")" + ); + m_stats.update_BP(m_bp_ledger); + m_env.update_stats(); + } + SingleSwitchProgramEnvironment& m_env; ProControllerContext& m_context; BBQOption& m_options; BBQSoloFarmer_Descriptor::Stats& m_stats; EventNotificationOption& m_status_update; + BBQPointsLedger m_bp_ledger; }; } @@ -330,6 +387,9 @@ 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)); 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 f3b0d5cc3b..dbfd67c69c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -13,6 +13,7 @@ #include "VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h" #include "VideoGames/PokemonSV/Options/PokemonSV_BBQuests.h" #include "PokemonSV_BlueberryQuests.h" +#include "PokemonSV_BBQPointsLedger.h" #include "PokemonSV_BBQSoloFarmer.h" #include "PokemonSV_BBQTests.h" @@ -1008,6 +1009,109 @@ 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. +struct BBQPointsLedgerScenario{ + struct Read{ + int balance; + int minute; + }; + std::string name; + std::vector reads = {}; + int expected_start = -1; + uint64_t expected_earned = 0; + uint64_t expected_spent = 0; + int64_t expected_net = 0; + int64_t expected_per_hour = 0; +}; + +class Test_BBQPointsLedger : public UnitTest{ +public: + Test_BBQPointsLedger(BBQPointsLedgerScenario scenario) + : UnitTest("PokemonSV::BBQPointsLedger - " + scenario.name) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const WallClock start = current_time(); + BBQPointsLedger ledger; + for (const BBQPointsLedgerScenario::Read& read : m_scenario.reads){ + ledger.record(read.balance, start + std::chrono::minutes(read.minute)); + } + const std::pair> checks[] = { + {"starting balance", {m_scenario.expected_start, ledger.starting_balance}}, + {"earned", {(int64_t)m_scenario.expected_earned, (int64_t)ledger.earned}}, + {"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()}}, + }; + for (const auto& [field, values] : checks){ + if (values.first != values.second){ + return std::string("Expected ") + field + " " + std::to_string(values.first) + + ", got " + std::to_string(values.second); + } + } + return true; + } + +private: + const BBQPointsLedgerScenario m_scenario; +}; + +} + +static void add_tests_BBQPointsLedger(UnitTestDatabase& database){ + const BBQPointsLedgerScenario scenarios[] = { + { + .name = "no reads", + }, + { + .name = "one read sets the start", + .reads = {{6750, 0}}, + .expected_start = 6750, + }, + { + // A quest reward, then a reroll. + .name = "rises are earned and drops are spent", + .reads = {{6750, 0}, {6780, 10}, {6730, 20}}, + .expected_start = 6750, + .expected_earned = 30, + .expected_spent = 50, + .expected_net = -20, + .expected_per_hour = -60, + }, + { + .name = "an unchanged balance adds nothing", + .reads = {{6750, 0}, {6750, 30}}, + .expected_start = 6750, + }, + { + .name = "OCR failures are skipped", + .reads = {{6750, 0}, {-1, 5}, {6800, 30}}, + .expected_start = 6750, + .expected_earned = 50, + .expected_net = 50, + .expected_per_hour = 100, + }, + { + // The hour starts at the first successful read. + .name = "a failed first read does not set the start", + .reads = {{-1, 0}, {6750, 10}, {6760, 70}}, + .expected_start = 6750, + .expected_earned = 10, + .expected_net = 10, + .expected_per_hour = 10, + }, + }; + for (const BBQPointsLedgerScenario& scenario : scenarios){ + database.add(scenario); + } +} + + + void add_tests_BBQ(UnitTestDatabase& database){ database.add(); @@ -1022,6 +1126,7 @@ void add_tests_BBQ(UnitTestDatabase& database){ add_tests_BBQReroll(database); add_tests_BBQuestPlan(database); add_tests_BBQFarmerRounds(database); + add_tests_BBQPointsLedger(database); add_tests_TeraEncounterAction(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 83145f1e77..6af83bb490 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -106,27 +106,12 @@ bool open_quest_panel(VideoStream& stream, ProControllerContext& context){ std::vector read_quests( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, - const BBQOption& BBQ_OPTIONS + const BBQOption& BBQ_OPTIONS, + int* BP ){ std::vector quest_list; - //Open quest list. Wait for it to open. - WhiteButtonWatcher right_panel(COLOR_BLUE, WhiteButton::ButtonB, {0.484, 0.117, 0.022, 0.037}); - int result = run_until( - stream, context, - [&](ProControllerContext& context){ - for (int i = 0; i < 6; i++){ //try 6 times - pbf_press_dpad(context, DPAD_RIGHT, 400ms, 160ms); - pbf_wait(context, 1600ms); - context.wait_for_all_requests(); - } - }, - {{ right_panel }} - ); - if (result == 0){ - stream.log("Found quest panel."); - } - context.wait_for_all_requests(); + open_quest_panel(stream, context); //Read in the initial 4 quests. stream.log("Reading quests."); @@ -145,6 +130,11 @@ std::vector read_quests( quest_list.push_back(BBQuests_string_to_enum(fourth_quest)); } + //The balance is on the same panel, so reading it costs no extra button presses. + if (BP != nullptr){ + *BP = read_BP_from_image(stream.logger(), screen); + } + //Close quest list pbf_mash_button(context, BUTTON_B, 800ms); context.wait_for_all_requests(); @@ -212,9 +202,11 @@ class ConsoleBBQRerollActions : public BBQRerollActions{ ConsoleBBQRerollActions( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, - const BBQOption& BBQ_OPTIONS + const BBQOption& BBQ_OPTIONS, + BBQPointsLedger* ledger ) : m_info(info), m_stream(stream), m_context(context), m_options(BBQ_OPTIONS) + , m_ledger(ledger) {} virtual bool open_panel() override{ @@ -259,7 +251,12 @@ class ConsoleBBQRerollActions : public BBQRerollActions{ press_Bs_to_back_to_overworld(m_info, m_stream, m_context); } virtual std::vector read_quests() override{ - return PokemonSV::read_quests(m_info, m_stream, m_context, m_options); + int BP = -1; + std::vector quests = PokemonSV::read_quests(m_info, m_stream, m_context, m_options, &BP); + if (m_ledger != nullptr){ + m_ledger->record(BP, current_time()); + } + return quests; } private: @@ -267,6 +264,7 @@ class ConsoleBBQRerollActions : public BBQRerollActions{ VideoStream& m_stream; ProControllerContext& m_context; const BBQOption& m_options; + BBQPointsLedger* m_ledger; }; //This does not handle out of BP. @@ -345,9 +343,10 @@ void reroll_quest_slot( VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, std::vector& quest_list, - size_t slot + size_t slot, + BBQPointsLedger* ledger ){ - ConsoleBBQRerollActions actions(info, stream, context, BBQ_OPTIONS); + ConsoleBBQRerollActions actions(info, stream, context, BBQ_OPTIONS, ledger); switch (reroll_quest(stream.logger(), actions, quest_list, slot)){ case BBQRerollResult::Rerolled: return; @@ -462,7 +461,8 @@ std::vector process_quest_list( VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, const std::vector& quest_list, - uint8_t eggs_hatched + uint8_t eggs_hatched, + BBQPointsLedger* ledger ){ //Read BP only when a quest not possible may be rerolled while others are doable. std::optional bp; @@ -470,6 +470,9 @@ std::vector process_quest_list( return quest != BBQuests::UnableToDetect && not_possible_quests.contains(quest); })){ const int read = read_BP(info, stream, context); + if (ledger != nullptr){ + ledger->record(read, current_time()); + } if (read >= 0){ bp = read; } @@ -480,7 +483,7 @@ std::vector process_quest_list( //Each reroll updates the panel the next one checks against. std::vector panel = quest_list; for (size_t slot : plan.reroll_slots){ - reroll_quest_slot(info, stream, context, BBQ_OPTIONS, panel, slot); + reroll_quest_slot(info, stream, context, BBQ_OPTIONS, panel, slot, ledger); } if (plan.reroll_all){ stream.log("No possible quests! Rerolling all quests."); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index f3f91cd04d..5c6c776f06 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -13,6 +13,7 @@ #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "VideoGames/PokemonSV/Programs/ShinyHunting/PokemonSV_LetsGoTools.h" #include "VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h" +#include "PokemonSV_BBQPointsLedger.h" namespace PokemonAutomation{ struct ProgramInfo; @@ -52,10 +53,12 @@ int read_BP(const ProgramInfo& info, VideoStream& stream, ProControllerContext& //Open the quest panel and read in the current quests. //Currently only supports singleplayer. //For multiplayer, we will want keep track of which quest are gold/red and scroll cursor down until all of the current player's blue quests are in. +//If BP is not null, also read the BP balance from the same screenshot (-1 on OCR failure). std::vector read_quests( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, - const BBQOption& BBQ_OPTIONS + const BBQOption& BBQ_OPTIONS, + int* BP = nullptr ); //What to do with the quests read from the panel. Slot 0 is the top quest on the panel. @@ -130,23 +133,26 @@ BBQRerollResult reroll_quest( ); //Reroll the quest in `slot` of `quests` on the console, with reroll_quest(). Stops the program -//if the reroll fails. +//if the reroll fails. If ledger is not null, it records the BP of each panel read. void reroll_quest_slot( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, std::vector& quests, - size_t slot + size_t slot, + BBQPointsLedger* ledger = nullptr ); //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. std::vector process_quest_list( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, const std::vector& quest_list, - uint8_t eggs_hatched + uint8_t eggs_hatched, + BBQPointsLedger* ledger = nullptr ); //Take the current quest and calls the function to do it, then checks the quest was successful. Returns true if so. diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 32c8dbf362..fb6bcebcc3 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2641,6 +2641,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Programs/Eggs/PokemonSV_EggRoutines.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_AuctionFarmer.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_AuctionFarmer.h + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp