From e61a3d4ccffdf2fb9d37d13a5595d7f118f56437 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 17 Nov 2026 01:26:55 -0800 Subject: [PATCH 1/4] FRLG League loop: each fight's phases from its start save run_league_fight() tries a fight from its start save through a LeagueFightRunner: load, top up, check the entry contract (hard on items), fight, check the caps and reserves. Phase 1: min_pass_of tries, none saved, stop below min_pass; phase 2: up to reset_budget tries, save the first pass. The plan's contract.entry is read for the check. Tests run the loop on a fake runner whose fights are the fake engine's scripted battles. --- .../PokemonFRLG_AutoStoryLeagueLoop.cpp | 182 +++++++++ .../PokemonFRLG_AutoStoryLeagueLoop.h | 110 ++++++ .../PokemonFRLG_AutoStoryLeaguePlanData.cpp | 14 +- .../PokemonFRLG_AutoStoryLeaguePlanData.h | 5 + ...nFRLG_AutoStoryPlannedBattleFake_Tests.cpp | 367 ++++++++++++++++++ SerialPrograms/cmake/SourceFiles.cmake | 2 + 6 files changed, 677 insertions(+), 3 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.h 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..9e93c3fb38 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.h @@ -0,0 +1,110 @@ +/* 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_AutoStoryPlannedBattleFake_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, +}; + +// 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; + std::function 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; +}; + +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_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_AutoStoryPlannedBattleFake_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattleFake_Tests.cpp index 5727309711..ab54f1f366 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattleFake_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattleFake_Tests.cpp @@ -12,6 +12,10 @@ * plan says: a Roar or Whirlwind in a fight that resets on one, Bruno's * early reset after Onix #1, a win or a loss. * + * The League loop (PokemonFRLG_AutoStoryLeagueLoop.h) runs on a fake + * runner whose fights are these scripted battles: its phases, its saves, + * the entry contract and the top-ups between fights. + * */ #include @@ -26,7 +30,9 @@ #include "Common/Cpp/TestRunners/UnitTest.h" #include "PokemonFRLG_AutoStoryLeagueFightData.h" #include "PokemonFRLG_AutoStoryLeagueFoes.h" +#include "PokemonFRLG_AutoStoryLeagueLoop.h" #include "PokemonFRLG_AutoStoryPlannedBattle.h" +#include "PokemonFRLG_AutoStoryPlannedBetween.h" #include "PokemonFRLG_AutoStorySupplies.h" namespace PokemonAutomation{ @@ -120,6 +126,9 @@ struct FakeResult{ std::vector actions = {}; FakeEnd end = FakeEnd::OUT_OF_TURNS; bool passes = false; + // Every item used, and whether a RESET was a Roar's or Whirlwind's. + SlugCounts used = {}; + bool roared = false; }; // The fight `trainer` from our side (`hp`, X items already used) against @@ -165,6 +174,8 @@ class FakeBattle{ .early_reset = m_early_reset, .used = m_used, }); + result.used = m_used; + result.roared = m_roared; return result; } @@ -509,6 +520,359 @@ const FakeCase FAKE_CASES[] = { }, }; + + +// The League loop on a fake runner. + +constexpr int OUR_MAX_HP = 171; +const SlugCounts FULL_PP = {{"bite", 25}, {"surf", 15}, {"blizzard", 5}, {"strength", 15}}; + +std::string trainer_of(std::string_view fight){ + if (fight == "champion"){ + return "TRAINER_CHAMPION_FIRST_BULBASAUR"; + } + std::string trainer = "TRAINER_ELITE_FOUR_"; + for (char c : fight){ + trainer += static_cast(std::toupper(static_cast(c))); + } + return trainer; +} + +// A battle that ends before its first turn: a loss. +FakeInput no_turns(std::string_view fight){ + return FakeInput{.trainer = trainer_of(fight), .foes = {{.slot = 0}}, .turns = 0}; +} +// The battle of FAKE_CASES's case `name`. +FakeInput fake_battle(std::string_view name){ + for (const FakeCase& test_case : FAKE_CASES){ + if (test_case.name == name){ + return test_case.input; + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("No fake battle \"{}\".", name)); +} +// A pass. +FakeInput lorelei_win(){ + return fake_battle("Lorelei's Lapras: Strength, Bite, Bite"); +} +// Won on two Full Restores, over Lorelei's cap of one. +FakeInput lorelei_over_cap(){ + return fake_battle("Lorelei won on two Full Restores: over the cap of one, no pass"); +} +// Bruno's Onix after all three X Speeds: the early reset. +FakeInput bruno_early_reset(){ + return fake_battle("Bruno's Onix: two Rock Tombs after all three X Speeds, reset when it falls"); +} +FakeInput bruno_roar(){ + return fake_battle("Bruno's Onix Roars: reset"); +} + +std::vector repeated(size_t count, const FakeInput& 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; +} + +// 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 FakeInput& input = m_battles[std::min(m_fights, m_battles.size() - 1)]; + m_fights++; + FakeBattle battle(input.trainer, input.hp, input.foes); + battle.used_before(input.used_before); + const FakeResult result = battle.run(input.turns); + for (const auto& [item, count] : result.used){ + m_now.bag[item] -= count; + } + switch (result.end){ + case FakeEnd::WON: + return LeagueBattleEnd::WON; + case FakeEnd::RESET: + return result.roared ? LeagueBattleEnd::ROARED : LeagueBattleEnd::EARLY_RESET; + case FakeEnd::LOST: + case FakeEnd::OUT_OF_TURNS: + break; + } + return LeagueBattleEnd::LOST; + } + + 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 LoopInput{ + std::string fight = {}; + LeagueSide save = {}; + std::vector battles = {}; +}; +struct LoopExpectation{ + LeagueFightVerdict verdict = LeagueFightVerdict::SAVED; + int phase1_passes = 0; + int fights = 0; + int saves = 0; + // Each 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; +}; +struct LoopCase{ + std::string name; + LoopInput input; + LoopExpectation 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 LoopExpectation& 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() : actions_text(report.tries.front().top_up), actions_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 = {lorelei_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, lorelei_win()), {no_turns("lorelei")}), + }, + .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, lorelei_win()), repeated(7, no_turns("lorelei"))), {lorelei_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, lorelei_win()), {no_turns("lorelei")}), + }, + .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, lorelei_win()), {lorelei_over_cap()}), + }, + .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 = {bruno_roar(), bruno_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 = {lorelei_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 = {no_turns("bruno")}, + }, + .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 = {no_turns("bruno")}, + }, + .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 = {no_turns("bruno")}, + }, + .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 = {no_turns("champion")}, + }, + .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 = {no_turns("lance")}, + }, + .expectation = { + .verdict = LeagueFightVerdict::BELOW_MIN_PASS, .phase1_passes = 0, .fights = 10, .saves = 0, + .top_up = std::vector{}, + }, + }, +}; + } // namespace @@ -516,6 +880,9 @@ void add_tests_AutoStoryPlannedBattleFake(UnitTestDatabase& database){ for (const FakeCase& test_case : FAKE_CASES){ database.add(test_case); } + for (const LoopCase& test_case : LOOP_CASES){ + database.add(test_case); + } } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 17f1697fd4..12c6af8904 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2230,6 +2230,8 @@ 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_AutoStoryLeagueSteps.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueSteps.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows13_Tests.h From 244a9df0b5c67392342827a44989837833f883b5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 17 Nov 2026 01:29:39 -0800 Subject: [PATCH 2/4] FRLG League battle: soft reset on Roar, Whirlwind and the early reset A planned fight that resets on Roar or Whirlwind soft resets when the lead is dragged out, instead of switching it back; the planned policy soft resets on the plan's early reset (league_early_reset(), Bruno's after Onix #1) when the foe it names falls. The battle ends ROARED or EARLY_RESET and its step fails with league_roar_reset or league_early_reset: the try didn't pass and the game is at its save. --- .../AutoStory/PokemonFRLG_AutoStoryBattle.cpp | 16 +++++++++++++++- .../AutoStory/PokemonFRLG_AutoStoryBattle.h | 4 ++++ .../PokemonFRLG_AutoStoryBattleDecisions.cpp | 11 +++++++++++ .../AutoStory/PokemonFRLG_AutoStoryBattleTurns.h | 5 +++++ .../AutoStory/PokemonFRLG_AutoStoryEvents.cpp | 4 ++++ .../PokemonFRLG_AutoStoryPlannedBattle.cpp | 15 +++++++++++++++ .../PokemonFRLG_AutoStoryPlannedBattle.h | 8 ++++++++ 7 files changed, 62 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp index 83bcb617cb..ab5fd62a67 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/ImageTools/ImageBoxes.h" @@ -269,9 +270,13 @@ 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_reset){ over = step(); } + if (m_reset){ + m_console.log(std::format("Battle: the try ended by a soft reset after {} turns.", m_decisions.turns()), COLOR_ORANGE); + return *m_reset; + } m_console.log(std::format( "Battle: over after {} turns{}.", m_decisions.turns(), m_decisions.lost() ? ", the starter fainted" : "" )); @@ -610,6 +615,13 @@ 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::RESET_ON_ROAR: + case TurnChoice::EARLY_RESET: + m_console.log("Battle: soft reset: the plan ends this try.", COLOR_ORANGE); + dump_debug_image(m_console, "AutoStory/battle", std::format("turn{}-reset", m_decisions.turns()), screen); + soft_reset(m_console, m_context); + m_reset = action.choice == TurnChoice::RESET_ON_ROAR ? BattleEnd::ROARED : BattleEnd::EARLY_RESET; + return; case TurnChoice::MOVE: m_console.log(std::format("Battle: move {}.", action.slot + 1)); m_refusal.move_chosen(current_time()); @@ -697,6 +709,8 @@ class BattleLoop{ bool m_move_chosen = false; // RUN was chosen and no battle menu has shown since. bool m_ran = false; + // The try was ended by a soft reset. + std::optional m_reset; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h index a6bdb6d528..ec7488dc18 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h @@ -61,6 +61,10 @@ 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 by a soft reset (TurnChoice::RESET_ON_ROAR, + // EARLY_RESET): the game is back at its save. + 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..3f34a8b42c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp @@ -302,6 +302,12 @@ 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::RESET_ON_ROAR, + .heal_reason = std::format("our {} is out, not sent: a Roar or Whirlwind landed; the plan resets the try", m_supplies.own_out), + }; + } if (m_planned){ m_planned->lead_switched_in(); } @@ -368,6 +374,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::EARLY_RESET, .heal_reason = reason}; + } if (turn.choice.item){ m_planned_item = *turn.choice.item; return TurnAction{ @@ -560,6 +569,8 @@ void BattleDecisions::turn_done(TurnChoice choice, TurnResult result){ case TurnChoice::RUN: case TurnChoice::MOVE: case TurnChoice::SWITCH_LEAD: + case TurnChoice::RESET_ON_ROAR: + case TurnChoice::EARLY_RESET: 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..4194616c06 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h @@ -40,6 +40,11 @@ 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: soft reset back to the save. A Roar or + // Whirlwind landed in a fight that resets on one, or the plan's early + // reset (Bruno's after Onix #1). + RESET_ON_ROAR, + EARLY_RESET, }; // Before a battle of `kind`'s first turn: whether it reads the Items pocket diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index 09d1c3417c..248bc83c95 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -280,6 +280,10 @@ 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); + // A League try the plan ended: the game was soft reset to its save. + if (end == BattleEnd::ROARED || end == BattleEnd::EARLY_RESET){ + return Pokemon::outcome_failed(end == BattleEnd::ROARED ? "league_roar_reset" : "league_early_reset", step.name); + } end_trainer_battle(env.supplies); if (end == BattleEnd::LOST && !arg(step, "may_lose")){ return Pokemon::outcome_failed("blackout"); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.cpp index 7f49d2ddd2..351f7d3214 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattle.cpp @@ -358,6 +358,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); @@ -373,6 +383,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 From 3dde053d16292a116f7062ddcf14e48c986774ad Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 17 Nov 2026 01:33:29 -0800 Subject: [PATCH 3/4] FRLG League battle: end the try mid-battle; the loop reloads The battle no longer soft resets itself: a Roar or Whirlwind in a fight that resets on one, or the plan's early reset, ends the battle at once (TurnChoice::END_TRY) as ROARED or EARLY_RESET, and the League loop's next try reloads the fight's save. Keeps PokemonFRLG_AutoStoryBattle.cpp and BattleDecisions within code health's limits; the League loop header gets its missing comments. --- .../AutoStory/PokemonFRLG_AutoStoryBattle.cpp | 24 +++++-------------- .../AutoStory/PokemonFRLG_AutoStoryBattle.h | 5 ++-- .../PokemonFRLG_AutoStoryBattleDecisions.cpp | 8 +++---- .../PokemonFRLG_AutoStoryBattleTurns.h | 10 ++++---- .../AutoStory/PokemonFRLG_AutoStoryEvents.cpp | 8 ++----- .../PokemonFRLG_AutoStoryLeagueLoop.h | 6 ++++- 6 files changed, 25 insertions(+), 36 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp index ab5fd62a67..a0e5192443 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/ImageTools/ImageBoxes.h" @@ -270,17 +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 && !m_reset){ + while (!over && !m_try_end){ over = step(); } - if (m_reset){ - m_console.log(std::format("Battle: the try ended by a soft reset after {} turns.", m_decisions.turns()), COLOR_ORANGE); - return *m_reset; - } - 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: @@ -615,12 +608,8 @@ 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::RESET_ON_ROAR: - case TurnChoice::EARLY_RESET: - m_console.log("Battle: soft reset: the plan ends this try.", COLOR_ORANGE); - dump_debug_image(m_console, "AutoStory/battle", std::format("turn{}-reset", m_decisions.turns()), screen); - soft_reset(m_console, m_context); - m_reset = action.choice == TurnChoice::RESET_ON_ROAR ? BattleEnd::ROARED : BattleEnd::EARLY_RESET; + case TurnChoice::END_TRY: + m_try_end = action.try_end; return; case TurnChoice::MOVE: m_console.log(std::format("Battle: move {}.", action.slot + 1)); @@ -709,8 +698,7 @@ class BattleLoop{ bool m_move_chosen = false; // RUN was chosen and no battle menu has shown since. bool m_ran = false; - // The try was ended by a soft reset. - std::optional m_reset; + 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 ec7488dc18..da7546b2ae 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h @@ -61,8 +61,9 @@ 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 by a soft reset (TurnChoice::RESET_ON_ROAR, - // EARLY_RESET): the game is back at its save. + // 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 3f34a8b42c..9b58ae7e6b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleDecisions.cpp @@ -304,8 +304,9 @@ TurnAction BattleDecisions::switch_lead_back(){ } if (m_planned && m_planned->resets_on_roar()){ return TurnAction{ - .choice = TurnChoice::RESET_ON_ROAR, + .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){ @@ -375,7 +376,7 @@ TurnAction BattleDecisions::planned_turn(const PartyStatus& status){ } const std::string reason = std::format("planned: {}", turn.log); if (turn.early_reset){ - return TurnAction{.choice = TurnChoice::EARLY_RESET, .heal_reason = reason}; + return TurnAction{.choice = TurnChoice::END_TRY, .heal_reason = reason, .try_end = BattleEnd::EARLY_RESET}; } if (turn.choice.item){ m_planned_item = *turn.choice.item; @@ -569,8 +570,7 @@ void BattleDecisions::turn_done(TurnChoice choice, TurnResult result){ case TurnChoice::RUN: case TurnChoice::MOVE: case TurnChoice::SWITCH_LEAD: - case TurnChoice::RESET_ON_ROAR: - case TurnChoice::EARLY_RESET: + 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 4194616c06..e01e3af2be 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleTurns.h @@ -40,11 +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: soft reset back to the save. A Roar or - // Whirlwind landed in a fight that resets on one, or the plan's early - // reset (Bruno's after Onix #1). - RESET_ON_ROAR, - EARLY_RESET, + // 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 @@ -65,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 248bc83c95..4eddab3524 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -280,13 +280,9 @@ 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); - // A League try the plan ended: the game was soft reset to its save. - if (end == BattleEnd::ROARED || end == BattleEnd::EARLY_RESET){ - return Pokemon::outcome_failed(end == BattleEnd::ROARED ? "league_roar_reset" : "league_early_reset", step.name); - } 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.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.h index 9e93c3fb38..901691f6dc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.h @@ -51,6 +51,9 @@ enum class LeagueBattleEnd{ 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. @@ -63,7 +66,7 @@ struct LeagueFightRunner{ std::function fight; // Saves the game where it is. std::function save; - std::function log; + LeagueLog log; }; // How the loop left a fight. @@ -90,6 +93,7 @@ struct LeagueTryRecord{ bool passes = false; }; +// The tries of a fight and how the loop left it. struct LeagueFightReport{ LeagueFightVerdict verdict = LeagueFightVerdict::NO_FIGURES; std::vector tries = {}; From 1e55de43024cdc7b6caf6b85088ea0d9b3e56c50 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 17 Nov 2026 01:39:57 -0800 Subject: [PATCH 4/4] FRLG League loop: its tests in a file of its own The loop's fake-runner tests move out of #638's PlannedBattleFake_Tests.cpp into PokemonFRLG_AutoStoryLeagueLoop_Tests.cpp, which this PR owns. Each try's battle is now scripted as its end and the items it spent; the fake battle engine file is back to #638's version. --- .../PokemonFRLG_AutoStoryLeagueLoop.h | 2 +- .../PokemonFRLG_AutoStoryLeagueLoop_Tests.cpp | 366 +++++++++++++++++ .../PokemonFRLG_AutoStoryLeagueLoop_Tests.h | 28 ++ ...nFRLG_AutoStoryPlannedBattleFake_Tests.cpp | 367 ------------------ .../PokemonFRLG_AutoStoryStepTable_Tests.cpp | 2 + SerialPrograms/cmake/SourceFiles.cmake | 2 + 6 files changed, 399 insertions(+), 368 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop_Tests.h diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.h index 901691f6dc..1c410f95f0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLeagueLoop.h @@ -14,7 +14,7 @@ * 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_AutoStoryPlannedBattleFake_Tests.cpp). + * plays scripted battles (PokemonFRLG_AutoStoryLeagueLoop_Tests.cpp). * */ 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_AutoStoryPlannedBattleFake_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattleFake_Tests.cpp index ab54f1f366..5727309711 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattleFake_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlannedBattleFake_Tests.cpp @@ -12,10 +12,6 @@ * plan says: a Roar or Whirlwind in a fight that resets on one, Bruno's * early reset after Onix #1, a win or a loss. * - * The League loop (PokemonFRLG_AutoStoryLeagueLoop.h) runs on a fake - * runner whose fights are these scripted battles: its phases, its saves, - * the entry contract and the top-ups between fights. - * */ #include @@ -30,9 +26,7 @@ #include "Common/Cpp/TestRunners/UnitTest.h" #include "PokemonFRLG_AutoStoryLeagueFightData.h" #include "PokemonFRLG_AutoStoryLeagueFoes.h" -#include "PokemonFRLG_AutoStoryLeagueLoop.h" #include "PokemonFRLG_AutoStoryPlannedBattle.h" -#include "PokemonFRLG_AutoStoryPlannedBetween.h" #include "PokemonFRLG_AutoStorySupplies.h" namespace PokemonAutomation{ @@ -126,9 +120,6 @@ struct FakeResult{ std::vector actions = {}; FakeEnd end = FakeEnd::OUT_OF_TURNS; bool passes = false; - // Every item used, and whether a RESET was a Roar's or Whirlwind's. - SlugCounts used = {}; - bool roared = false; }; // The fight `trainer` from our side (`hp`, X items already used) against @@ -174,8 +165,6 @@ class FakeBattle{ .early_reset = m_early_reset, .used = m_used, }); - result.used = m_used; - result.roared = m_roared; return result; } @@ -520,359 +509,6 @@ const FakeCase FAKE_CASES[] = { }, }; - - -// The League loop on a fake runner. - -constexpr int OUR_MAX_HP = 171; -const SlugCounts FULL_PP = {{"bite", 25}, {"surf", 15}, {"blizzard", 5}, {"strength", 15}}; - -std::string trainer_of(std::string_view fight){ - if (fight == "champion"){ - return "TRAINER_CHAMPION_FIRST_BULBASAUR"; - } - std::string trainer = "TRAINER_ELITE_FOUR_"; - for (char c : fight){ - trainer += static_cast(std::toupper(static_cast(c))); - } - return trainer; -} - -// A battle that ends before its first turn: a loss. -FakeInput no_turns(std::string_view fight){ - return FakeInput{.trainer = trainer_of(fight), .foes = {{.slot = 0}}, .turns = 0}; -} -// The battle of FAKE_CASES's case `name`. -FakeInput fake_battle(std::string_view name){ - for (const FakeCase& test_case : FAKE_CASES){ - if (test_case.name == name){ - return test_case.input; - } - } - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("No fake battle \"{}\".", name)); -} -// A pass. -FakeInput lorelei_win(){ - return fake_battle("Lorelei's Lapras: Strength, Bite, Bite"); -} -// Won on two Full Restores, over Lorelei's cap of one. -FakeInput lorelei_over_cap(){ - return fake_battle("Lorelei won on two Full Restores: over the cap of one, no pass"); -} -// Bruno's Onix after all three X Speeds: the early reset. -FakeInput bruno_early_reset(){ - return fake_battle("Bruno's Onix: two Rock Tombs after all three X Speeds, reset when it falls"); -} -FakeInput bruno_roar(){ - return fake_battle("Bruno's Onix Roars: reset"); -} - -std::vector repeated(size_t count, const FakeInput& 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; -} - -// 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 FakeInput& input = m_battles[std::min(m_fights, m_battles.size() - 1)]; - m_fights++; - FakeBattle battle(input.trainer, input.hp, input.foes); - battle.used_before(input.used_before); - const FakeResult result = battle.run(input.turns); - for (const auto& [item, count] : result.used){ - m_now.bag[item] -= count; - } - switch (result.end){ - case FakeEnd::WON: - return LeagueBattleEnd::WON; - case FakeEnd::RESET: - return result.roared ? LeagueBattleEnd::ROARED : LeagueBattleEnd::EARLY_RESET; - case FakeEnd::LOST: - case FakeEnd::OUT_OF_TURNS: - break; - } - return LeagueBattleEnd::LOST; - } - - 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 LoopInput{ - std::string fight = {}; - LeagueSide save = {}; - std::vector battles = {}; -}; -struct LoopExpectation{ - LeagueFightVerdict verdict = LeagueFightVerdict::SAVED; - int phase1_passes = 0; - int fights = 0; - int saves = 0; - // Each 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; -}; -struct LoopCase{ - std::string name; - LoopInput input; - LoopExpectation 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 LoopExpectation& 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() : actions_text(report.tries.front().top_up), actions_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 = {lorelei_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, lorelei_win()), {no_turns("lorelei")}), - }, - .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, lorelei_win()), repeated(7, no_turns("lorelei"))), {lorelei_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, lorelei_win()), {no_turns("lorelei")}), - }, - .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, lorelei_win()), {lorelei_over_cap()}), - }, - .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 = {bruno_roar(), bruno_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 = {lorelei_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 = {no_turns("bruno")}, - }, - .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 = {no_turns("bruno")}, - }, - .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 = {no_turns("bruno")}, - }, - .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 = {no_turns("champion")}, - }, - .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 = {no_turns("lance")}, - }, - .expectation = { - .verdict = LeagueFightVerdict::BELOW_MIN_PASS, .phase1_passes = 0, .fights = 10, .saves = 0, - .top_up = std::vector{}, - }, - }, -}; - } // namespace @@ -880,9 +516,6 @@ void add_tests_AutoStoryPlannedBattleFake(UnitTestDatabase& database){ for (const FakeCase& test_case : FAKE_CASES){ database.add(test_case); } - for (const LoopCase& test_case : LOOP_CASES){ - database.add(test_case); - } } 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 12c6af8904..481917057c 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2232,6 +2232,8 @@ file(GLOB LIBRARY_SOURCES 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