diff --git a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp index 72c29a2fa1..33ef79c627 100644 --- a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp @@ -18,6 +18,8 @@ #include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.h" +#include "Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.h" +#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.h" #include "PokemonBDSP_Tests.h" namespace PokemonAutomation{ @@ -36,8 +38,10 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStory_StorySimulation(database); add_tests_AutoStory_StoryTable(database); add_tests_AutoStory_Walk(database); + add_tests_AutoStory_WildBattlePolicy(database); add_tests_AutoStory_PlannedRoute(database); add_tests_AutoStory_GridSim(database); + add_tests_AutoStory_WallBattleSim(database); add_tests_AutoStory_RecoveryWalk(database); add_tests_AutoStory_StorySim(database); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp index 9f8dbf52c2..5994605164 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp @@ -208,39 +208,81 @@ bool mine_select_run(VideoStream& stream, ProControllerContext& context, BattleT return false; } -// Run on the battle menu, until the battle's exit shows or `timeout` -// runs out. Whether the exit was seen. -bool run_until_escaped( - VideoStream& stream, ProControllerContext& context, BattleType battle_type, - std::chrono::seconds timeout, EndBattleWatcher& battle_end -){ - const auto deadline = std::chrono::steady_clock::now() + timeout; - while (std::chrono::steady_clock::now() < deadline){ - pbf_press_button(context, BUTTON_B, 40ms, 200ms); - context.wait_for_all_requests(); - BattleMenuWatcher menu_ready(battle_type, COLOR_YELLOW); - const int ready = wait_until(stream, context, 3s, {{battle_end}, {menu_ready}}); - if (ready == 0){ - return true; - } - if (ready != 1 || !mine_select_run(stream, context, battle_type)){ - continue; +// The escape policy's game steps on the console. The exit watcher lives +// across the attempts: EndBattleWatcher needs black followed by a visible +// screen, which one attempt's waits may split. +class ConsoleWildBattleEscape : public WildBattleEscapeActions{ +public: + ConsoleWildBattleEscape(VideoStream& stream, ProControllerContext& context, BattleType battle_type) + : m_stream(stream) + , m_context(context) + , m_battle_type(battle_type) + {} + + virtual WildRunOutcome run() override; + virtual void fight() override{ + fight_wild_battle(m_stream, m_context, "Mine escape"); + } + virtual void note(const std::string& text) override{ + m_stream.log("[Mine escape] " + text); + } + + // After the policy gave up: whether the exit still shows within `wait`. + bool exit_within(Milliseconds wait){ + return wait_until(m_stream, m_context, wait, {{m_battle_end}}) == 0; + } + +private: + // B, then the exit (0) or the battle menu (1) within three seconds; -1 for neither. + int exit_or_menu(); + + VideoStream& m_stream; + ProControllerContext& m_context; + BattleType m_battle_type; + EndBattleWatcher m_battle_end; +}; + +int ConsoleWildBattleEscape::exit_or_menu(){ + pbf_press_button(m_context, BUTTON_B, 40ms, 200ms); + m_context.wait_for_all_requests(); + BattleMenuWatcher menu_ready(m_battle_type, COLOR_YELLOW); + return wait_until(m_stream, m_context, 3s, {{m_battle_end}, {menu_ready}}); +} + +WildRunOutcome ConsoleWildBattleEscape::run(){ + const int ready = exit_or_menu(); + if (ready == 0){ + return WildRunOutcome::ESCAPED; + } + if (ready != 1 || !mine_select_run(m_stream, m_context, m_battle_type)){ + return WildRunOutcome::UNCERTAIN; + } + m_stream.log("[Mine escape] Run input sent; watching for black-screen exit."); + const int result = run_until(m_stream, + m_context, + [](ProControllerContext& controller){ + pbf_mash_button(controller, BUTTON_ZL, 1000ms); + pbf_mash_button(controller, BUTTON_A, 1000ms); + pbf_mash_button(controller, BUTTON_B, 3000ms); + controller.wait_for_all_requests(); + }, + {m_battle_end}); + if (result == 0){ + return WildRunOutcome::ESCAPED; + } + // No exit after Run: the menu back means the game refused it ("Can't + // escape!": Onix's Bind in Oreburgh Mine, console B 2026-11-07). Its + // text and the foe's turn take a few seconds. + for (size_t look = 0; look < 2; look++){ + const int seen = exit_or_menu(); + if (seen == 0){ + return WildRunOutcome::ESCAPED; } - stream.log("[Mine escape] Run input sent; watching for black-screen exit."); - const int result = run_until(stream, - context, - [](ProControllerContext& controller){ - pbf_mash_button(controller, BUTTON_ZL, 1000ms); - pbf_mash_button(controller, BUTTON_A, 1000ms); - pbf_mash_button(controller, BUTTON_B, 3000ms); - controller.wait_for_all_requests(); - }, - {battle_end}); - if (result == 0){ - return true; + if (seen == 1){ + return WildRunOutcome::TRAPPED; } } - return false; + return WildRunOutcome::UNCERTAIN; } // B until the overworld shows; fails with `failure` after two minutes. @@ -390,13 +432,17 @@ void catch_1_pokemon( void escape_wild_battle(VideoStream& stream, ProControllerContext& context, BattleType battle_type, - std::chrono::seconds timeout){ - EndBattleWatcher battle_end; - bool exit_transition = run_until_escaped(stream, context, battle_type, timeout, battle_end); - if (!exit_transition && wait_until(stream, context, 10s, {{battle_end}}) == 0){ - exit_transition = true; + const WildBattleEscapePolicy& policy){ + ConsoleWildBattleEscape actions(stream, context, battle_type); + WildEscapeResult result = escape_wild_battle(actions, policy); + if (result == WildEscapeResult::FOUGHT){ + // fight_wild_battle() ended on the overworld. + return; + } + if (result == WildEscapeResult::UNCONFIRMED && actions.exit_within(10s)){ + result = WildEscapeResult::ESCAPED; } - if (exit_transition){ + if (result == WildEscapeResult::ESCAPED){ // EndBattleWatcher requires black followed by a visible screen. stream.log("[Mine escape] Black-screen exit transition confirmed.", COLOR_GREEN); pbf_wait(context, 800ms); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.h index 7ca96f2bb7..98a93ffb9b 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.h @@ -13,10 +13,10 @@ #ifndef PokemonAutomation_PokemonBDSP_AutoStory_WildBattle_H #define PokemonAutomation_PokemonBDSP_AutoStory_WildBattle_H -#include #include "CommonFramework/Tools/VideoStream.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -34,13 +34,16 @@ void battle_1_pokemon(VideoStream& stream, ProControllerContext& context); // in the Bag, up to ten throws; ends on the overworld. void catch_1_pokemon(VideoStream& stream, ProControllerContext& context); -// Runs from the wild battle on screen: Run on the battle menu until the -// exit's black screen shows. Fails with a screenshot after `timeout`. +// Leaves the wild battle on screen as `policy` says: Run on the battle menu +// until the exit's black screen shows, or, once the game has refused the +// escape `policy.run_refusals` times (a trapping move), fights it to its end +// with the best move. Fails with a screenshot once the policy gives up and +// the main menu does not prove the overworld either. void escape_wild_battle( VideoStream& stream, ProControllerContext& context, BattleType battle_type = BattleType::STANDARD, - std::chrono::seconds timeout = std::chrono::seconds(60) + const WildBattleEscapePolicy& policy = WildBattleEscapePolicy() ); // escape_wild_battle() for a standard wild battle, as the walker takes it diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp index 909766eb48..38630ad729 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp @@ -7,6 +7,7 @@ #include #include #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy.h" #include "PokemonBDSP_AutoStory_GridSim.h" namespace PokemonAutomation{ @@ -380,14 +381,67 @@ WalkStop walk_stop(SimStop stop){ return WalkStop::LIMIT; } +// The escape policy's game steps on a scripted battle. +class SimWildBattleEscape : public WildBattleEscapeActions{ +public: + SimWildBattleEscape(const SimWildBattle& battle, std::vector& notes) + : m_refusals_left(battle.run_refusals) + , m_notes(notes) + {} + virtual WildRunOutcome run() override{ + if (m_refusals_left == 0){ + return WildRunOutcome::ESCAPED; + } + m_refusals_left--; + return WildRunOutcome::TRAPPED; + } + virtual void fight() override{ + m_notes.push_back("[Sim escape] Fought with the best move until the battle ended."); + } + virtual void note(const std::string& text) override{ + m_notes.push_back("[Sim escape] " + text); + } + +private: + size_t m_refusals_left; + std::vector& m_notes; +}; + } // namespace +std::optional GridSimRouteIO::take_wall_encounter(const RouteWallWalk& walk){ + const auto found = std::find_if(m_wall_encounters.begin(), m_wall_encounters.end(), [&](const SimWallEncounter& encounter){ + return encounter.direction == walk.direction && encounter.label == walk.label; + }); + if (found == m_wall_encounters.end()){ + return std::nullopt; + } + SimWallEncounter encounter = *found; + m_wall_encounters.erase(found); + return encounter; +} + +void GridSimRouteIO::escape_scripted_battle(const SimWildBattle& battle, const std::string& label){ + SimWildBattleEscape actions(battle, m_notes); + const WildEscapeResult result = escape_wild_battle(actions, WildBattleEscapePolicy()); + if (result == WildEscapeResult::UNCONFIRMED){ + note(std::format("escape_wild_battle: {}: the Run attempts ran out without the exit.", label), RouteNote::FAILURE); + } + const char* ended = result == WildEscapeResult::FOUGHT ? "fought to its end" : "run from"; + m_notes.push_back(std::format("[Sim] {}: wild battle {}.", label, ended)); +} + WalkResult GridSimRouteIO::walk_to_wall(const RouteWallWalk& walk){ + size_t wild_battles = 0; + if (const auto encounter = take_wall_encounter(walk)){ + escape_scripted_battle(encounter->battle, walk.label); + wild_battles = 1; + } const SimPace pace = walk.options.pace == DpadPace::RUN ? SimPace::RUN : SimPace::WALK; const SimMove move = m_sim.hold(walk.direction, walk.limit, pace); m_notes.push_back(std::format("[Sim] {}: held {} to {} at {}.", walk.label, direction_name(walk.direction), sim_stop_name(move.stop), to_string(m_sim.player().pos))); - return {.stop = walk_stop(move.stop), .walked = move.time, .wild_battles = 0, .moved_chunks = (size_t)(move.distance / SIM_TILE)}; + return {.stop = walk_stop(move.stop), .walked = move.time, .wild_battles = wild_battles, .moved_chunks = (size_t)(move.distance / SIM_TILE)}; } std::optional GridSimRouteIO::take_encounter(Direction direction, size_t tiles, size_t tap, const std::string& label){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.h index 0535e3f043..68db3ed247 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.h @@ -195,14 +195,33 @@ struct SimWildTap{ WalkOrigin origin = WalkOrigin::CURRENT_LEG; }; +// A scripted wild battle, as the escape policy's game steps see it: how +// many Run presses the game refuses ("Can't escape!": Bind, Wrap) before +// one works. Not a model of the battle itself. +struct SimWildBattle{ + size_t run_refusals = 0; +}; +// A wild battle that interrupts the walk to a wall (walk_to_wall()) with +// this label toward this direction, once; the hold goes on to the wall +// after it, as the console's does. +struct SimWallEncounter{ + std::string label; + Pokemon::Direction direction = Pokemon::Direction::NORTH; + SimWildBattle battle; +}; + // The walker's RouteIO on a GridSim. A failure throws GridSimFailure. class GridSimRouteIO : public RouteIO{ public: // Every line noted goes to `notes`, which must outlive this. - GridSimRouteIO(GridSim& sim, std::vector& notes, std::vector encounters = {}) + GridSimRouteIO( + GridSim& sim, std::vector& notes, + std::vector encounters = {}, std::vector wall_encounters = {} + ) : m_sim(sim) , m_notes(notes) , m_encounters(std::move(encounters)) + , m_wall_encounters(std::move(wall_encounters)) {} virtual WalkResult walk_to_wall(const RouteWallWalk& walk) override; @@ -215,10 +234,15 @@ class GridSimRouteIO : public RouteIO{ private: std::optional take_encounter(Pokemon::Direction direction, size_t tiles, size_t tap, const std::string& label); + std::optional take_wall_encounter(const RouteWallWalk& walk); + // The escape policy on `battle`; notes a FAILURE where the console + // would fail the program. + void escape_scripted_battle(const SimWildBattle& battle, const std::string& label); GridSim& m_sim; std::vector& m_notes; std::vector m_encounters; + std::vector m_wall_encounters; }; // What GridSimRouteIO throws for RouteNote::FAILURE. diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp new file mode 100644 index 0000000000..d4bd12f569 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp @@ -0,0 +1,146 @@ +/* BDSP AutoStory Grid Simulation - Wild Battles on Wall Legs Tests + * + * From: https://github.com/PokemonAutomation/ + * + * A wild battle on a leg to a wall, walked by the route walker on the grid + * simulation: however the escape ends the battle, the leg must still end at + * the wall with the battle counted. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_GridSim.h" +#include "PokemonBDSP_AutoStory_WallBattleSim_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +using Pokemon::Direction; +using Pokemon::TilePos; + + +struct WallBattleInput{ + std::string name; + SimWildBattle battle; +}; +struct WallBattleExpectation{ + // How the simulation notes the battle ended. + std::string ended; +}; +struct WallBattleCase{ + WallBattleInput input; + WallBattleExpectation expectation; +}; + +StoryWorld make_world(const std::vector& rows){ + Pokemon::MapData map; + map.name = "Test"; + map.tiles = Pokemon::Grid{(int)rows[0].size(), (int)rows.size(), {}}; + for (const std::string& row : rows){ + for (char glyph : row){ + map.tiles.cells.push_back(*Pokemon::tile_from_glyph(glyph)); + } + } + StoryWorld world; + world.add("Test", map, {0, 0}); + return world; +} + +// The corridor's one leg, east to its end wall, walked with `battle` on +// the way; the walk's notes, or why it did not end at the wall. +struct WallBattleWalk{ + std::vector notes; + std::string error; +}; +WallBattleWalk walk_wall_battle(const std::string& leg, const SimWildBattle& battle){ + StoryWorld world = make_world({ + "########################", + "#......................#", + "########################", + }); + const TilePos start{1, 1}; + const TilePos target{22, 1}; + GridSim sim({.world = &world}, start, Direction::EAST, true); + WallBattleWalk walk; + GridSimRouteIO sim_io(sim, walk.notes, {}, {{.label = leg, .direction = Direction::EAST, .battle = battle}}); + RouteTrainers trainers{{}, [](const std::string&){}}; + try{ + walk_route(sim_io, world, {start}, trainers, { + .goal = [&](const TilePos& pos){ return pos == target; }, + .label = "wall leg battle", + .avoid = {}, + .costs = *story_route_costs("town"), + .speed = MoveSpeed::RUN, + }); + }catch (const GridSimFailure& f){ + walk.error = "failed: " + f.message; + return walk; + } + if (nearest_tile(sim.player().pos) != target){ + walk.error = "ended at " + to_string(sim.player().pos) + ", not at the wall."; + } + return walk; +} + +class Test_WallBattleSim : public UnitTest{ +public: + explicit Test_WallBattleSim(WallBattleCase test_case) + : UnitTest("PokemonBDSP::AutoStory - Grid sim: a wild battle on a wall leg: " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::string leg = "wall leg battle: " + to_string(TileLeg{.direction = Direction::EAST, .tiles = 21, .ends_at_wall = true}); + const WallBattleWalk walk = walk_wall_battle(leg, m_case.input.battle); + if (!walk.error.empty()){ + return walk.error; + } + const std::string ended = std::format("[Sim] {}: wild battle {}.", leg, m_case.expectation.ended); + if (std::find(walk.notes.begin(), walk.notes.end(), ended) == walk.notes.end()){ + std::string notes; + for (const std::string& line : walk.notes){ + notes += "\n " + line; + } + return std::format("expected the note \"{}\"; the walk noted:{}", ended, notes); + } + return true; + } + +private: + WallBattleCase m_case; +}; + +const WallBattleCase WALL_BATTLE_SIM_CASES[] = { + {.input = {.name = "the first Run works", .battle = {.run_refusals = 0}}, .expectation = {.ended = "run from"}}, + {.input = {.name = "two refusals, then the third Run works", .battle = {.run_refusals = 2}}, .expectation = {.ended = "run from"}}, + // console B, BDSP 01.03 run 2026-11-07: Onix's Bind in Oreburgh Mine + // refused nine Runs in 60 s and escape_wild_battle() gave up. + {.input = {.name = "bound: every Run refused", .battle = {.run_refusals = 20}}, .expectation = {.ended = "fought to its end"}}, +}; + +} // namespace + + +void add_tests_AutoStory_WallBattleSim(UnitTestDatabase& database){ + for (const WallBattleCase& item : WALL_BATTLE_SIM_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.h new file mode 100644 index 0000000000..0833c3040a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.h @@ -0,0 +1,27 @@ +/* BDSP AutoStory Grid Simulation - Wild Battles on Wall Legs Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_WallBattleSim_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_WallBattleSim_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + + +// A wild battle on a leg to a wall, on the grid simulation: run from, or +// fought when the game refuses the escape, the leg still ends at the wall. +void add_tests_AutoStory_WallBattleSim(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy.cpp new file mode 100644 index 0000000000..26a20e6fd4 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy.cpp @@ -0,0 +1,42 @@ +/* BDSP AutoStory - Wild battle escape policy + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonBDSP_AutoStory_WildBattlePolicy.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +WildEscapeResult escape_wild_battle(WildBattleEscapeActions& actions, const WildBattleEscapePolicy& policy){ + size_t refusals = 0; + for (size_t attempt = 1; attempt <= policy.attempts; attempt++){ + actions.note(std::format("Run attempt {}/{}.", attempt, policy.attempts)); + switch (actions.run()){ + case WildRunOutcome::ESCAPED: + return WildEscapeResult::ESCAPED; + case WildRunOutcome::TRAPPED: + refusals++; + actions.note(std::format("The game refused the escape ({} so far).", refusals)); + if (refusals >= policy.run_refusals && policy.fallback == TrappedFallback::FIGHT){ + actions.note("Trapped: fighting the battle to its end instead."); + actions.fight(); + return WildEscapeResult::FOUGHT; + } + break; + case WildRunOutcome::UNCERTAIN: + break; + } + } + return WildEscapeResult::UNCONFIRMED; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy.h new file mode 100644 index 0000000000..9c0fb3ff12 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy.h @@ -0,0 +1,81 @@ +/* BDSP AutoStory - Wild battle escape policy + * + * From: https://github.com/PokemonAutomation/ + * + * How a wild battle that interrupts a walk is left, as decisions over the + * game steps the console or the simulation supplies, so the same policy + * runs on the Switch and in GridSim tests. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_WildBattlePolicy_H +#define PokemonAutomation_PokemonBDSP_AutoStory_WildBattlePolicy_H + +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// What one Run on the battle menu came to. +enum class WildRunOutcome{ + ESCAPED, // the battle's exit transition showed + TRAPPED, // the battle menu came back: the game refused the escape + UNCERTAIN, // neither showed within the wait: text still scrolling, cursor unread +}; + +// The game steps escape_wild_battle() takes, injected so its decisions run +// in tests without a Switch: the console presses the buttons and watches +// the screen; the simulation scripts the outcomes. +class WildBattleEscapeActions{ +public: + virtual ~WildBattleEscapeActions() = default; + + // On the battle menu: Run, then watch for the exit or the menu again. + virtual WildRunOutcome run() = 0; + + // On the battle menu after a refused Run: fight with the best move until + // the battle ends, on the overworld when it returns. + virtual void fight() = 0; + + // A line of the escape's log. + virtual void note(const std::string& text) = 0; +}; + +// What to do once the game has refused the escape `run_refusals` times. +enum class TrappedFallback{ + GIVE_UP, // keep pressing Run until the attempts run out + FIGHT, // fight the battle to its end instead +}; + +// How a wild battle is left: data, so the simulation runs the console's +// policy, and a later shared policy can carry it. +struct WildBattleEscapePolicy{ + // run() calls in all before the escape is given up as unconfirmed. + size_t attempts = 12; + // Refused Runs (the menu back each time) before `fallback` applies: a + // trapping move such as Bind refuses every one of them. + size_t run_refusals = 3; + TrappedFallback fallback = TrappedFallback::FIGHT; +}; + +// How escape_wild_battle() ended. +enum class WildEscapeResult{ + ESCAPED, + FOUGHT, // the game kept refusing the escape; the battle was fought to its end + UNCONFIRMED, // the attempts ran out without the exit +}; + +// The decisions of escape_wild_battle(): Run on the battle menu until the +// exit shows, within `policy.attempts` tries; after `policy.run_refusals` +// refused Runs, `policy.fallback`. +WildEscapeResult escape_wild_battle(WildBattleEscapeActions& actions, const WildBattleEscapePolicy& policy); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.cpp new file mode 100644 index 0000000000..1dc864e70b --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.cpp @@ -0,0 +1,150 @@ +/* BDSP AutoStory - Wild battle escape policy tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "PokemonBDSP_AutoStory_WildBattlePolicy.h" +#include "PokemonBDSP_AutoStory_WildBattlePolicy_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +// Scripted game steps: each run() returns the next outcome, and the last +// one again once they run out. +class ScriptedEscapeActions : public WildBattleEscapeActions{ +public: + explicit ScriptedEscapeActions(std::vector outcomes) + : m_outcomes(std::move(outcomes)) + {} + virtual WildRunOutcome run() override{ + const WildRunOutcome outcome = m_outcomes[std::min(m_runs, m_outcomes.size() - 1)]; + m_runs++; + return outcome; + } + virtual void fight() override{ + m_fights++; + } + virtual void note(const std::string&) override{} + + size_t runs() const{ + return m_runs; + } + size_t fights() const{ + return m_fights; + } + +private: + std::vector m_outcomes; + size_t m_runs = 0; + size_t m_fights = 0; +}; + +const char* result_name(WildEscapeResult result){ + switch (result){ + case WildEscapeResult::ESCAPED: + return "ESCAPED"; + case WildEscapeResult::FOUGHT: + return "FOUGHT"; + case WildEscapeResult::UNCONFIRMED: + return "UNCONFIRMED"; + } + return "?"; +} + +struct EscapePolicyInput{ + std::string name; + std::vector outcomes; + WildBattleEscapePolicy policy = {}; +}; +struct EscapePolicyExpectation{ + WildEscapeResult result = WildEscapeResult::ESCAPED; + size_t runs = 0; + size_t fights = 0; +}; +struct EscapePolicyCase{ + EscapePolicyInput input; + EscapePolicyExpectation expectation; +}; + +class Test_WildBattlePolicy : public UnitTest{ +public: + explicit Test_WildBattlePolicy(EscapePolicyCase test_case) + : UnitTest("PokemonBDSP::AutoStory - Wild battle escape policy: " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + ScriptedEscapeActions actions(m_case.input.outcomes); + const WildEscapeResult result = escape_wild_battle(actions, m_case.input.policy); + const EscapePolicyExpectation& expected = m_case.expectation; + if (result != expected.result || actions.runs() != expected.runs || actions.fights() != expected.fights){ + return std::format("expected {} after {} Run presses and {} fights, got {} after {} and {}.", + result_name(expected.result), expected.runs, expected.fights, + result_name(result), actions.runs(), actions.fights()); + } + return true; + } + +private: + EscapePolicyCase m_case; +}; + +constexpr WildRunOutcome ESCAPED = WildRunOutcome::ESCAPED; +constexpr WildRunOutcome TRAPPED = WildRunOutcome::TRAPPED; +constexpr WildRunOutcome UNCERTAIN = WildRunOutcome::UNCERTAIN; + +const EscapePolicyCase WILD_BATTLE_POLICY_CASES[] = { + { + .input = {.name = "the first Run works", .outcomes = {ESCAPED}}, + .expectation = {.result = WildEscapeResult::ESCAPED, .runs = 1}, + }, + { + .input = {.name = "text still scrolling twice, then away", .outcomes = {UNCERTAIN, UNCERTAIN, ESCAPED}}, + .expectation = {.result = WildEscapeResult::ESCAPED, .runs = 3}, + }, + { + .input = {.name = "two refusals, then the third Run works", .outcomes = {TRAPPED, TRAPPED, ESCAPED}}, + .expectation = {.result = WildEscapeResult::ESCAPED, .runs = 3}, + }, + { + .input = {.name = "never a menu or an exit: the attempts run out", .outcomes = {UNCERTAIN}, .policy = {.attempts = 5}}, + .expectation = {.result = WildEscapeResult::UNCONFIRMED, .runs = 5}, + }, + // Onix's Bind in Oreburgh Mine, console B 2026-11-07: nine Runs refused. + { + .input = {.name = "bound: every Run refused, so the battle is fought", .outcomes = {TRAPPED}}, + .expectation = {.result = WildEscapeResult::FOUGHT, .runs = 3, .fights = 1}, + }, + { + .input = {.name = "refusals between scrolling text count up to the fight", .outcomes = {TRAPPED, UNCERTAIN, TRAPPED, UNCERTAIN, TRAPPED}}, + .expectation = {.result = WildEscapeResult::FOUGHT, .runs = 5, .fights = 1}, + }, + { + .input = {.name = "bound with the fight turned off: the attempts run out", .outcomes = {TRAPPED}, .policy = {.attempts = 6, .fallback = TrappedFallback::GIVE_UP}}, + .expectation = {.result = WildEscapeResult::UNCONFIRMED, .runs = 6}, + }, +}; + +} // namespace + + +void add_tests_AutoStory_WildBattlePolicy(UnitTestDatabase& database){ + for (const EscapePolicyCase& item : WILD_BATTLE_POLICY_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.h new file mode 100644 index 0000000000..3082f5343d --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.h @@ -0,0 +1,28 @@ +/* BDSP AutoStory - Wild battle escape policy tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_WildBattlePolicy_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_WildBattlePolicy_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + + +// The escape policy's decisions (PokemonBDSP_AutoStory_WildBattlePolicy.h) +// on scripted Run outcomes: when it escapes, when it fights a battle the +// game will not let it leave, and when it gives up. +void add_tests_AutoStory_WildBattlePolicy(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index ce5d08921a..9eab8f466a 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1579,6 +1579,8 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.h + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.h Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.h Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.cpp @@ -1593,6 +1595,10 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h + Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy.cpp + Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy.h + Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.h Source/PokemonBDSP/Inference/Battles/PokemonBDSP_BattleBallReader.cpp Source/PokemonBDSP/Inference/Battles/PokemonBDSP_BattleBallReader.h Source/PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.cpp