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
@@ -1,4 +1,4 @@
/* AutoStory Battle

Check warning on line 1 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=968 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -269,13 +269,11 @@
m_kind == BattleKind::WILD_FIGHT ? "Battle: fighting a wild Pokemon." : "Battle: running from a wild Pokemon."
);
bool over = false;
while (!over){
while (!over && !m_try_end){
over = step();
}
m_console.log(std::format(
"Battle: over after {} turns{}.", m_decisions.turns(), m_decisions.lost() ? ", the starter fainted" : ""
));
return m_decisions.lost() ? BattleEnd::LOST : BattleEnd::WON;
m_console.log(std::format("Battle: over after {} turns{}.", m_decisions.turns(), m_try_end ? ", the plan ended the try" : m_decisions.lost() ? ", the starter fainted" : ""));
return m_try_end.value_or(m_decisions.lost() ? BattleEnd::LOST : BattleEnd::WON);
}

private:
Expand Down Expand Up @@ -571,7 +569,7 @@
status.battle_texts = std::exchange(m_policy_texts, {});
return status;
}
void take_turn(const ImageViewRGB32& screen){

Check warning on line 572 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::BattleLoop::take_turn( const ImageViewRGB32 & screen): function_lines=50 (advisory above 25, limit 50)
read_battlers(screen);
if (m_refusal.refused(current_time())){
m_console.log("Battle: the move list came back without a turn (Disable?); choosing another move.", COLOR_ORANGE);
Expand Down Expand Up @@ -610,6 +608,9 @@
case TurnChoice::SWITCH_LEAD:
m_decisions.turn_done(action.choice, switch_lead_in(m_console, m_context, m_language, m_supplies.lead.species) ? TurnResult::DONE : TurnResult::NOT_DONE);
return;
case TurnChoice::END_TRY:
m_try_end = action.try_end;
return;
case TurnChoice::MOVE:
m_console.log(std::format("Battle: move {}.", action.slot + 1));
m_refusal.move_chosen(current_time());
Expand Down Expand Up @@ -697,6 +698,7 @@
bool m_move_chosen = false;
// RUN was chosen and no battle menu has shown since.
bool m_ran = false;
std::optional<BattleEnd> m_try_end;
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ enum class BattleEnd{
// by the next member, except in the lab's rival battle, which the
// story goes on from.
LOST,
// A League fight's try ended mid-battle (TurnChoice::END_TRY), for the
// League loop to soft reset: a Roar or Whirlwind landed in a fight that
// resets on one, or the plan's early reset (Bruno's after Onix #1).
ROARED,
EARLY_RESET,
};


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* AutoStory Battle Decisions

Check warning on line 1 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=751 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -302,6 +302,13 @@
if (m_policy){
m_policy.emplace(m_supplies.policy);
}
if (m_planned && m_planned->resets_on_roar()){
return TurnAction{
.choice = TurnChoice::END_TRY,
.heal_reason = std::format("our {} is out, not sent: a Roar or Whirlwind landed; the plan resets the try", m_supplies.own_out),
.try_end = BattleEnd::ROARED,
};
}
if (m_planned){
m_planned->lead_switched_in();
}
Expand Down Expand Up @@ -352,7 +359,7 @@
};
}

TurnAction BattleDecisions::planned_turn(const PartyStatus& status){

Check warning on line 362 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::BattleDecisions::planned_turn( const PartyStatus & status): function_lines=35 (advisory above 25, limit 50)
const PolicyView view{
.hp = lead_hp(m_supplies, status).value_or(static_cast<double>(m_supplies.lead.stats->hp)),
.max_hp = static_cast<double>(m_supplies.lead.stats->hp),
Expand All @@ -368,6 +375,9 @@
turn.choice = PlannedChoice{.row = "items_off"};
}
const std::string reason = std::format("planned: {}", turn.log);
if (turn.early_reset){
return TurnAction{.choice = TurnChoice::END_TRY, .heal_reason = reason, .try_end = BattleEnd::EARLY_RESET};
}
if (turn.choice.item){
m_planned_item = *turn.choice.item;
return TurnAction{
Expand Down Expand Up @@ -524,7 +534,7 @@
m_items_off = true;
}

void BattleDecisions::turn_done(TurnChoice choice, TurnResult result){

Check warning on line 537 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::BattleDecisions::turn_done( TurnChoice choice , TurnResult result): function_lines=40 (advisory above 25, limit 50)
if (result == TurnResult::REFUSED){
refused(choice);
return;
Expand Down Expand Up @@ -560,6 +570,7 @@
case TurnChoice::RUN:
case TurnChoice::MOVE:
case TurnChoice::SWITCH_LEAD:
case TurnChoice::END_TRY:
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ enum class TurnChoice{
// The lead back in through POKEMON and SHIFT: a Roar or Whirlwind
// dragged it out (lead_on_field()).
SWITCH_LEAD,
// A League fight's try ends here (TurnAction::try_end), mid-battle:
// the League loop soft resets to the fight's save.
END_TRY,
};

// Before a battle of `kind`'s first turn: whether it reads the Items pocket
Expand All @@ -60,6 +63,8 @@ struct TurnAction{
std::string heal_reason = {};
// For BATTLE_ITEM, the item's slug.
std::string item = {};
// For END_TRY, how the try ends (ROARED, EARLY_RESET).
std::optional<BattleEnd> try_end = std::nullopt;
};

// What came of a TurnAction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ Outcome battle(StoryEnv& env, const StoryStep& step){
const BattleKind kind = arg<bool>(step, "run") ? BattleKind::WILD_RUN : BattleKind::TRAINER;
const BattleEnd end = fight_battle(env.console, env.context, env.supplies, kind, env.checks, env.language);
end_trainer_battle(env.supplies);
if (end == BattleEnd::LOST && !arg<bool>(step, "may_lose")){
return Pokemon::outcome_failed("blackout");
if (end != BattleEnd::WON && (end != BattleEnd::LOST || !arg<bool>(step, "may_lose"))){
return Pokemon::outcome_failed(end == BattleEnd::LOST ? "blackout" : end == BattleEnd::ROARED ? "league_roar_reset" : "league_early_reset");
}
mash_until_overworld(env.console, env.context, SCENE_TIMEOUT);
// A trainer's script can go on after the battle, its black while the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/* AutoStory League Loop
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <optional>
#include <string>
#include "PokemonFRLG_AutoStoryLeagueLoop.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonFRLG{


namespace{

std::string_view end_name(LeagueBattleEnd end){
switch (end){
case LeagueBattleEnd::WON:
return "won";
case LeagueBattleEnd::LOST:
return "lost";
case LeagueBattleEnd::ROARED:
return "reset on a Roar or Whirlwind";
case LeagueBattleEnd::EARLY_RESET:
return "early reset";
}
return "";
}

std::string counts_text(const SlugCounts& counts){
std::string text;
for (const auto& [name, count] : counts){
if (count != 0){
text += std::format("{}{} {}", text.empty() ? "" : ", ", name, count);
}
}
return text.empty() ? std::string("nothing") : text;
}

// The first of `floors` that `counts` is below, as a line; none if none.
std::optional<std::string> below_floor(std::string_view what, const SlugCounts& counts, const SlugCounts& floors){
for (const auto& [name, floor] : floors){
const int have = count_of(counts, name);
if (have < floor){
return std::format("{} {}: {}, at least {}", what, name, have, floor);
}
}
return std::nullopt;
}

// What `before` has more of than `after`.
SlugCounts spent(const SlugCounts& before, const SlugCounts& after){
SlugCounts used;
for (const auto& [item, count] : before){
const int less = count - count_of(after, item);
if (less > 0){
used[item] = less;
}
}
return used;
}

class FightLoop{
public:
FightLoop(const LeagueFightData& fight, const LeagueFightRunner& runner)
: m_fight(fight)
, m_runner(runner)
{}

LeagueFightReport run(){

Check warning on line 73 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::FightLoop::run(): function_lines=30 (advisory above 25, limit 50)
if (!m_fight.pass){
m_runner.log(std::format("League {}: the plan has no pass figures; stopping.", m_fight.name));
return std::move(m_report);
}
const LeaguePassFigures& figures = *m_fight.pass;
for (int c = 0; c < figures.min_pass_of; c++){
if (!try_once(1, c, figures.min_pass_of)){

Check warning on line 80 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: try_once(): literal argument 1 needs a /*param=*/ comment (Google "Function Arguments", user review on fork #590)
return std::move(m_report);
}
m_report.phase1_passes += m_report.tries.back().passes ? 1 : 0;
}
if (m_report.phase1_passes < figures.min_pass){
return stop(LeagueFightVerdict::BELOW_MIN_PASS, std::format(
"phase 1 passed {} of {}, under {}", m_report.phase1_passes, figures.min_pass_of, figures.min_pass
));
}
for (int c = 0; c < figures.reset_budget; c++){
if (!try_once(2, c, figures.reset_budget)){

Check warning on line 91 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: try_once(): literal argument 2 needs a /*param=*/ comment (Google "Function Arguments", user review on fork #590)
return std::move(m_report);
}
if (m_report.tries.back().passes){
m_runner.save();
m_runner.log(std::format("League {}: saved after phase 2 try {}.", m_fight.name, c + 1));
m_report.verdict = LeagueFightVerdict::SAVED;
return std::move(m_report);
}
}
return stop(LeagueFightVerdict::OUT_OF_BUDGET, std::format("no pass in {} phase 2 tries", figures.reset_budget));
}

private:
LeagueFightReport stop(LeagueFightVerdict verdict, std::string detail){
m_runner.log(std::format("League {}: {}; stopping.", m_fight.name, detail));
m_report.verdict = verdict;
m_report.detail = std::move(detail);
return std::move(m_report);
}

// One try from the start save; false if the entry contract broke.
bool try_once(int phase, int index, int of){

Check warning on line 113 in SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::FightLoop::try_once( int phase , int index , int of): function_lines=35 (advisory above 25, limit 50)
m_runner.load_start();
const LeagueSide start = m_runner.read_side();
LeagueTryRecord record{.phase = phase, .top_up = m_runner.top_up()};
const LeagueSide entry = m_runner.read_side();
if (std::optional<std::string> broken = entry_broken(entry)){
stop(LeagueFightVerdict::PRECONDITION_FAILED, std::format("entry contract: {}", *broken));
return false;
}
record.end = m_runner.fight();
std::optional<std::string> after;
if (record.end == LeagueBattleEnd::WON){
const LeagueSide end = m_runner.read_side();
record.used = spent(start.bag, end.bag);
after = below_floor("item", end.bag, m_fight.reserve_items);
after = after ? after : below_floor("PP", end.pp, m_fight.reserve_pp);
}
record.passes = !after && league_try_passes(m_fight, LeagueTry{
.won = record.end == LeagueBattleEnd::WON,
.roared = record.end == LeagueBattleEnd::ROARED,
.early_reset = record.end == LeagueBattleEnd::EARLY_RESET,
.used = record.used,
});
m_runner.log(std::format(
"League {}: phase {} try {}/{}: HP {}/{}, top-up [{}], {}, used {}{}: {}.",
m_fight.name, phase, index + 1, of, entry.hp, entry.max_hp, counts_text(counted(record.top_up)),
end_name(record.end), counts_text(record.used), after ? std::format(", {}", *after) : "",
record.passes ? "pass" : "no pass"
));
if (after){
m_report.detail = *after;
}
m_report.tries.push_back(std::move(record));
return true;
}

// The entry contract: every item at least its count (hard); a move's
// PP under its count only logged.
std::optional<std::string> entry_broken(const LeagueSide& entry) const{
if (std::optional<std::string> pp = below_floor("PP", entry.pp, m_fight.entry_pp)){
m_runner.log(std::format("League {}: entry {}; fighting on.", m_fight.name, *pp));
}
return below_floor("item", entry.bag, m_fight.entry_items);
}

static SlugCounts counted(const std::vector<std::string>& items){
SlugCounts counts;
for (const std::string& item : items){
counts[item]++;
}
return counts;
}

const LeagueFightData& m_fight;
const LeagueFightRunner& m_runner;
LeagueFightReport m_report;
};

} // namespace


LeagueFightReport run_league_fight(const LeagueFightData& fight, const LeagueFightRunner& runner){
return FightLoop(fight, runner).run();
}



} // namespace PokemonFRLG
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Loading
Loading