diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp index 6393923e5a..bf20cbee90 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp @@ -15,7 +15,7 @@ const EnumDropdownDatabase& BBQuests_database(){ static EnumDropdownDatabase database{ {BBQuests::auto_10, "auto-10", "Defeat 10 wild Pokemon using Auto Battle!"}, {BBQuests::make_tm, "make-tm", "Make yourself a TM!"}, - //{BBQuests::pickup_10, "pickup-10", "Pick up items on the ground 10 times!"}, + {BBQuests::pickup_10, "pickup-10", "Pick up items on the ground 10 times!"}, {BBQuests::sneak_up, "sneak-up", "Successfully sneak up on 1 Pokemon and surprise them with a battle!"}, {BBQuests::photo_fly, "photo-fly", "Take a photo of a wild Pokemon in flight!"}, {BBQuests::photo_swim, "photo-swim", "Take a photo of a wild Pokemon that is swimming!"}, @@ -108,8 +108,9 @@ BBQuestTable::BBQuestTable() "Exclude the quests in the table. If you want to skip a quest, select it below. " "(Quests can be explicitly included, but this is not necessary.) " "Do not exclude too many quests, as rerolling costs BP. " - "The program will automatically reroll all quests if none are possible, but will not handle being out of BP. " - "Does not include egg hatching or pickup quests, as eggs are handled in other options and pickup is not possible. ", + "If no quest is possible, the program rerolls one quest it cannot do, or all quests if there is none. " + "Quests the program cannot do, such as pickup, are never run, but can be rerolled or skipped here. " + "Does not include egg hatching, as eggs are handled in other options. ", //"Duplicates of the same quest", //duplicates will work in table order. might lead to weird behavior like rerolling and then marking a quest as complete. //won't break the program though. diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 3fdfab30ec..f3b0d5cc3b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -530,6 +530,7 @@ struct BBQuestPlanScenario{ std::string name; std::vector quests; std::vector> extra_exclusions = {}; + std::optional bp = 1000; uint8_t num_eggs = 1; uint8_t eggs_hatched = 0; BBQOption::OOEggs out_of_eggs = BBQOption::OOEggs::Reroll; @@ -572,7 +573,7 @@ class Test_BBQuestPlan : public UnitTest{ } NullLogger logger; - const BBQuestPlan plan = plan_quests(logger, options, m_scenario.quests, m_scenario.eggs_hatched); + const BBQuestPlan plan = plan_quests(logger, options, m_scenario.quests, m_scenario.eggs_hatched, m_scenario.bp); if (plan.to_do != m_scenario.expected_to_do){ return "Expected quests to do: " + quests_to_string(m_scenario.expected_to_do) + "got: " + quests_to_string(plan.to_do); @@ -724,15 +725,141 @@ static void add_tests_BBQuestPlan(UnitTestDatabase& database){ .expected_to_do = {make_tm, sneak_up}, }, { - .name = "quests not possible are left out", + // Left alone, it would hold its slot until something else is + // rerolled. A blue reroll costs 10 BP. + .name = "a blue quest not possible is rerolled while others are doable", + .quests = {make_tm, catch_flying, sneak_up}, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {1}, + }, + { + // An undetected quest is neither run nor rerolled. + .name = "a quest not possible is rerolled, an undetected one is not", .quests = {pickup_10, make_tm, UnableToDetect}, .expected_to_do = {make_tm}, + .expected_reroll_slots = {0}, + }, + { + // A red quest costs 50 BP to reroll, but pays 100-150 BP, and + // the next red quest cannot appear while it stays. + .name = "a red quest not possible is rerolled while the blues are doable", + .quests = {photo_flying, make_tm, sneak_up, travel_500}, + .expected_to_do = {make_tm, sneak_up, travel_500}, + .expected_reroll_slots = {0}, + }, + { + // The board that held 2 of 4 slots for 20 minutes. + .name = "a red and a blue quest not possible are both rerolled", + .quests = {photo_flying, make_tm, catch_flying, sneak_up}, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {0, 2}, + }, + { + // There is no longer a single slot to fall back on: every quest + // not possible was rerolled already, so not every quest is. + .name = "nothing doable rerolls each quest not possible, not all", + .quests = {photo_flying, auto_10, pickup_10, catch_flying}, + .expected_reroll_slots = {0, 2, 3}, + }, + { + .name = "a skip row keeps a red quest not possible", + .quests = {photo_flying, make_tm, catch_flying, sneak_up}, + .extra_exclusions = {{photo_flying, BBQAction::skip}}, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {2}, + }, + { + .name = "a run row keeps a blue quest not possible", + .quests = {photo_flying, make_tm, catch_flying, sneak_up}, + .extra_exclusions = {{catch_flying, BBQAction::run}}, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {0}, + }, + { + // A red reroll costs 50 BP, a blue one 10. + .name = "BP for a blue reroll but not a red one skips the red quest not possible", + .quests = {photo_flying, make_tm, catch_flying, sneak_up}, + .bp = 49, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {2}, + }, + { + .name = "BP for exactly a red reroll rerolls it and skips the blue one", + .quests = {photo_flying, make_tm, catch_flying, sneak_up}, + .bp = 55, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {0}, + }, + { + // Before, the reroll did not happen and the program stopped. + .name = "BP under a blue reroll skips quests not possible and runs the doable ones", + .quests = {photo_flying, make_tm, catch_flying, sneak_up}, + .bp = 9, + .expected_to_do = {make_tm, sneak_up}, + }, + { + .name = "unknown BP skips quests not possible and runs the doable ones", + .quests = {photo_flying, make_tm, catch_flying, sneak_up}, + .bp = std::nullopt, + .expected_to_do = {make_tm, sneak_up}, + }, + { + // A reroll row is paid for first. + .name = "BP left after a reroll row decides the quests not possible", + .quests = {make_tm, catch_flying, sneak_up, pickup_10}, + .extra_exclusions = {{sneak_up, BBQAction::reroll}}, + .bp = 25, + .expected_to_do = {make_tm}, + .expected_reroll_slots = {1, 2}, + }, + { + // Nothing else changes the panel, so BP does not hold it back. + .name = "nothing doable rerolls each quest not possible whatever the BP", + .quests = {photo_flying, auto_10, catch_flying}, + .bp = 0, + .expected_reroll_slots = {0, 2}, + }, + { + .name = "nothing doable and nothing not possible rerolls all", + .quests = {auto_10, sneak_up}, + .extra_exclusions = {{sneak_up, BBQAction::skip}}, + .expected_reroll_all = true, + }, + { + // An undetected quest is not known to be impossible. + .name = "an undetected quest is not rerolled alone", + .quests = {UnableToDetect, auto_10}, + .expected_reroll_all = true, }, { - .name = "nothing doable rerolls all", + .name = "a skipped quest not possible is not rerolled", .quests = {pickup_10, auto_10, catch_flying}, + .extra_exclusions = {{pickup_10, BBQAction::skip}}, + .expected_reroll_slots = {2}, + }, + { + // Only skipped quests left: nothing changes the panel otherwise. + .name = "skip rows on every quest not possible reroll all when nothing is doable", + .quests = {photo_flying, auto_10, catch_flying}, + .extra_exclusions = { + {photo_flying, BBQAction::skip}, + {catch_flying, BBQAction::skip}, + }, .expected_reroll_all = true, }, + { + .name = "a reroll row rerolls a quest not possible", + .quests = {make_tm, sneak_up, pickup_10}, + .extra_exclusions = {{pickup_10, BBQAction::reroll}}, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {2}, + }, + { + .name = "a run row does not run a quest not possible", + .quests = {make_tm, pickup_10}, + .extra_exclusions = {{pickup_10, BBQAction::run}}, + .expected_to_do = {make_tm}, + }, { // The reroll replaces a quest, so the next read has something new. .name = "a reroll row rerolls that slot", @@ -746,7 +873,7 @@ static void add_tests_BBQuestPlan(UnitTestDatabase& database){ .name = "a rerolled slot with nothing doable does not reroll all", .quests = {pickup_10, sneak_up, auto_10}, .extra_exclusions = {{sneak_up, BBQAction::reroll}}, - .expected_reroll_slots = {1}, + .expected_reroll_slots = {0, 1}, }, { // Each row for a quest applies, in table order. diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 2861283c87..83145f1e77 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -4,6 +4,7 @@ * */ +#include #include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/Tools/ErrorDumper.h" @@ -171,10 +172,11 @@ void plan_out_of_eggs(Logger& logger, const BBQOption& BBQ_OPTIONS, size_t slot, } //Apply each exclusions table row for the quest, in table order. Returns false if no row is for it. +//A quest the program cannot do is never run, but can still be rerolled or skipped. bool plan_from_exclusions( Logger& logger, const std::vector>& exclusions_table, - BBQuests quest, size_t slot, + BBQuests quest, size_t slot, bool possible, BBQuestPlan& plan ){ bool quest_in_table = false; @@ -186,6 +188,10 @@ bool plan_from_exclusions( quest_in_table = true; switch (row->action){ case BBQAction::run: + if (!possible){ + logger.log("Run selected, but this quest is not possible. Skipping quest."); + break; + } logger.log("Run selected. Adding quest to list."); plan.to_do.push_back(quest); break; @@ -367,20 +373,28 @@ void reroll_quest_slot( } } +int reroll_cost(BBQuests quest){ + return red_quests.contains(quest) ? 50 : 10; +} + BBQuestPlan plan_quests( Logger& logger, const BBQOption& BBQ_OPTIONS, const std::vector& quest_list, - uint8_t eggs_hatched + uint8_t eggs_hatched, + std::optional bp ){ BBQuestPlan plan; std::vector> exclusions_table = BBQ_OPTIONS.QUEST_EXCLUSIONS.copy_snapshot(); + //Quests not possible and not in the table, in panel order. Rerolled below if BP allows. + std::vector not_possible_slots; + logger.log("Processing quests."); for (size_t slot = 0; slot < quest_list.size(); slot++){ const BBQuests n = quest_list[slot]; - if (not_possible_quests.contains(n)){ - logger.log("Quest not possible"); + if (n == BBQuests::UnableToDetect){ + logger.log("Quest not detected."); continue; } //Check eggs remaining. @@ -389,15 +403,57 @@ BBQuestPlan plan_quests( plan_out_of_eggs(logger, BBQ_OPTIONS, slot, plan); continue; } - if (!plan_from_exclusions(logger, exclusions_table, n, slot, plan)){ - logger.log("Quest not in inclusion/exclusions table. Adding to list."); - plan.to_do.push_back(n); + const bool possible = !not_possible_quests.contains(n); + if (plan_from_exclusions(logger, exclusions_table, n, slot, possible, plan)){ + continue; + } + if (!possible){ + logger.log("Quest not possible."); + not_possible_slots.push_back(slot); + continue; + } + logger.log("Quest not in inclusion/exclusions table. Adding to list."); + plan.to_do.push_back(n); + } + + if (plan.to_do.empty()){ + //Nothing else to do: a quest not possible only changes by rerolling it. + for (size_t slot : not_possible_slots){ + logger.log("No possible quests! Rerolling quest " + std::to_string(slot + 1) + ", which is not possible."); + plan.reroll_slots.push_back(slot); + } + }else{ + //Left on the panel, a quest not possible holds a slot until something else is rerolled. + //A red quest costs 50 BP to reroll instead of 10, but pays 100-150 BP and keeps the + //next red quest from appearing, so it is rerolled too. Without the BP for it, it is + //skipped and the doable quests run, rather than stopping the program on a reroll + //that does not happen. + int budget = bp.value_or(0); + for (size_t slot : plan.reroll_slots){ + budget -= reroll_cost(quest_list[slot]); + } + for (size_t slot : not_possible_slots){ + const int cost = reroll_cost(quest_list[slot]); + if (!bp || budget < cost){ + logger.log( + "Quest " + std::to_string(slot + 1) + " not possible, but BP (" + + (bp ? std::to_string(*bp) : "unknown") + ") does not cover its " + + std::to_string(cost) + " BP reroll. Skipping it." + ); + continue; + } + logger.log("Quest " + std::to_string(slot + 1) + " not possible. Rerolling quest. New quest will be run in the next batch of quests."); + budget -= cost; + plan.reroll_slots.push_back(slot); } } + std::sort(plan.reroll_slots.begin(), plan.reroll_slots.end()); //Check that quests_to_do is not empty (after completing all quests on the list, be sure to erase it. //Lag might be a problem in multi - look into making slots like menu-left navigation - plan.reroll_all = plan.reroll_slots.empty() && plan.to_do.empty(); + if (plan.reroll_slots.empty() && plan.to_do.empty()){ + plan.reroll_all = true; + } return plan; } @@ -408,7 +464,18 @@ std::vector process_quest_list( const std::vector& quest_list, uint8_t eggs_hatched ){ - BBQuestPlan plan = plan_quests(stream.logger(), BBQ_OPTIONS, quest_list, eggs_hatched); + //Read BP only when a quest not possible may be rerolled while others are doable. + std::optional bp; + if (std::any_of(quest_list.begin(), quest_list.end(), [](BBQuests quest){ + return quest != BBQuests::UnableToDetect && not_possible_quests.contains(quest); + })){ + const int read = read_BP(info, stream, context); + if (read >= 0){ + bp = read; + } + stream.log("BP: " + (bp ? std::to_string(*bp) : "unable to read")); + } + BBQuestPlan plan = plan_quests(stream.logger(), BBQ_OPTIONS, quest_list, eggs_hatched, bp); //Each reroll updates the panel the next one checks against. std::vector panel = quest_list; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 4b45e65e49..f3f91cd04d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -64,15 +64,23 @@ struct BBQuestPlan{ std::vector reroll_slots; //Slots to reroll, in panel order. bool reroll_all = false; //Nothing to do and nothing rerolled: reroll every slot. }; +//BP it costs to reroll one quest: 50 for a red quest, 10 for a blue one. +int reroll_cost(BBQuests quest); //Decide what to do with each quest from the options alone, without touching the game, so -//the decision can be tested. Quests not possible are left out. Out-of-egg hatch quests and -//quests set to reroll in the exclusions table are rerolled. A quest with several table rows -//gets the action of each row, in table order. +//the decision can be tested. Quests not possible are never run. Those not in the exclusions +//table are rerolled, red or blue, so they do not hold a slot, as long as BP (the balance +//read from the panel, if any) still covers their cost after the other planned rerolls; the +//rest are skipped, and the doable quests run. A table row for one decides instead. +//Out-of-egg hatch quests and quests set to reroll in the exclusions table are rerolled. A +//quest with several table rows gets the action of each row, in table order. With nothing to +//do, every quest not possible and not in the table is rerolled whatever the BP, or else +//every quest. BBQuestPlan plan_quests( Logger& logger, const BBQOption& BBQ_OPTIONS, const std::vector& quest_list, - uint8_t eggs_hatched + uint8_t eggs_hatched, + std::optional bp ); //The steps of rerolling one quest on the quest panel. They are injected so that the reroll