Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,39 +208,81 @@
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(){

Check warning on line 252 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::ConsoleWildBattleEscape::run(): function_lines=35 (advisory above 25, limit 50)
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<ProControllerContext>(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<ProControllerContext>(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.
Expand Down Expand Up @@ -387,16 +429,20 @@
stream.log("[Catch cleanup] Verified receipt, completed battle exit, and four seconds of clear overworld.", COLOR_GREEN);
}

void escape_wild_battle(VideoStream& stream,

Check warning on line 432 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::escape_wild_battle( VideoStream & stream , ProControllerContext & context , BattleType battle_type , const WildBattleEscapePolicy & policy): function_lines=30 (advisory above 25, limit 50)
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
#ifndef PokemonAutomation_PokemonBDSP_AutoStory_WildBattle_H
#define PokemonAutomation_PokemonBDSP_AutoStory_WildBattle_H

#include <chrono>
#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{
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* BDSP AutoStory - Grid simulation

Check warning on line 1 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=537 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand All @@ -7,6 +7,7 @@
#include <algorithm>
#include <format>
#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{
Expand Down Expand Up @@ -380,14 +381,67 @@
return WalkStop::LIMIT;
}

// The escape policy's game steps on a scripted battle.
class SimWildBattleEscape : public WildBattleEscapeActions{
public:
SimWildBattleEscape(const SimWildBattle& battle, std::vector<std::string>& 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<std::string>& m_notes;
};

} // namespace

std::optional<SimWallEncounter> 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<SimWildTap> GridSimRouteIO::take_encounter(Direction direction, size_t tiles, size_t tap, const std::string& label){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& notes, std::vector<SimWildTap> encounters = {})
GridSimRouteIO(
GridSim& sim, std::vector<std::string>& notes,
std::vector<SimWildTap> encounters = {}, std::vector<SimWallEncounter> 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;
Expand All @@ -215,10 +234,15 @@ class GridSimRouteIO : public RouteIO{

private:
std::optional<SimWildTap> take_encounter(Pokemon::Direction direction, size_t tiles, size_t tap, const std::string& label);
std::optional<SimWallEncounter> 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<std::string>& m_notes;
std::vector<SimWildTap> m_encounters;
std::vector<SimWallEncounter> m_wall_encounters;
};

// What GridSimRouteIO throws for RouteNote::FAILURE.
Expand Down
Loading
Loading