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 @@ -210,11 +210,21 @@
return join(ret, " ");
}

std::string auto_battle_result_to_string(BBQAutoBattleResult result){
switch (result){
case BBQAutoBattleResult::TargetReached: return "TargetReached";
case BBQAutoBattleResult::NoKills: return "NoKills";
case BBQAutoBattleResult::TimeLimit: return "TimeLimit";
}
return "?";
}

struct BBQAutoBattleScenario{
std::string name;
uint64_t kill_target = 10;
int time_limit_minutes = 35;
std::vector<uint64_t> kills_per_clear;
bool expected_endless = false; // Runs past the script without stopping.
BBQAutoBattleResult expected_result = BBQAutoBattleResult::TargetReached;
std::string expected_steps;
int expected_seconds = 0;
std::vector<std::string> expected_log = {}; // Checked when not empty.
Expand All @@ -234,16 +244,16 @@
RecordingLogger logger;
BBQAutoBattlePolicy policy;
policy.kill_target = m_scenario.kill_target;
bool endless = false;
policy.time_limit = std::chrono::minutes(m_scenario.time_limit_minutes);
BBQAutoBattleResult result;
try{
run_auto_battle(logger, actions, policy);
result = run_auto_battle(logger, actions, policy);
}catch (const std::runtime_error&){
endless = true;
return "Expected the loop to stop, but it cleared more often than scripted.";
}
if (endless != m_scenario.expected_endless){
return endless
? "Expected the loop to stop, but it cleared more often than scripted."
: "Expected the loop to run past the script, but it stopped.";
if (result != m_scenario.expected_result){
return "Expected " + auto_battle_result_to_string(m_scenario.expected_result)
+ ", got " + auto_battle_result_to_string(result);
}
const std::string steps = join(actions.steps, " ");
if (steps != m_scenario.expected_steps){
Expand All @@ -266,7 +276,7 @@

}

static void add_tests_BBQAutoBattle(UnitTestDatabase& database){

Check failure on line 279 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_BBQAutoBattle( UnitTestDatabase & database): function_lines=87, limit=50, allowed 52
constexpr int PASS_SECONDS = ScriptedAutoBattle::FLY_SECONDS + ScriptedAutoBattle::CLEAR_SECONDS
+ ScriptedAutoBattle::RETURN_SECONDS + ScriptedAutoBattle::HEAL_SECONDS;
constexpr int LAST_PASS_SAVED_SECONDS = ScriptedAutoBattle::CLEAR_TAIL_SECONDS + ScriptedAutoBattle::HEAL_SECONDS;
Expand Down Expand Up @@ -306,12 +316,47 @@
.expected_seconds = 5 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS,
},
{
// Nothing bounds the loop: with no kills it never ends.
.name = "no kills never stops",
.kills_per_clear = {0, 0, 0, 0, 0, 0},
.expected_endless = true,
.expected_steps = passes(6) + " fly",
.expected_seconds = 6 * PASS_SECONDS + ScriptedAutoBattle::FLY_SECONDS,
.name = "gives up after 3 passes with no kill",
.kills_per_clear = {0, 0, 0},
.expected_result = BBQAutoBattleResult::NoKills,
.expected_steps = passes(2) + " " + LAST_PASS,
.expected_seconds = 3 * PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS,
.expected_log = {
"Auto Battle pass 1: 0 kills, 0/10 in total, HP ?, 46 s elapsed. Below the target: return, heal and go again.",
"Auto Battle pass 2: 0 kills, 0/10 in total, HP ?, 148 s elapsed. Below the target: return, heal and go again.",
"Auto Battle pass 3: 0 kills, 0/10 in total, HP ?, 250 s elapsed. No kill in 3 passes in a row: return and give up.",
},
},
{
// Only passes in a row count.
.name = "a kill resets the empty passes",
.kills_per_clear = {0, 0, 1, 0, 0, 0},
.expected_result = BBQAutoBattleResult::NoKills,
.expected_steps = passes(5) + " " + LAST_PASS,
.expected_seconds = 6 * PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS,
},
{
// The pass that ends past the limit is the last.
.name = "gives up at the time limit",
.time_limit_minutes = 5,
.kills_per_clear = {1, 1, 1, 1},
.expected_result = BBQAutoBattleResult::TimeLimit,
.expected_steps = passes(3) + " " + LAST_PASS,
.expected_seconds = 4 * PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS,
.expected_log = {
"Auto Battle pass 1: 1 kills, 1/10 in total, HP ?, 46 s elapsed. Below the target: return, heal and go again.",
"Auto Battle pass 2: 1 kills, 2/10 in total, HP ?, 148 s elapsed. Below the target: return, heal and go again.",
"Auto Battle pass 3: 1 kills, 3/10 in total, HP ?, 250 s elapsed. Below the target: return, heal and go again.",
"Auto Battle pass 4: 1 kills, 4/10 in total, HP ?, 352 s elapsed. Time limit of 5 min reached: return and give up.",
},
},
{
// Reaching the target counts as reached, even past the time limit.
.name = "target reached past the time limit",
.time_limit_minutes = 5,
.kills_per_clear = {3, 3, 3, 3},
.expected_steps = passes(3) + " " + LAST_PASS,
.expected_seconds = 4 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS,
},
};
for (const BBQAutoBattleScenario& scenario : scenarios){
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=1431, limit=1000, allowed 1420
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -1247,13 +1247,14 @@
day_skip_from_overworld(console, context);
}

BBQAutoBattleResult run_auto_battle(

Check warning on line 1250 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::run_auto_battle( Logger & logger , BBQAutoBattleActions & actions , const BBQAutoBattlePolicy & policy): function_lines=50 (advisory above 25, limit 50)
Logger& logger,
BBQAutoBattleActions& actions,
const BBQAutoBattlePolicy& policy
){
const WallClock start = actions.now();
uint64_t pass = 0;
uint64_t empty_passes = 0;
while (actions.kills() < policy.kill_target){
pass++;
const uint64_t kills_before = actions.kills();
Expand All @@ -1262,6 +1263,7 @@
const std::optional<double> hp = actions.clear(policy.kill_target);

const uint64_t kills = actions.kills();
empty_passes = kills == kills_before ? empty_passes + 1 : 0;
const auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(actions.now() - start);
const std::string hp_text = hp ? std::to_string((int)std::floor(*hp * 100)) + "%" : "?";

Expand All @@ -1270,6 +1272,12 @@
if (kills >= policy.kill_target){
end = BBQAutoBattleResult::TargetReached;
next = "Target reached: return without healing.";
}else if (empty_passes >= policy.max_empty_passes){
end = BBQAutoBattleResult::NoKills;
next = "No kill in " + std::to_string(empty_passes) + " passes in a row: return and give up.";
}else if (elapsed >= policy.time_limit){
end = BBQAutoBattleResult::TimeLimit;
next = "Time limit of " + std::to_string(policy.time_limit.count()) + " min reached: return and give up.";
}
logger.log(
"Auto Battle pass " + std::to_string(pass) + ": "
Expand Down Expand Up @@ -1382,7 +1390,7 @@

}

void quest_auto_battle(

Check warning on line 1393 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::quest_auto_battle( ProgramEnvironment & env , VideoStream & stream , ProControllerContext & context , const BBQOption & BBQ_OPTIONS , BBQuests current_quest): function_lines=33 (advisory above 25, limit 50)
ProgramEnvironment& env,
VideoStream& stream, ProControllerContext& context,
const BBQOption& BBQ_OPTIONS,
Expand All @@ -1402,11 +1410,14 @@
sensors.lets_go_kill
);

//The time limits are about 3x the slowest attempts seen with a correct lead.
BBQAutoBattlePolicy policy;
policy.kill_target = 10;
policy.time_limit = std::chrono::minutes(35);

if (current_quest == BBQuests::auto_30){
policy.kill_target = 30;
policy.time_limit = std::chrono::minutes(60);
}

ConsoleBBQAutoBattleActions actions(env, stream, context, stats, tracker, sensors.lets_go_hp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,24 @@ class BBQAutoBattleActions{

struct BBQAutoBattlePolicy{
uint64_t kill_target = 10;
//Give up after this many passes in a row with no kill.
uint64_t max_empty_passes = 3;
//Give up after the pass that ends past this time.
std::chrono::minutes time_limit = std::chrono::minutes(35);
};

//Why run_auto_battle() returned.
enum class BBQAutoBattleResult{
TargetReached,
NoKills, //max_empty_passes passes in a row had no kill.
TimeLimit,
};

//Clear Chargestone Cavern, one pass at a time, until the kills reach the target, and return why
//it stopped. It ends at Central Plaza, where the next quest starts. Logs one line per pass: its
//kills, the total, the lead's HP, the time spent, and what happens next.
//It gives up early when a limit in `policy` is reached; the board check then decides the quest
//failed.
BBQAutoBattleResult run_auto_battle(
Logger& logger,
BBQAutoBattleActions& actions,
Expand Down
Loading