diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp index 209b0bdbc0..64a3249f0e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp @@ -269,13 +269,11 @@ class BattleLoop{ 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: @@ -610,6 +608,9 @@ class BattleLoop{ 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()); @@ -697,6 +698,7 @@ class BattleLoop{ bool m_move_chosen = false; // RUN was chosen and no battle menu has shown since. bool m_ran = false; + std::optional m_try_end; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h index a6bdb6d528..da7546b2ae 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h @@ -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, }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp index be17cfbc88..9b58ae7e6b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp @@ -302,6 +302,13 @@ TurnAction BattleDecisions::switch_lead_back(){ 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(); } @@ -368,6 +375,9 @@ TurnAction BattleDecisions::planned_turn(const PartyStatus& status){ 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{ @@ -560,6 +570,7 @@ void BattleDecisions::turn_done(TurnChoice choice, TurnResult result){ case TurnChoice::RUN: case TurnChoice::MOVE: case TurnChoice::SWITCH_LEAD: + case TurnChoice::END_TRY: return; } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h index 3b546c0c00..e01e3af2be 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h @@ -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 @@ -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 try_end = std::nullopt; }; // What came of a TurnAction. diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index 09d1c3417c..4eddab3524 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -281,8 +281,8 @@ Outcome battle(StoryEnv& env, const StoryStep& step){ const BattleKind kind = arg(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(step, "may_lose")){ - return Pokemon::outcome_failed("blackout"); + if (end != BattleEnd::WON && (end != BattleEnd::LOST || !arg(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 diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.cpp new file mode 100644 index 0000000000..e00ddf8817 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.cpp @@ -0,0 +1,182 @@ +/* AutoStory League Loop + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#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 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(){ + 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)){ + 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)){ + 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){ + 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 broken = entry_broken(entry)){ + stop(LeagueFightVerdict::PRECONDITION_FAILED, std::format("entry contract: {}", *broken)); + return false; + } + record.end = m_runner.fight(); + std::optional 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 entry_broken(const LeagueSide& entry) const{ + if (std::optional 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& 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 diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.h new file mode 100644 index 0000000000..1c410f95f0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.h @@ -0,0 +1,114 @@ +/* AutoStory League Loop + * + * From: https://github.com/PokemonAutomation/ + * + * The League fight by fight (the user's design, 2026-11-17): each fight + * starts from its own save, where the fight before it was won. A try + * loads that save, tops our side up (planned_between()), checks the + * fight's entry contract, fights, and passes on a win that spends no item + * over the fight's caps and leaves its reserves. + * + * Phase 1: the plan's min_pass_of tries (10), none saved. Fewer passes + * than min_pass: the run stops. Phase 2: up to reset_budget more tries, + * until one passes, which is saved. A try that a Roar, a Whirlwind or the + * plan's early reset ended is a try that didn't pass. + * + * The loop drives a LeagueFightRunner: the console's, or a fake one that + * plays scripted battles (PokemonFRLG_AutoStoryLeagueLoop_Tests.cpp). + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryLeagueLoop_H +#define PokemonAutomation_PokemonFRLG_AutoStoryLeagueLoop_H + +#include +#include +#include +#include +#include "PokemonFRLG_AutoStoryLeagueFightData.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// Our side as the loop reads it: the lead's HP, its moves' PP and the bag. +struct LeagueSide{ + int hp = 0; + int max_hp = 0; + SlugCounts pp = {}; + SlugCounts bag = {}; +}; + +// How a try's battle ended. +enum class LeagueBattleEnd{ + WON, + LOST, + // A Roar or Whirlwind landed in a fight that resets on one; the game + // was soft reset. + ROARED, + // The plan's early reset (Bruno's after Onix #1); the game was soft reset. + EARLY_RESET, +}; + +// Where the loop's lines go. +using LeagueLog = std::function; + +// What the loop drives. +struct LeagueFightRunner{ + // Back to the fight's start save. + std::function load_start; + // The top-ups before the fight (planned_between()); the items used. + std::function()> top_up; + // Our side as it is now. + std::function read_side; + // The fight, to its end. + std::function fight; + // Saves the game where it is. + std::function save; + LeagueLog log; +}; + +// How the loop left a fight. +enum class LeagueFightVerdict{ + // A phase 2 try passed and was saved: on to the next fight. + SAVED, + // Phase 1 passed fewer than the plan's min_pass. + BELOW_MIN_PASS, + // No phase 2 try passed within the reset budget. + OUT_OF_BUDGET, + // A try's side broke the fight's entry contract, before fighting. + PRECONDITION_FAILED, + // The plan has no pass figures for the fight. + NO_FIGURES, +}; + +// One try. +struct LeagueTryRecord{ + int phase = 1; + std::vector top_up = {}; + LeagueBattleEnd end = LeagueBattleEnd::LOST; + // Every item the try spent, the top-ups included; empty unless won. + SlugCounts used = {}; + bool passes = false; +}; + +// The tries of a fight and how the loop left it. +struct LeagueFightReport{ + LeagueFightVerdict verdict = LeagueFightVerdict::NO_FIGURES; + std::vector tries = {}; + int phase1_passes = 0; + // What broke the entry contract, or the postcondition of the last try. + std::string detail = {}; +}; + +// `fight` through phases 1 and 2 on `runner`, every try logged with its +// counts. +LeagueFightReport run_league_fight(const LeagueFightData& fight, const LeagueFightRunner& runner); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop_Tests.cpp new file mode 100644 index 0000000000..5ca96e9d18 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop_Tests.cpp @@ -0,0 +1,366 @@ +/* AutoStory League Loop Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The League loop (PokemonFRLG_AutoStoryLeagueLoop.h) on a fake runner: + * each try's battle is scripted as how it ended and the items it spent, + * and the top-ups between fights are planned_between()'s. The cases check + * the phases, the saves, the entry contract and the top-ups. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonFRLG_AutoStoryLeagueFightData.h" +#include "PokemonFRLG_AutoStoryLeagueLoop.h" +#include "PokemonFRLG_AutoStoryLeagueLoop_Tests.h" +#include "PokemonFRLG_AutoStoryPlannedBetween.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +constexpr int OUR_MAX_HP = 171; +const SlugCounts FULL_PP = {{"bite", 25}, {"surf", 15}, {"blizzard", 5}, {"strength", 15}}; + +// The bag the League starts with (PokemonSimulation BAG_NOW). +SlugCounts league_bag(){ + return { + {"full-restore", 8}, {"x-special", 15}, {"x-speed", 5}, {"x-accuracy", 3}, {"guard-spec", 2}, + {"max-elixir", 1}, {"elixir", 1}, {"super-potion", 6}, {"full-heal", 2}, {"parlyz-heal", 2}, {"antidote", 1}, + }; +} + +// A try's battle: how it ended and the items it spent. +struct ScriptedBattle{ + LeagueBattleEnd end = LeagueBattleEnd::LOST; + SlugCounts used = {}; +}; + +// A win on no items. +ScriptedBattle win(){ + return ScriptedBattle{.end = LeagueBattleEnd::WON}; +} +// A win on two Full Restores, over Lorelei's cap of one. +ScriptedBattle win_on_two_full_restores(){ + return ScriptedBattle{.end = LeagueBattleEnd::WON, .used = {{"full-restore", 2}}}; +} +ScriptedBattle lost(){ + return ScriptedBattle{.end = LeagueBattleEnd::LOST}; +} +ScriptedBattle roared(){ + return ScriptedBattle{.end = LeagueBattleEnd::ROARED, .used = {{"x-speed", 1}}}; +} +ScriptedBattle early_reset(){ + return ScriptedBattle{.end = LeagueBattleEnd::EARLY_RESET, .used = {{"x-speed", 3}}}; +} + +std::vector repeated(size_t count, const ScriptedBattle& battle){ + return std::vector(count, battle); +} +std::vector joined(std::vector first, const std::vector& then){ + first.insert(first.end(), then.begin(), then.end()); + return first; +} + +// The fight's start save: full HP and PP, `bag`. +LeagueSide full_side(SlugCounts bag){ + return LeagueSide{.hp = OUR_MAX_HP, .max_hp = OUR_MAX_HP, .pp = FULL_PP, .bag = std::move(bag)}; +} + +SlugCounts changed(SlugCounts counts, const SlugCounts& set){ + for (const auto& [name, count] : set){ + counts[name] = count; + } + return counts; +} + +std::string items_text(const std::vector& items){ + std::string text; + for (const std::string& item : items){ + text += text.empty() ? item : std::format(", {}", item); + } + return text; +} + +// The fight's start save, and each try's battle in order (the last one +// repeats). +class FakeLeagueRunner{ +public: + FakeLeagueRunner(const LeagueFightData& fight, LeagueSide save, std::vector battles) + : m_fight(fight) + , m_save(std::move(save)) + , m_battles(std::move(battles)) + {} + + LeagueFightRunner runner(){ + return LeagueFightRunner{ + .load_start = [this]{ m_now = m_save; }, + .top_up = [this]{ return top_up(); }, + .read_side = [this]{ return m_now; }, + .fight = [this]{ return fight(); }, + .save = [this]{ m_saves++; }, + .log = [](std::string_view){}, + }; + } + int saves() const{ + return m_saves; + } + int fights() const{ + return m_fights; + } + +private: + std::vector top_up(){ + PlannedCarry carry{.hp = m_now.hp, .max_hp = m_now.max_hp, .pp = m_now.pp, .full_pp = FULL_PP, .bag = m_now.bag}; + std::vector used = planned_between(carry, m_fight); + m_now.hp = carry.hp; + m_now.pp = carry.pp; + m_now.bag = carry.bag; + return used; + } + LeagueBattleEnd fight(){ + const ScriptedBattle& battle = m_battles[std::min(m_fights, m_battles.size() - 1)]; + m_fights++; + for (const auto& [item, count] : battle.used){ + m_now.bag[item] -= count; + } + return battle.end; + } + + const LeagueFightData& m_fight; + LeagueSide m_save; + LeagueSide m_now; + std::vector m_battles; + int m_saves = 0; + int m_fights = 0; +}; + +std::string_view verdict_name(LeagueFightVerdict verdict){ + switch (verdict){ + case LeagueFightVerdict::SAVED: + return "saved"; + case LeagueFightVerdict::BELOW_MIN_PASS: + return "below min_pass"; + case LeagueFightVerdict::OUT_OF_BUDGET: + return "out of budget"; + case LeagueFightVerdict::PRECONDITION_FAILED: + return "precondition failed"; + case LeagueFightVerdict::NO_FIGURES: + return "no figures"; + } + return ""; +} + +struct LoopCase{ + struct Input{ + std::string fight = {}; + LeagueSide save = {}; + std::vector battles = {}; + }; + struct Expectation{ + LeagueFightVerdict verdict = LeagueFightVerdict::SAVED; + int phase1_passes = 0; + int fights = 0; + int saves = 0; + // The first try's top-ups, when checked. + std::optional> top_up = std::nullopt; + // The last try's end, when checked. + std::optional last_end = std::nullopt; + }; + + std::string name = {}; + Input input = {}; + Expectation expectation = {}; +}; + +class Test_LeagueLoop : public UnitTest{ +public: + explicit Test_LeagueLoop(LoopCase test_case) + : UnitTest(std::format("PokemonFRLG::AutoStoryLeagueLoop - fake runner: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const LeagueFightData* fight = league_fight_data(league_plan_data(), m_case.input.fight); + if (fight == nullptr){ + return std::format("no fight {}.", m_case.input.fight); + } + FakeLeagueRunner fake(*fight, m_case.input.save, m_case.input.battles); + const LeagueFightReport report = run_league_fight(*fight, fake.runner()); + const LoopCase::Expectation& want = m_case.expectation; + if (report.verdict != want.verdict || report.phase1_passes != want.phase1_passes + || fake.fights() != want.fights || fake.saves() != want.saves + ){ + return std::format("{}, phase 1 passes {}, fights {}, saves {}; expected {}, {}, {}, {}.", + verdict_name(report.verdict), report.phase1_passes, fake.fights(), fake.saves(), + verdict_name(want.verdict), want.phase1_passes, want.fights, want.saves); + } + if (want.top_up && (report.tries.empty() || report.tries.front().top_up != *want.top_up)){ + return std::format("top-up [{}]; expected [{}].", + report.tries.empty() ? std::string() : items_text(report.tries.front().top_up), items_text(*want.top_up)); + } + if (want.last_end && (report.tries.empty() || report.tries.back().end != *want.last_end)){ + return "the last try ended otherwise."; + } + return true; + } + +private: + LoopCase m_case; +}; + +// Bruno's start save: Lorelei won on her caps (a Full Restore, six X Specials). +SlugCounts bruno_bag(){ + return changed(league_bag(), {{"full-restore", 7}, {"x-special", 9}}); +} + +const LoopCase LOOP_CASES[] = { + { + .name = "Lorelei: ten passes, saved on the first phase 2 try", + .input = {.fight = "lorelei", .save = full_side(league_bag()), .battles = {win()}}, + .expectation = {.verdict = LeagueFightVerdict::SAVED, .phase1_passes = 10, .fights = 11, .saves = 1}, + }, + { + .name = "Lorelei: four passes in phase 1, under min_pass 5: stop", + .input = {.fight = "lorelei", .save = full_side(league_bag()), .battles = joined(repeated(4, win()), {lost()})}, + .expectation = {.verdict = LeagueFightVerdict::BELOW_MIN_PASS, .phase1_passes = 4, .fights = 10, .saves = 0}, + }, + { + .name = "Lorelei: phase 2 passes on its third try: saved", + .input = { + .fight = "lorelei", + .save = full_side(league_bag()), + .battles = joined(joined(repeated(5, win()), repeated(7, lost())), {win()}), + }, + .expectation = {.verdict = LeagueFightVerdict::SAVED, .phase1_passes = 5, .fights = 13, .saves = 1}, + }, + { + .name = "Lorelei: no pass in the reset budget of 5: stop", + .input = {.fight = "lorelei", .save = full_side(league_bag()), .battles = joined(repeated(5, win()), {lost()})}, + .expectation = {.verdict = LeagueFightVerdict::OUT_OF_BUDGET, .phase1_passes = 5, .fights = 15, .saves = 0}, + }, + { + .name = "Lorelei: phase 2 wins over the Full Restore cap: never saved", + .input = { + .fight = "lorelei", + .save = full_side(league_bag()), + .battles = joined(repeated(5, win()), {win_on_two_full_restores()}), + }, + .expectation = { + .verdict = LeagueFightVerdict::OUT_OF_BUDGET, .phase1_passes = 5, .fights = 15, .saves = 0, + .last_end = LeagueBattleEnd::WON, + }, + }, + { + .name = "Bruno: a Roar, then early resets after Onix: tries that don't pass", + .input = {.fight = "bruno", .save = full_side(bruno_bag()), .battles = {roared(), early_reset()}}, + .expectation = { + .verdict = LeagueFightVerdict::BELOW_MIN_PASS, .phase1_passes = 0, .fights = 10, .saves = 0, + .last_end = LeagueBattleEnd::EARLY_RESET, + }, + }, + { + .name = "Lorelei's save is a Full Restore short of the entry contract: stop before fighting", + .input = {.fight = "lorelei", .save = full_side(changed(league_bag(), {{"full-restore", 7}})), .battles = {win()}}, + .expectation = {.verdict = LeagueFightVerdict::PRECONDITION_FAILED, .phase1_passes = 0, .fights = 0, .saves = 0}, + }, + { + .name = "before Bruno, 40 HP missing: one Super Potion", + .input = { + .fight = "bruno", + .save = LeagueSide{.hp = OUR_MAX_HP - 40, .max_hp = OUR_MAX_HP, .pp = FULL_PP, .bag = bruno_bag()}, + .battles = {lost()}, + }, + .expectation = { + .verdict = LeagueFightVerdict::BELOW_MIN_PASS, .phase1_passes = 0, .fights = 10, .saves = 0, + .top_up = std::vector{"super-potion"}, + }, + }, + { + .name = "before Bruno, 90 HP missing and two Super Potions spare: two", + .input = { + .fight = "bruno", + .save = LeagueSide{ + .hp = OUR_MAX_HP - 90, .max_hp = OUR_MAX_HP, .pp = FULL_PP, .bag = changed(bruno_bag(), {{"super-potion", 7}}), + }, + .battles = {lost()}, + }, + .expectation = { + .verdict = LeagueFightVerdict::BELOW_MIN_PASS, .phase1_passes = 0, .fights = 10, .saves = 0, + .top_up = std::vector{"super-potion", "super-potion"}, + }, + }, + { + .name = "before Bruno, 120 HP missing: a Full Restore", + .input = { + .fight = "bruno", + .save = LeagueSide{.hp = OUR_MAX_HP - 120, .max_hp = OUR_MAX_HP, .pp = FULL_PP, .bag = bruno_bag()}, + .battles = {lost()}, + }, + .expectation = { + .verdict = LeagueFightVerdict::BELOW_MIN_PASS, .phase1_passes = 0, .fights = 10, .saves = 0, + .top_up = std::vector{"full-restore"}, + }, + }, + { + .name = "before the Champion, Surf 5 and Blizzard 2: the Elixir", + .input = { + .fight = "champion", + .save = LeagueSide{ + .hp = OUR_MAX_HP, .max_hp = OUR_MAX_HP, + .pp = changed(FULL_PP, {{"surf", 5}, {"blizzard", 2}}), + .bag = { + {"full-restore", 2}, {"x-special", 2}, {"x-speed", 1}, {"x-accuracy", 3}, {"guard-spec", 2}, + {"elixir", 1}, {"max-elixir", 1}, + }, + }, + .battles = {lost()}, + }, + .expectation = { + .verdict = LeagueFightVerdict::BELOW_MIN_PASS, .phase1_passes = 0, .fights = 10, .saves = 0, + .top_up = std::vector{"elixir"}, + }, + }, + { + .name = "before Lance, Surf 3: no Elixir", + .input = { + .fight = "lance", + .save = LeagueSide{ + .hp = OUR_MAX_HP, .max_hp = OUR_MAX_HP, + .pp = changed(FULL_PP, {{"surf", 3}}), + .bag = { + {"full-restore", 4}, {"x-special", 4}, {"x-speed", 1}, {"x-accuracy", 3}, {"guard-spec", 2}, + {"super-potion", 2}, {"elixir", 1}, {"max-elixir", 1}, + }, + }, + .battles = {lost()}, + }, + .expectation = { + .verdict = LeagueFightVerdict::BELOW_MIN_PASS, .phase1_passes = 0, .fights = 10, .saves = 0, + .top_up = std::vector{}, + }, + }, +}; + +} // namespace + + +void add_tests_AutoStoryLeagueLoop(UnitTestDatabase& database){ + for (const LoopCase& test_case : LOOP_CASES){ + database.add(test_case); + } +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop_Tests.h new file mode 100644 index 0000000000..124d06f754 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop_Tests.h @@ -0,0 +1,28 @@ +/* AutoStory League Loop Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Registration of the League loop's tests on a fake runner + * (PokemonFRLG_AutoStoryLeagueLoop_Tests.cpp). + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryLeagueLoop_Tests_H +#define PokemonAutomation_PokemonFRLG_AutoStoryLeagueLoop_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// Registers the League loop's tests. +void add_tests_AutoStoryLeagueLoop(UnitTestDatabase& database); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeaguePlanData.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeaguePlanData.cpp index 75bef27f58..19f6a3f3fd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeaguePlanData.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeaguePlanData.cpp @@ -129,6 +129,16 @@ LeaguePassFigures pass_of(const JsonObject& pass){ }; } +void read_contract(const JsonObject& contract, LeagueFightData& fight){ + if (const JsonObject* pass = contract.get_object("pass")){ + fight.pass = pass_of(*pass); + } + if (const JsonObject* entry = contract.get_object("entry")){ + fight.entry_items = counts_of(entry->get_object("items"), true); + fight.entry_pp = counts_of(entry->get_object("pp"), false); + } +} + LeagueFightData fight_of(const std::string& name, const JsonObject& object){ LeagueFightData fight{.name = name, .goal = object.get_string_default("goal")}; for (const JsonValue& foe : object.get_array_throw("foes")){ @@ -150,9 +160,7 @@ LeagueFightData fight_of(const std::string& name, const JsonObject& object){ read_reset(*reset, fight); } if (const JsonObject* contract = object.get_object("contract")){ - if (const JsonObject* pass = contract->get_object("pass")){ - fight.pass = pass_of(*pass); - } + read_contract(*contract, fight); } return fight; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeaguePlanData.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeaguePlanData.h index b672a75ddb..1c421e7fb1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeaguePlanData.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeaguePlanData.h @@ -69,6 +69,11 @@ struct LeagueFightData{ bool reset_on_roar = false; std::optional early_reset = std::nullopt; std::optional pass = std::nullopt; + // The entry contract (contract.entry): the least of each item and of + // each move's PP our side starts the fight with, the top-ups before it + // done. + SlugCounts entry_items = {}; + SlugCounts entry_pp = {}; // PP this fight must not spend, by move, and the items it must leave // in the bag. SlugCounts reserve_pp = {}; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.cpp index f9a1789640..c381d09211 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.cpp @@ -360,6 +360,16 @@ PlannedBattle::Turn PlannedBattle::choose(const Supplies& supplies, const Policy .status = view.status, .foe = foe_out(supplies, view), }; + // The foe out last turn fell. + const std::optional fallen = m_slot && *m_slot < inputs.foe.slot ? m_slot : std::nullopt; + m_slot = inputs.foe.slot; + const int speed = m_track.state().own.speed; + if (fallen && league_early_reset(*m_fight, *fallen, speed, m_used)){ + return Turn{ + .log = std::format("{}: the foe at slot {} fell with our Speed at {}: the plan's early reset", m_fight->name, *fallen + 1, speed), + .early_reset = true, + }; + } inputs.track = m_track.state(); for (std::string_view item : LEAGUE_ITEMS){ inputs.bag[std::string(item)] = bag_count(supplies, item); @@ -375,6 +385,11 @@ PlannedBattle::Turn PlannedBattle::choose(const Supplies& supplies, const Policy void PlannedBattle::item_used(std::string_view item){ m_track.item_used(item); + m_used[std::string(item)]++; +} + +bool PlannedBattle::resets_on_roar() const{ + return m_fight->reset_on_roar; } void PlannedBattle::lead_switched_in(){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.h index 35f20023d2..0503d3b936 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.h @@ -72,11 +72,16 @@ class PlannedBattle{ struct Turn{ PlannedChoice choice = {}; std::string log = {}; + // The plan's early reset ends the try here (league_early_reset()): + // no choice. + bool early_reset = false; }; Turn choose(const Supplies& supplies, const PolicyView& view); void item_used(std::string_view item); // Back in after Roar or Whirlwind: its stages are gone. void lead_switched_in(); + // A Roar or Whirlwind that lands ends the try instead. + bool resets_on_roar() const; private: PlannedFoeTrack foe_out(const Supplies& supplies, const PolicyView& view); @@ -86,6 +91,9 @@ class PlannedBattle{ const LeagueTrainerData* m_trainer; BattleTrack m_track; PlannedStreak m_streak; + // The items used in the fight, and the foe's party slot last turn. + SlugCounts m_used; + std::optional m_slot; }; // The League fight `trainer` (a battle step's "trainer") is fought by diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable_Tests.cpp index 913b2d61fe..7e0e6fce5f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable_Tests.cpp @@ -25,6 +25,7 @@ #include "PokemonFRLG_AutoStoryRooms.h" #include "PokemonFRLG_AutoStoryBattleBag.h" #include "PokemonFRLG_AutoStoryBattleTrack.h" +#include "PokemonFRLG_AutoStoryLeagueLoop_Tests.h" #include "PokemonFRLG_AutoStoryLeagueParity_Tests.h" #include "PokemonFRLG_AutoStoryPlannedBattle.h" #include "PokemonFRLG_AutoStoryPlannedPolicy.h" @@ -343,6 +344,7 @@ void add_tests_AutoStoryStepTable(UnitTestDatabase& database){ add_tests_AutoStoryPlannedPolicy(database); add_tests_AutoStoryPlannedBattle(database); add_tests_AutoStoryPlannedBattleFake(database); + add_tests_AutoStoryLeagueLoop(database); add_tests_AutoStoryLeagueParity(database); add_tests_AutoStoryBattleTrack(database); } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 4ad3ca2ee7..a8a736828c 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2230,6 +2230,10 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueFightData.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueFightData.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueFoePlan.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop_Tests.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop_Tests.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueSteps.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueSteps.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows13_Tests.h