Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
71a7eb3
FRLG OverworldSim: the walker takes the caller's interrupts
dayfine Nov 3, 2026
bbaf19e
FRLG: one delay for a wild battle seen late, the measured 3.7 s
dayfine Nov 3, 2026
1ae97f7
FRLG walker: find the player at walls after an open leg that may have…
dayfine Nov 3, 2026
5e98351
Merge frlg/overworld-sim into frlg/short-leg-late-battle
dayfine Nov 3, 2026
41249f6
Merge frlg/overworld-sim into frlg/short-leg-late-battle
dayfine Nov 3, 2026
b8ae150
FRLG walker: a wild battle in place of a warp's fade stops the leg be…
dayfine Nov 3, 2026
57c29b4
Merge frlg/overworld-sim into frlg/short-leg-late-battle
dayfine Nov 3, 2026
ab39eb4
FRLG walker: a battle's text box after a leg's watch is a battle on i…
dayfine Nov 3, 2026
85314fa
Merge frlg/short-leg-late-battle (frlg/overworld-sim's AutoStory GRIN…
dayfine Nov 3, 2026
6e113d6
Merge frlg/overworld-sim (battle loop: end on the overworld after RUN…
dayfine Nov 3, 2026
0c95ec4
FRLG walker: a battle's black after any leg, or before a step, is a b…
dayfine Nov 3, 2026
dfd6e34
Merge frlg/overworld-sim (a disabled move, and a move list that never…
dayfine Nov 3, 2026
2ed0be9
Merge frlg/autostory-mvp (matchup attacks in every battle) into frlg/…
dayfine Nov 3, 2026
5a5d3b6
Merge frlg/autostory-mvp (HP bar share, nurse for yellow HP, move pro…
dayfine Nov 3, 2026
74e3885
Merge frlg/overworld-sim (31de679f3: verify mode from #122) into frlg…
dayfine Nov 3, 2026
ddc9fc8
Merge frlg/overworld-sim (1a206a4dc: battle sim from #120) into frlg/…
dayfine Nov 3, 2026
6e7c183
Merge frlg/overworld-sim into frlg/short-leg-late-battle
dayfine Nov 3, 2026
1f7f6c7
Merge frlg/overworld-sim (the console's TEACH through the TM CASE) in…
dayfine Nov 3, 2026
14019ff
Merge frlg/overworld-sim (GRIND until the boss is winnable; TEACH ski…
dayfine Nov 3, 2026
a58f7f0
Merge frlg/overworld-sim (GRIND/CAPTURE missed flash, TEACH after-for…
dayfine Nov 3, 2026
d5212df
Merge frlg/overworld-sim (bag rows below an item the reader can't nam…
dayfine Nov 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* Battle Text Box Detector
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "CommonTools/Images/SolidColorTest.h"
#include "Tests/ImageUnitTest.h"
#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h"
#include "PokemonFRLG_BattleTextBoxDetector.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonFRLG{


namespace{

// Measured on the console's captures (1920x1080): blue (34, 88, 121),
// white (236, 229, 235), gold (222, 176, 52).
const FloatPixel BOX_BLUE{0.176, 0.357, 0.467};
const FloatPixel FRAME_GOLD{0.493, 0.391, 0.116};

}


BattleTextBoxDetector::BattleTextBoxDetector(Color color)
: m_color(color)
// As BattleDialogDetector's.
, m_blue_top(0.0372308, 0.750154, 0.925538, 0.00934615)
, m_blue_right(0.956615, 0.7595, 0.00615385, 0.185885)
, m_white_line(0.0385, 0.7337, 0.923, 0.0058)
, m_gold_band(0.0385, 0.7163, 0.923, 0.0087)
{}

void BattleTextBoxDetector::make_overlays(VideoOverlaySet& items) const{
const BoxOption& GAME_BOX = GameSettings::instance().GAME_BOX;
items.add(m_color, GAME_BOX.inner_to_outer(m_blue_top));
items.add(m_color, GAME_BOX.inner_to_outer(m_blue_right));
items.add(m_color, GAME_BOX.inner_to_outer(m_white_line));
items.add(m_color, GAME_BOX.inner_to_outer(m_gold_band));
}

bool BattleTextBoxDetector::detect(const ImageViewRGB32& screen){
const ImageViewRGB32 game_screen = extract_box_reference(screen, GameSettings::instance().GAME_BOX);
return is_solid(extract_box_reference(game_screen, m_blue_top), BOX_BLUE, 0.25, 20)
&& is_solid(extract_box_reference(game_screen, m_blue_right), BOX_BLUE, 0.25, 20)
&& is_white(extract_box_reference(game_screen, m_white_line), 500, 20)
&& is_solid(extract_box_reference(game_screen, m_gold_band), FRAME_GOLD, 0.10, 40);
}



class Test_BattleTextBoxDetector : public ImageUnitTest{
public:
Test_BattleTextBoxDetector(const std::string& image, bool expected)
: ImageUnitTest("PokemonFRLG::BattleTextBoxDetector - " + image, {image})
, m_expected(expected)
{}

virtual UnitTestResult run_on_images(
Logger& logger, CancellableScope& scope,
const std::vector<ImageRGB32>& images
) const override{
BattleTextBoxDetector detector(COLOR_RED);
return detector.detect(images[0]) == m_expected;
}

private:
bool m_expected;
};

void add_tests_BattleTextBoxDetector(UnitTestDatabase& database){
// Console B's captures: Mt. Moon B1F and Route 1, a battle's opening
// with the box still empty, after a leg's watch saw nothing; Viridian
// Forest, "Wild CATERPIE" with its HP box.
database.add<Test_BattleTextBoxDetector>("PokemonFRLG/BattleTextBoxDetector/MtMoonB1F_Opening_True.png", true);
database.add<Test_BattleTextBoxDetector>("PokemonFRLG/BattleTextBoxDetector/Route1_Opening_True.png", true);
database.add<Test_BattleTextBoxDetector>("PokemonFRLG/BattleTextBoxDetector/ViridianForest_WildCaterpie_True.png", true);
// The cave just before that battle, and black below Pewter's Pokemon
// Center, which BattleDialogDetector reads as the box.
database.add<Test_BattleTextBoxDetector>("PokemonFRLG/BattleTextBoxDetector/MtMoonB1F_Overworld_False.png", false);
database.add<Test_BattleTextBoxDetector>("PokemonFRLG/BattleTextBoxDetector/PewterPokemonCenter_BlackBelow_False.png", false);
}



}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Battle Text Box Detector
*
* From: https://github.com/PokemonAutomation/
*
* A battle's text box, by its frame: blue inside a white line and a gold
* band. It shows from the moment a battle's opening ends, before the HP
* boxes and the "Wild <Pokemon> appeared!" that BattleScreenDetector needs:
* in Mt. Moon the HP boxes came over 5s after the step that set the battle
* off (console B, 2026-11-03). Black below a small room, which passes for
* the box's blue (BattleDialogDetector), has neither line.
*
* English layout only: the Japanese box sits a little lower and narrower
* (BattleDialogDetector) and wasn't checked.
*
*/

#ifndef PokemonAutomation_PokemonFRLG_BattleTextBoxDetector_H
#define PokemonAutomation_PokemonFRLG_BattleTextBoxDetector_H

#include "Common/Cpp/Color.h"
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "CommonFramework/ImageTools/ImageBoxes.h"
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
#include "CommonTools/VisualDetector.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonFRLG{


class BattleTextBoxDetector : public StaticScreenDetector{
public:
BattleTextBoxDetector(Color color = COLOR_RED);

virtual void make_overlays(VideoOverlaySet& items) const override;
virtual bool detect(const ImageViewRGB32& screen) override;

private:
Color m_color;
ImageFloatBox m_blue_top;
ImageFloatBox m_blue_right;
ImageFloatBox m_white_line;
ImageFloatBox m_gold_band;
};


void add_tests_BattleTextBoxDetector(UnitTestDatabase& database);



}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,46 @@
return sight;
}

// The tiles an open `step` from `start` may have ended on with nothing
// seen: its end, and up to OPEN_LEG_SHORTFALL tiles before it.
std::vector<StopCandidate> short_candidates(const std::vector<MapData>& maps, const MapPosition& start, const RouteStep& step){
const std::vector<MapPosition> tiles = leg_tiles(maps, start, step);
std::vector<StopCandidate> candidates;
for (size_t c = 0; c <= (size_t)OPEN_LEG_SHORTFALL && c < tiles.size(); c++){
candidates.push_back(StopCandidate{tiles[tiles.size() - 1 - c], std::nullopt});
}
return candidates;
}

// Whether `next` is a hold to a wall.
bool is_wall_hold(const RouteStep& next){
return next.action == RouteAction::WALK && next.to_wall && !next.warps;
}

// Whether `next`, a hold to a wall, ends on its planned tile from each of
// `candidates`: then it finds the player itself.
bool hold_finds(const std::vector<MapData>& maps, const std::vector<StopCandidate>& candidates, const RouteStep& next){
for (const StopCandidate& candidate : candidates){
const std::optional<WallHold> hold = hold_to_wall(maps, candidate.player, false, next.direction);
if (!hold || !(hold->end == next.end)){
return false;
}
}
return true;
}

// How many tiles `anchor`'s holds and way back walk, as far as planned.
int anchor_tiles(const Reanchor& anchor){
int tiles = 0;
for (const RouteStep& step : anchor.holds){
tiles += step.tiles;
}
for (const RouteStep& step : anchor.back){
tiles += step.tiles;
}
return tiles;
}

// The route's lines in the route log the games share, as BDSP's walker
// writes them, under the caller's label.
class RouteLogLines{
Expand Down Expand Up @@ -118,6 +158,9 @@
const std::string leg = to_string(step);
m_reholds = m_last_late && leg == m_last ? m_reholds + 1 : 0;
StepOutcome outcome = m_calls.walk(step, facing);
if (outcome.end == StepEnd::BATTLE_BEFORE){
return outcome;
}
if (outcome.end == StepEnd::DONE && crossing && m_calls.watch_for_trainer){
outcome = StepOutcome{.end = m_calls.watch_for_trainer(step, crossing->approach_tiles), .walked = std::nullopt};
}
Expand Down Expand Up @@ -173,7 +216,7 @@
, m_facing(facing)
{}

void run(){

Check warning on line 219 in SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::RouteRun::run(): function_lines=35 (advisory above 25, limit 50)
m_lines.plan(m_at, m_plan);
while (m_next < m_plan.size()){
const RouteStep step = m_plan[m_next];
Expand All @@ -182,9 +225,12 @@
const std::optional<TrainerCrossing> crossing = m_next >= m_holds ? crossing_here(step) : std::nullopt;
const StepOutcome outcome = m_steps.walk(step, m_facing, crossing);
if (outcome.end == StepEnd::DONE){
const std::optional<MapPosition> start = m_next >= m_unchecked ? m_at : std::nullopt;
remember_walked(step);
m_at = step.end;
m_facing = step.facing_after;
m_lines.done(step, ++m_next, m_holds);
check_open_leg(step, start);
continue;
}
const std::optional<MapPosition> start = m_next >= m_holds ? m_at : std::nullopt;
Expand All @@ -193,6 +239,13 @@
m_calls.fail(std::format("walk_overworld_route(): stopped part-way {} times.", m_stops));
return;
}
if (outcome.end == StepEnd::BATTLE_BEFORE){
if (!after_battle_before(step)){
return;
}
continue;
}
m_walked.reset();
if (!after_stop(step, outcome, start, crossing)){
return;
}
Expand All @@ -211,6 +264,44 @@
return trainer_crossing(*maps(), *m_at, step);
}

// A walk the next step's battle check may find stopped part-way:
// where in the plan it is, and where it started from, if known.
struct Walked{
RouteStep step;
size_t index = 0;
std::optional<MapPosition> start;
};

// `step`, m_plan[m_next], walked to its end. Only a walk not onto a
// warp can have stopped part-way unseen.
void remember_walked(const RouteStep& step){
m_walked.reset();
if (step.action == RouteAction::WALK && !step.warps){
m_walked = Walked{step, m_next, m_next >= m_holds ? m_at : std::nullopt};
}
}

// A wild battle set off before `step`, m_plan[m_next], showed before
// it started. The step walked before it may have been stopped by the
// battle anywhere: the player is found again on it, as after a battle
// seen after its hold, and never taken to be on `step`'s line (the
// next leg was held from where the battle left the player, and the
// route went on 6 tiles off: console B, 2026-11-03, Mt. Moon B2F).
// With nothing walked since the plan was made, the player is where
// `step` starts. False once the route has failed.
bool after_battle_before(const RouteStep& step){
if (!m_walked){
m_calls.log("Route: a wild battle before " + to_string(step) + ", with nothing walked since the plan: walking it from its start.");
return true;
}
const Walked walked = std::move(*m_walked);
m_walked.reset();
m_calls.log("Route: a wild battle before " + to_string(step) + " was set off on " + to_string(walked.step) + ", which may have stopped part-way.");
m_next = walked.index;
m_at = walked.start;
return after_stop(walked.step, StepOutcome{.end = StepEnd::LATE, .walked = std::nullopt}, walked.start, std::nullopt);
}

// After a stop on `step`, from `start` if known. False once the route
// has failed.
bool after_stop(
Expand Down Expand Up @@ -262,9 +353,59 @@
return true;
}

// After `step`, an open leg from `start` (not known: not checked) that
// ended with nothing seen, while m_plan[m_next] is the step after it.
// A hold to a wall after a leg that ended short ends somewhere else
// with nothing to show it (r16), unless it ends on its tile from
// wherever the leg left the player. If it doesn't, finds the player at
// walls first; without such holds, or if they and the way back walk
// farther than the leg there and back, trusts the leg. Only there, and
// only that far, to keep the check cheap: in Viridian Forest (r16) it
// walks 10 tiles after "west 6", while after Route 1's "west 4" the
// only holds walk 30. An open leg after a short one walks on off the
// route, which isn't checked.
void check_open_leg(const RouteStep& step, const std::optional<MapPosition>& start){
if (maps() == nullptr || m_planning->open_leg_check != OpenLegCheck::REANCHOR || !start
|| step.action != RouteAction::WALK || step.to_wall || step.warps
|| m_next >= m_plan.size() || !is_wall_hold(m_plan[m_next])
){
return;
}
const std::vector<StopCandidate> candidates = short_candidates(*maps(), *start, step);
if (candidates.size() < 2 || hold_finds(*maps(), candidates, m_plan[m_next])){
return;
}
const std::optional<Reanchor> anchor = plan_reanchor(*maps(), candidates, step, m_planning->costs);
if (!anchor || anchor->holds.empty() || anchor_tiles(*anchor) > 2 * step.tiles){
m_calls.log("Route: no holds to walls within " + std::to_string(2 * step.tiles) + " tiles find the player if " + to_string(step) + " ended short; taken as walked.");
return;
}
m_calls.log(std::format(
"Route: position check: {} may have ended up to {} tiles short with nothing seen; finding the player at walls.",
to_string(step), OPEN_LEG_SHORTFALL
));
replan(*anchor, m_next);
m_at = std::nullopt;
m_lines.plan(std::nullopt, m_plan);
}

// The plan from here: `anchor`'s holds and way back, then the old
// plan's steps from `rest` on. Neither the holds nor the way back are
// checked as open legs again.
void replan(const Reanchor& anchor, size_t rest){
std::vector<RouteStep> plan = anchor.holds;
plan.insert(plan.end(), anchor.back.begin(), anchor.back.end());
plan.insert(plan.end(), m_plan.begin() + rest, m_plan.end());
m_plan = std::move(plan);
m_walked.reset();
m_holds = anchor.holds.size();
m_unchecked = m_holds + anchor.back.size();
m_next = 0;
}

// Finds the player on one of `candidates`, plans the way back to the
// end of `step` (m_plan[m_next]), and the rest of the route after it.
bool rejoin(const RouteStep& step, const std::vector<StopCandidate>& candidates, WalkedTiles walked){

Check warning on line 408 in SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::RouteRun::rejoin( const RouteStep & step , const std :: vector<StopCandidate> & candidates , WalkedTiles walked): function_lines=38 (advisory above 25, limit 50)
std::string where;
for (const StopCandidate& candidate : candidates){
where += (where.empty() ? "" : "; ") + to_string(candidate);
Expand All @@ -283,12 +424,7 @@
);
return false;
}
std::vector<RouteStep> plan = anchor->holds;
plan.insert(plan.end(), anchor->back.begin(), anchor->back.end());
plan.insert(plan.end(), m_plan.begin() + m_next + 1, m_plan.end());
m_plan = std::move(plan);
m_holds = anchor->holds.size();
m_next = 0;
replan(*anchor, m_next + 1);
if (m_holds > 0){
m_at = std::nullopt;
m_lines.plan(std::nullopt, m_plan);
Expand Down Expand Up @@ -318,11 +454,17 @@
size_t m_next = 0;
size_t m_stops = 0;
size_t m_holds = 0; // of the plan, holds to walls that find the player
size_t m_unchecked = 0; // of the plan, the holds and the way back after them
std::optional<Walked> m_walked; // the step walked last, if it may have stopped part-way
};

}


WalkedTiles walked_before_warp(const RouteStep& step){
return WalkedTiles{.min = 1, .max = step.tiles - 1};
}

std::optional<TrainerCrossing> trainer_crossing(
const std::vector<MapData>& maps, const MapPosition& start, const RouteStep& step
){
Expand Down
Loading
Loading