diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index e1ce8a64ca..c8cab96caa 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -280,9 +280,9 @@ jobs: uses: actions/checkout@v7 if: inputs.run-tests with: - # The fork's fixtures: upstream's plus the SystemKeyboardDetector screenshots. + # The fork's fixtures: upstream's, the SystemKeyboardDetector screenshots and pending FRLG image submissions. repository: 'dayfine/CommandLineTests' - ref: '5de4ff774f9141d904c4b5c557fa7b9dc85eaef1' + ref: '3a6708327b225e71a329d77d8d067305ea4058d2' path: 'CommandLineTests' # The unit tests assume the downloadable resources they need are already downloaded diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLateBattle.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLateBattle.cpp new file mode 100644 index 0000000000..11eabed102 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLateBattle.cpp @@ -0,0 +1,68 @@ +/* Overworld Late Battle + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonFRLG_OverworldWalk.h" +#include "PokemonFRLG_OverworldLateBattle.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +const std::vector& wild_battle_lags(){ + static const std::vector LAGS{ + {ScreenEvent::BATTLE_STARTED, OverworldWalkSteps::LATE_WILD_BATTLE_DELAY}, + }; + return LAGS; +} + +int tile_reached(Milliseconds since_hold, Milliseconds tile_time, int tiles){ + if (tile_time.count() <= 0){ + return tiles; + } + if (since_hold.count() < 0){ + return 0; + } + const int begun = (int)((since_hold + tile_time / 2) / tile_time) + 1; + return std::clamp(begun, 0, tiles); +} + +LateStop place_late_stop( + const RouteStep& step, Milliseconds tile_time, + const ScreenEventLog& log, WallClock hold_start, WallClock now +){ + LateStop out; + out.verdict = Pokemon::classify_interrupt(log, {hold_start, now}, {}); + if (out.verdict.kind != Pokemon::InterruptKind::WILD_ENCOUNTER){ + return out; + } + const std::optional pressed = Pokemon::interrupt_step_time(out.verdict, wild_battle_lags()); + if (!pressed){ + return out; + } + const Milliseconds since_hold = std::chrono::duration_cast(*pressed - hold_start); + out.walked = walked_near(step, tile_reached(since_hold, tile_time, step.tiles), tile_time); + return out; +} + +std::string late_stop_line(const RouteStep& step, const LateStop& stop, WallClock hold_start){ + std::string out = std::format( + "Route: seen after the hold of {}: {}", + to_string(step), Pokemon::describe(stop.verdict, hold_start) + ); + if (stop.walked){ + return out + std::format("; {} to {} tiles in.", stop.walked->min, stop.walked->max); + } + return out + "; not placed."; +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLateBattle.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLateBattle.h new file mode 100644 index 0000000000..55eae62234 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLateBattle.h @@ -0,0 +1,75 @@ +/* Overworld Late Battle + * + * From: https://github.com/PokemonAutomation/ + * + * Where a wild battle seen only after an open leg's hold stopped the + * player. The walker records the leg's screens as timed events + * (ScreenEventLog) through the hold and the watch after it. The + * shared classifier (Pokemon::classify_interrupt()) says what stopped the + * player, and a wild battle is placed by when its first timed event + * showed, not by when the watcher that cleared it fired: that is after the + * hold, and the leg could have stopped anywhere along it. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_OverworldLateBattle_H +#define PokemonAutomation_PokemonFRLG_OverworldLateBattle_H + +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon/AutoStory/Pokemon_Interrupts.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" +#include "PokemonFRLG_OverworldReanchor.h" +#include "PokemonFRLG_OverworldRoute.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The events of a wild battle FRLG has timed from its step: its text box +// ("Wild appeared!", BATTLE_STARTED), LATE_WILD_BATTLE_DELAY +// after the step on Route 1 (console B, 2026-11-01, run 7). The flashes +// are what the walker missed, and the wipe isn't timed on the console yet. +const std::vector& wild_battle_lags(); + +// The tile of a leg (from 1) that a step begun `since_hold` into its hold +// reaches: the steps begin a tile apart from the hold's start. At most +// `tiles`. +int tile_reached(Milliseconds since_hold, Milliseconds tile_time, int tiles); + +// What stopped an open leg after its hold, and where. +struct LateStop{ + Pokemon::InterruptVerdict verdict; + // The tiles of the leg the player may be on. Set for a wild battle + // whose step the log times. + std::optional walked; +}; + +// For `step`, an open leg held from `hold_start` at `tile_time` a tile, +// after which something was seen and cleared by `now`: what `log` shows +// stopped the player there, and for a wild battle, the tile its step +// reached (tile_reached()), give or take walked_near()'s slack. +LateStop place_late_stop( + const RouteStep& step, Milliseconds tile_time, + const ScreenEventLog& log, WallClock hold_start, WallClock now +); + +// "Route: seen after the hold of : wild encounter: +5035 ms battle +// started rose; 5 to 7 tiles in.", times from `hold_start`. +std::string late_stop_line(const RouteStep& step, const LateStop& stop, WallClock hold_start); + + +// Registers the late battle unit tests, on the sim. add_tests_OverworldSim() +// calls it. +void add_tests_OverworldLateBattle(UnitTestDatabase& database); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLateBattle_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLateBattle_Tests.cpp new file mode 100644 index 0000000000..d6b6b4e46b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLateBattle_Tests.cpp @@ -0,0 +1,194 @@ +/* Overworld Late Battle Tests + * + * From: https://github.com/PokemonAutomation/ + * + * A wild battle seen only after an open leg's hold, on the sim: the walker + * must place the player within a few tiles of where the battle stopped + * them, find them again and finish the route. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonFRLG_OverworldLateBattle.h" +#include "PokemonFRLG_OverworldMaps.h" +#include "PokemonFRLG_OverworldReanchor.h" +#include "PokemonFRLG_OverworldSim.h" +#include "PokemonFRLG_OverworldWalk.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +using namespace std::chrono_literals; + +struct LateBattleInput{ + std::string name; + // The leg's step (from 1) the battle starts on. + int battle_step = 1; +}; + +struct LateBattleExpectation{ + // The most the walker's bound on the player's tile may span, in tiles. + int max_spread = 0; +}; + +struct LateBattleCase{ + LateBattleInput input; + LateBattleExpectation expectation; +}; + +const std::vector& maps(){ + static const std::vector MAPS = overworld_maps(); + return MAPS; +} + +// The bound run_route() logged for the stop: " stopped to +// tiles in; ...". +std::optional logged_bound(const std::vector& log){ + for (const std::string& line : log){ + const size_t at = line.find(" stopped "); + WalkedTiles bound; + if (at != std::string::npos && std::sscanf(line.c_str() + at, " stopped %d to %d tiles in", &bound.min, &bound.max) == 2){ + return bound; + } + } + return std::nullopt; +} + +// A walk on the sim: how it ended, and what the walker and the sim logged. +struct LateBattleRun{ + std::string failure; + SimState state; + std::vector log; +}; + +std::string describe(const LateBattleRun& run){ + std::string out = std::format("\n ended on {}, failure: \"{}\"", to_string(run.state.position), run.failure); + for (const std::string& line : run.log){ + out += "\n " + line; + } + for (const std::string& line : run.state.log){ + out += "\n " + line; + } + return out; +} + +const MapPosition LEG_START{"Route10", {5, 10}}; +const MapPosition LEG_END{"Route10", {16, 10}}; + +// "east 11" from LEG_START through Route 10's grass, with a battle on +// `battle_step` whose flashes are missed. +LateBattleRun walk_with_battle(int battle_step, SimWalkerRules rules){ + RouteStep east; + east.direction = Direction::EAST; + east.tiles = 11; + east.wild = true; + east.facing_after = Direction::EAST; + east.end = LEG_END; + SimScript script; + script.encounters = {{.grass_step = battle_step, .seen_late = true}}; + script.timing.battle_opening = OverworldWalkSteps::LATE_WILD_BATTLE_DELAY; + script.timing.battle_screen = OverworldWalkSteps::LATE_WILD_BATTLE_DELAY + 800ms; + OverworldSim sim(maps(), LEG_START, Direction::EAST, script); + OverworldSimWalker walker(sim, rules); + const RoutePlanning planning{.maps = &maps(), .costs = default_route_costs(), .start = LEG_START, .label = "Sim"}; + LateBattleRun run; + run.failure = walker.run({east}, planning); + run.state = sim.state(); + run.log = walker.log(); + return run; +} + +std::string bound_text(const std::optional& bound){ + return bound ? std::format("{} to {}", bound->min, bound->max) : std::string("no bound"); +} + +// Console B, FRLG 00.27 run 2, 2026-11-09 (~/FRLGLab/captures/ +// 2026-11-09-0027-route10-legs.log): on the open leg "east 11" across +// Route 10, wild battles (Voltorb) showed only after the hold. The walker +// took the player to be anywhere on the leg ("stopped 0 to 11 tiles in") +// and walked "east 12 (to wall)" to find them, into a second battle. Here +// the leg crosses Route 10's grass on row 10, the battle's flashes are +// missed, its text box ("Wild appeared!") shows +// LATE_WILD_BATTLE_DELAY after its step and its HP boxes 800 ms after +// that, as in Mt. Moon (console B, 2026-11-03, row 00.13). After running, +// the walker must bound the player to a few tiles around the battle's. +class Test_OverworldLateBattle : public UnitTest{ +public: + explicit Test_OverworldLateBattle(LateBattleCase test_case) + : UnitTest("PokemonFRLG::OverworldLateBattle - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + try{ + return run_case(); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + UnitTestResult run_case() const{ + const int battle = m_case.input.battle_step; + const LateBattleRun now = walk_with_battle(battle, {}); + if (!now.failure.empty() || !(now.state.position == LEG_END) || !now.state.violations.empty()){ + return "Expected the battle run from and " + to_string(LEG_END) + " reached:" + describe(now); + } + const std::optional bound = logged_bound(now.log); + if (!bound || bound->min > battle || bound->max < battle || bound->max - bound->min > m_case.expectation.max_spread){ + return std::format( + "Expected the player placed within {} tiles around tile {} of the leg, got {}:{}", + m_case.expectation.max_spread, battle, bound_text(bound), describe(now) + ); + } + SimWalkerRules unplaced; + unplaced.place_late_battle = false; + const LateBattleRun old = walk_with_battle(battle, unplaced); + const std::optional whole = logged_bound(old.log); + if (!whole || whole->min != 0 || whole->max != 11){ + return "Expected the whole leg, 0 to 11 tiles, without the rule, as on the console, got " + bound_text(whole) + ":" + describe(old); + } + return true; + } + + LateBattleCase m_case; +}; + +} // namespace + + +const LateBattleCase LATE_BATTLE_CASES[] = { + { + .input = {.name = "a battle on step 3 of an open leg, seen after the hold", .battle_step = 3}, + .expectation = {.max_spread = 2}, + }, + { + .input = {.name = "a battle on step 6 of an open leg, seen after the hold", .battle_step = 6}, + .expectation = {.max_spread = 2}, + }, + { + .input = {.name = "a battle on step 10, its HP boxes after the leg's watch", .battle_step = 10}, + .expectation = {.max_spread = 2}, + }, +}; + +void add_tests_OverworldLateBattle(UnitTestDatabase& database){ + for (const LateBattleCase& item : LATE_BATTLE_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.cpp index cb57749b33..b53dd793ed 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.cpp @@ -71,6 +71,13 @@ constexpr std::string_view MAP_NAMES[] = { "SSAnne_CaptainsOffice", // Vermilion Gym, past the tree Cut fells. "VermilionCity_Gym", + // The BIKE VOUCHER and the BICYCLE. + "VermilionCity_PokemonFanClub", + "CeruleanCity_BikeShop", + // East over Route 9 to Route 10's Pokemon Center. + "Route9", + "Route10", + "Route10_PokemonCenter_1F", "CeruleanCave_1F", "CeruleanCave_2F", "CeruleanCave_B1F", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor.cpp index 62e20d0528..588b63c357 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor.cpp @@ -6,6 +6,7 @@ #include #include +#include "Pokemon/Navigation/Pokemon_WallRecovery.h" #include "PokemonFRLG_OverworldReanchor.h" namespace PokemonAutomation{ @@ -126,15 +127,6 @@ struct Hypothesis{ bool operator==(const Hypothesis& other) const = default; }; -bool converged(const std::vector& positions){ - for (const Hypothesis& position : positions){ - if (!(position.at == positions[0].at)){ - return false; - } - } - return true; -} - // Whether a person stands, or may wander, on a tile `wall` passes after // `from`. The step rules don't have people, but one there stops the player // short of the wall. @@ -183,41 +175,33 @@ std::optional, RouteStep>> hold_all( return std::make_pair(std::move(ends), hold); } -int total_tiles(const std::vector& legs){ - int total = 0; - for (const RouteStep& leg : legs){ - total += leg.tiles; - } - return total; -} +using Found = Pokemon::WallAnchor; -struct Found{ - std::vector holds; - MapPosition at; +// Runtime world alternatives and movement mode, separate from the policy type. +struct ReanchorContext{ + const std::vector>& worlds; + bool surfing = false; }; -// Every way of exactly `depth` holds that brings all of `positions` to one -// tile, in a fixed order. -void search( - const std::vector>& worlds, const std::vector& positions, bool surfing, - size_t depth, std::vector& holds, std::vector& found -){ - for (Direction direction : Pokemon::ALL_DIRECTIONS){ - auto next = hold_all(worlds, positions, surfing, direction); - if (!next || next->first == positions || next->second.tiles == 0){ - continue; - } - holds.push_back(next->second); - if (converged(next->first)){ - if (depth == 1){ - found.push_back(Found{holds, next->first[0].at}); - } - }else if (depth > 1){ - search(worlds, next->first, surfing, depth - 1, holds, found); +class ReanchorPolicy{ +public: + using Hypothesis = PokemonFRLG::Hypothesis; + using Position = MapPosition; + using Hold = RouteStep; + + static Position position(const Hypothesis& hypothesis){ return hypothesis.at; } + static int tiles(const Hold& hold){ return hold.tiles; } + static std::optional> hold( + const ReanchorContext& context, const std::vector& positions, Direction direction + ){ + auto next = hold_all(context.worlds, positions, context.surfing, direction); + if (!next){ + return std::nullopt; } - holds.pop_back(); + return Pokemon::WallRecoveryStep{std::move(next->first), std::move(next->second)}; } -} + +}; // Whether `route`, planned from `at` on other maps, walks the same on // `world`: each leg to a wall stops at its end, each open leg gets there. @@ -257,18 +241,9 @@ bool walks_same_everywhere( std::vector find_player( const std::vector>& worlds, const std::vector& positions, bool surfing ){ - std::vector found; - if (converged(positions)){ - found.push_back(Found{{}, positions[0].at}); - } - for (size_t depth = 1; found.empty() && depth <= MAX_HOLDS; depth++){ - std::vector holds; - search(worlds, positions, surfing, depth, holds, found); - std::stable_sort(found.begin(), found.end(), [](const Found& a, const Found& b){ - return total_tiles(a.holds) < total_tiles(b.holds); - }); - } - return found; + return Pokemon::find_wall_anchors( + positions, ReanchorContext{.worlds = worlds, .surfing = surfing}, {.max_holds = MAX_HOLDS} + ); } } // namespace diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp index b6953fedbe..f3e58ab5f6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp @@ -6,14 +6,18 @@ #include #include +#include +#include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/ImageTools/ImageDiff.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/Async/InferenceSession.h" #include "CommonTools/VisualDetectors/BlackScreenDetector.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_OverworldLateBattle.h" #include "PokemonFRLG_OverworldRecoveryWalk.h" namespace PokemonAutomation{ @@ -41,13 +45,20 @@ Pokemon::LegSpec leg_spec(const RouteStep& step, MoveSpeed speed){ return leg; } -// One step of a route, for a player facing `facing`. If an open leg -// stopped part-way, returns how far the timing puts the player along it. -std::optional walk_step( +// How one step's walk went: if an open leg stopped part-way, how far the +// timing puts the player along it; and when its hold started. +struct StepWalk{ + std::optional stopped; + WallClock hold_start; +}; + +// One step of a route, for a player facing `facing`. +StepWalk walk_step( ConsoleHandle& console, ProControllerContext& context, const RouteStep& step, std::optional facing, const OverworldInterruptSet& interrupts, MoveSpeed speed ){ + StepWalk out{std::nullopt, current_time()}; switch (step.action){ case RouteAction::WALK:{ // Turning first, with a tap, makes the hold start from a standstill @@ -59,19 +70,23 @@ std::optional walk_step( if (const std::string note = speed_note(step, speed); !note.empty()){ console.log(note); } + context.wait_for_all_requests(); + out.hold_start = current_time(); const Pokemon::LegResult result = Pokemon::walk_leg( console, context, leg_mover(step, speed), step.wild ? interrupts.wild : interrupts.other, leg_spec(step, speed), MAX_INTERRUPTIONS_PER_LEG ); if (result.stopped){ - return walked_near(step, result.tiles, tile_time(step, speed)); + out.stopped = walked_near(step, result.tiles, tile_time(step, speed)); + return out; } if (step.warps){ wait_out_warp(console, context, step); static_assert(AFTER_WARP_BATTLE_WATCH >= AFTER_WARP); if (run_from_battle_after_warp(console, context)){ console.log("Route: a wild battle, not the warp to " + step.end.map + "; the player is on " + to_string(step) + " before the warp.", COLOR_ORANGE); - return walked_before_warp(step); + out.stopped = walked_before_warp(step); + return out; } } break; @@ -83,9 +98,54 @@ std::optional walk_step( pbf_press_dpad(context, dpad(step.direction), FACE_HOLD, STEP_RELEASE); break; } - return std::nullopt; + return out; } +// An open leg's screens, recorded with the interrupts' observer from +// before its hold until the watch after it ends, alongside the walker's +// own watchers. Records nothing for other steps, or with no observer. +class LegRecorder{ +public: + LegRecorder( + ConsoleHandle& console, ProControllerContext& context, + const OverworldInterruptSet& interrupts, const RouteStep& step + ) + : m_observer(records(interrupts, step) ? interrupts.make_observer() : nullptr) + , m_watcher(m_observer ? std::make_unique(*m_observer) : nullptr) + , m_session(context, console, callbacks()) + {} + + // Where what was seen after `step`'s hold, from `hold_start`, stopped + // the player (place_late_stop()); nothing if not recorded or not placed. + std::optional place( + ConsoleHandle& console, const RouteStep& step, Milliseconds tile_time, WallClock hold_start + ) const{ + if (!m_watcher){ + return std::nullopt; + } + const LateStop stop = place_late_stop(step, tile_time, m_watcher->log(), hold_start, current_time()); + console.log(late_stop_line(step, stop, hold_start), COLOR_ORANGE); + return stop.walked; + } + +private: + static bool records(const OverworldInterruptSet& interrupts, const RouteStep& step){ + const bool open_leg = step.action == RouteAction::WALK && !step.to_wall && !step.warps; + return open_leg && interrupts.make_observer; + } + std::vector callbacks() const{ + if (!m_watcher){ + return {}; + } + return {*m_watcher}; + } + + std::unique_ptr m_observer; + std::unique_ptr m_watcher; + // With no watcher it runs nothing. + InferenceSession m_session; +}; + // walk_step(), then a watch for what its last step set off. Not DONE if // the player may have stopped short of the step's end: an open leg // stopped, or something seen after the hold, which may have stopped it @@ -104,8 +164,10 @@ StepOutcome walk_and_watch( } clear_open_message(console, context, step.wild ? interrupts.wild : interrupts.other, step); check_overworld(console, context, step); - if (const std::optional walked = walk_step(console, context, step, facing, interrupts, speed)){ - return StepOutcome{.end = StepEnd::STOPPED, .walked = walked}; + const LegRecorder recorder(console, context, interrupts, step); + const StepWalk walked = walk_step(console, context, step, facing, interrupts, speed); + if (walked.stopped){ + return StepOutcome{.end = StepEnd::STOPPED, .walked = walked.stopped}; } if (step.action == RouteAction::FACE){ return StepOutcome{}; @@ -118,7 +180,10 @@ StepOutcome walk_and_watch( } // After a warp it was checked for already. const bool done = late == 0 || step.warps || step.action != RouteAction::WALK; - return StepOutcome{.end = done ? StepEnd::DONE : StepEnd::LATE, .walked = std::nullopt}; + if (done){ + return StepOutcome{}; + } + return StepOutcome{.end = StepEnd::LATE, .walked = recorder.place(console, step, tile_time(step, speed), walked.hold_start)}; } // Holds on along `step`, stopped part-way off the grass, until its warp's diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp index 078f718233..d18e2f71e0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp @@ -10,6 +10,7 @@ #include "Common/Cpp/Exceptions.h" #include "CommonFramework/Exceptions/OperationFailedException.h" #include "Pokemon/Navigation/Pokemon_LegWalker.h" +#include "PokemonFRLG_OverworldLateBattle.h" #include "PokemonFRLG_OverworldReanchor.h" #include "PokemonFRLG_OverworldWalk.h" #include "PokemonFRLG_OverworldSim.h" @@ -44,6 +45,50 @@ std::string_view sim_screen_name(SimScreen screen){ } +WallClock sim_wall_clock(Milliseconds time){ + return WallClock() + time; +} + +namespace{ + +ScreenEvents observed_events(SimScreen screen){ + switch (screen){ + case SimScreen::MESSAGE: + return {ScreenEvent::DIALOG_BOX}; + case SimScreen::BATTLE_WIPE: + case SimScreen::FADE: + return {ScreenEvent::FADE_BLACK}; + case SimScreen::BATTLE_OPENING: + case SimScreen::BATTLE: + return {ScreenEvent::BATTLE_STARTED}; + case SimScreen::OVERWORLD: + case SimScreen::START_MENU: + case SimScreen::BATTLE_FLASH: + break; + } + return {}; +} + +} // namespace + +ScreenEventLog screen_event_log(const SimState& state, Milliseconds from){ + ScreenEventLog log; + SimScreen screen = state.shown.empty() ? state.screen : state.shown.front().screen; + size_t next = 0; + for (; next < state.shown.size() && state.shown[next].at <= from; next++){ + screen = state.shown[next].screen; + } + Milliseconds at = from; + log.record(observed_events(screen), sim_wall_clock(at)); + for (; next < state.shown.size(); next++){ + at = std::max(at, state.shown[next].at); + log.record(observed_events(state.shown[next].screen), sim_wall_clock(at)); + } + log.record(observed_events(state.screen), sim_wall_clock(std::max(at, state.time))); + return log; +} + + namespace{ // A short press: TURN_TAP and TURN_SETTLE. @@ -105,7 +150,7 @@ OverworldSim::OverworldSim(std::vector maps, MapPosition start, Directi } m_player = PlayerState{start.point, elevation_at(map(), start.point), false}; m_state.facing = facing; - m_state.screen = m_script.start_menu_open ? SimScreen::START_MENU : SimScreen::OVERWORLD; + show(m_script.start_menu_open ? SimScreen::START_MENU : SimScreen::OVERWORLD, m_now); update_view(); } @@ -135,11 +180,17 @@ void OverworldSim::schedule(Milliseconds at, SimScreen screen, bool unfreeze){ void OverworldSim::advance_to(Milliseconds time){ while (!m_pending.empty() && m_pending.front().at <= time){ - m_state.screen = m_pending.front().screen; + show(m_pending.front().screen, m_pending.front().at); m_frozen = m_frozen && !m_pending.front().unfreeze; m_pending.erase(m_pending.begin()); } m_now = std::max(m_now, time); + m_state.time = m_now; +} + +void OverworldSim::show(SimScreen screen, Milliseconds at){ + m_state.screen = screen; + m_state.shown.push_back(SimShown{at, screen}); } @@ -453,7 +504,7 @@ SimBattleEnd OverworldSim::handle_battle(SimBattleChoice choice){ return SimBattleEnd::CANT_RUN; } advance_to(m_now + BATTLE_TIME); - m_state.screen = SimScreen::OVERWORLD; + show(SimScreen::OVERWORLD, m_now); m_frozen = false; m_battle_pending = false; if (m_trainer){ @@ -471,7 +522,7 @@ bool OverworldSim::close_message(){ return false; } advance_to(m_now + MESSAGE_CLOSE); - m_state.screen = SimScreen::OVERWORLD; + show(SimScreen::OVERWORLD, m_now); if (!m_trainer_speaking){ m_frozen = false; return false; @@ -656,6 +707,7 @@ std::optional OverworldSimWalker::walk_step(const RouteStep& step, calls.name = [&](size_t index){ return ints[index].name; }; calls.log = [&](const std::string& message){ m_log.push_back("Leg: " + message); }; calls.fail = [&](const std::string& message){ fail(message); }; + m_leg_start = m_sim.state().time; const Pokemon::LegResult result = Pokemon::walk_leg_with(leg, calls, MAX_INTERRUPTIONS_PER_LEG); if (result.stopped){ return walked_near(step, result.tiles, TILE_TIME); @@ -694,7 +746,24 @@ StepOutcome OverworldSimWalker::walk(const RouteStep& step, std::optional OverworldSimWalker::late_stop(const RouteStep& step){ + if (step.to_wall || !m_rules.place_late_battle){ + return std::nullopt; + } + const WallClock start = sim_wall_clock(m_leg_start); + const LateStop stop = place_late_stop( + step, TILE_TIME, screen_event_log(m_sim.state(), m_leg_start), start, sim_wall_clock(m_sim.state().time) + ); + m_log.push_back(late_stop_line(step, stop, start)); + return stop.walked; } // Whether the screen shows a battle on its way: its wipe's black, or its diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h index 3fb3cb5c4e..73cfe34e54 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h @@ -37,6 +37,7 @@ #include #include "Common/Cpp/Time.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" #include "PokemonFRLG_OverworldRecovery.h" #include "PokemonFRLG_OverworldRoute.h" #include "PokemonFRLG_OverworldWalk.h" @@ -101,6 +102,7 @@ class OverworldSim{ }; void advance_to(Milliseconds time); + void show(SimScreen screen, Milliseconds at); void schedule(Milliseconds at, SimScreen screen, bool unfreeze); bool sees(SimWatcher watcher, SimScreen screen) const; std::optional first_seen(SimWatcher watcher, Milliseconds from, Milliseconds to) const; @@ -193,6 +195,11 @@ struct SimWalkerRules{ // Cerulean City onto Nugget Bridge ended a tile short (console B, // 2026-11-03, rows 00.16 and 00.20). bool hold_over_connections = true; + // An open leg after whose hold a wild battle was seen is placed by the + // battle's first timed screen event (place_late_stop()). Without it the + // player may be anywhere on the leg: on Route 10, "east 11" was found + // again from all 12 of its tiles (console B, 2026-11-09, 00.27 run 2). + bool place_late_battle = true; }; // The console side of run_route() on an OverworldSim, as @@ -230,6 +237,7 @@ class OverworldSimWalker{ bool battle; }; StepOutcome walk(const RouteStep& step, std::optional facing); + std::optional late_stop(const RouteStep& step); std::optional walk_step(const RouteStep& step, std::optional facing); bool after_warp(const RouteStep& step, int warps_before); [[noreturn]] void fail_off_route(const RouteStep& step); @@ -251,6 +259,8 @@ class OverworldSimWalker{ std::vector m_wild; std::vector m_other; std::vector m_log; + // When the last WALK leg's first hold started, on the sim's clock. + Milliseconds m_leg_start{0}; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimScreens.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimScreens.h index 18d8d480aa..ebed049840 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimScreens.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimScreens.h @@ -73,6 +73,12 @@ struct SimTiming{ Milliseconds trainer_battle{2000}; // after the trainer's message closes }; +// A screen the sim showed, and when, from its start. +struct SimShown{ + Milliseconds at{0}; + SimScreen screen = SimScreen::OVERWORLD; +}; + } // namespace PokemonFRLG diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimState.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimState.h index cf254da5e7..c8fea0bd6b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimState.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimState.h @@ -16,6 +16,7 @@ #include #include #include "Common/Cpp/Time.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" #include "PokemonFRLG_OverworldRoute.h" #include "PokemonFRLG_OverworldSimScreens.h" @@ -61,8 +62,23 @@ struct SimState{ // there. std::vector violations; std::vector log; + // The sim's clock, from its start. + Milliseconds time{0}; + // Each screen as it showed, oldest first, from `screen` at the start. + std::vector shown; }; +// A time on the sim's clock as a WallClock, for the shared judges. +WallClock sim_wall_clock(Milliseconds time); + +// The screens `state` showed from `from` until its time, as the walker's +// screen observer (FrlgScreenObserver) reports them: a message's box, a +// battle's text box with or without its HP boxes, and the black of a +// battle's wipe or a warp's fade. A battle's flashes are no uniform +// screen, and black below a small room, which the observer's battle text +// box detector may take for a box, isn't modelled. +ScreenEventLog screen_event_log(const SimState& state, Milliseconds from); + // A watcher that fired: which (an index into the watchers asked for), and // how long after the hold or watch started. struct SimSeen{ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim_Tests.cpp index 9a1ecbf4f8..917031fc11 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim_Tests.cpp @@ -21,6 +21,7 @@ #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonFRLG_OverworldLateBattle.h" #include "PokemonFRLG_OverworldMaps.h" #include "PokemonFRLG_OverworldSim.h" @@ -981,6 +982,7 @@ void add_tests_OverworldSim(UnitTestDatabase& database){ for (const auto& item : OVERWORLD_SIM_CASES){ database.add(item); } + add_tests_OverworldLateBattle(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h index 2c74414a43..a0f6f01020 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h @@ -17,11 +17,13 @@ #ifndef PokemonAutomation_PokemonFRLG_OverworldWalk_H #define PokemonAutomation_PokemonFRLG_OverworldWalk_H +#include #include #include #include #include #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" #include "Pokemon/Navigation/Pokemon_LegWalker.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalkOptions.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalkOptions.h index 8473f74c0e..4b2b112e3f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalkOptions.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalkOptions.h @@ -10,11 +10,13 @@ #ifndef PokemonAutomation_PokemonFRLG_OverworldWalkOptions_H #define PokemonAutomation_PokemonFRLG_OverworldWalkOptions_H +#include #include #include #include #include #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" #include "Pokemon/Navigation/Pokemon_LegWalker.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" @@ -39,6 +41,11 @@ using OverworldInterrupts = Pokemon::LegInterrupts()> make_observer = {}; }; // A wild battle, run from, and a message (e.g. "REPEL's effect wore off..."), diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index db97b4c44e..38b6dd563a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -53,9 +53,10 @@ #include "Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryRngLoop.h" +#include "Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryTable.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryTrashCans.h" -#include "Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.h" +#include "Programs/AutoStory/Data/Json_Tests.h" #include "Programs/RngManipulation/PokemonFRLG_GiftRngAttempts_Tests.h" #include "Programs/RngManipulation/PokemonFRLG_RngPlanInputs_Tests.h" #include "Programs/RngManipulation/PokemonFRLG_RngPlanLog_Tests.h" @@ -100,6 +101,7 @@ static void add_tests_RngPlanning(UnitTestDatabase& database){ static void add_tests_AutoStoryRows(UnitTestDatabase& database){ add_tests_AutoStoryVermilion(database); add_tests_AutoStoryVermilionGym(database); + add_tests_AutoStoryCeladon(database); } void add_tests(UnitTestDatabase& database){ @@ -141,7 +143,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_OverworldSim(database); add_tests_PlayerFacingReader(database); add_tests_AutoStory(database); - add_tests_AutoStoryExport(database); + add_tests_AutoStoryJson(database); add_tests_AutoStorySimulation(database); add_tests_AutoStoryRows(database); add_tests_AutoStoryTrashCans(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/JsonWriter.cpp similarity index 76% rename from SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.cpp rename to SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/JsonWriter.cpp index 35619d684c..bed5307c20 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/JsonWriter.cpp @@ -1,25 +1,22 @@ -/* FRLG Authored Checkpoint Export +/* Legacy FRLG Checkpoint JSON Writer * From: https://github.com/PokemonAutomation/ */ + #include #include -#include "Common/Cpp/Exceptions.h" #include "Common/Cpp/Json/JsonArray.h" #include "Common/Cpp/Json/JsonObject.h" #include "Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h" -#include "PokemonFRLG_AutoStoryExport.h" +#include "LegacyJsonFormat.h" +#include "JsonWriter.h" + namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonFRLG{ + + namespace{ -std::string_view menu_name(StartMenu menu){ - switch (menu){ - case StartMenu::BEFORE_POKEMON: return "before_pokemon"; - case StartMenu::BEFORE_POKEDEX: return "before_pokedex"; - case StartMenu::FULL: return "full"; - } - throw ParseException("Unsupported FRLG START menu."); -} + JsonArray strings(const std::vector& values){ JsonArray result; for (const std::string& value : values){ @@ -27,12 +24,14 @@ JsonArray strings(const std::vector& values){ } return result; } + int64_t count(size_t value){ if (value > (size_t)std::numeric_limits::max()){ throw ParseException("FRLG checkpoint count exceeds JSON integer range."); } return (int64_t)value; } + JsonObject stats_json(const Stats& stats){ JsonObject result; result["hp"] = stats.hp; @@ -43,6 +42,7 @@ JsonObject stats_json(const Stats& stats){ result["speed"] = stats.speed; return result; } + JsonObject member_json(const PartyMember& member){ JsonObject result; result["species"] = member.species; @@ -60,6 +60,7 @@ JsonObject member_json(const PartyMember& member){ result["stats"] = member.stats ? JsonValue(stats_json(*member.stats)) : JsonValue(); return result; } + JsonObject supplies_json(const Supplies& supplies){ JsonObject result; result["potions"] = supplies.potions; @@ -83,20 +84,25 @@ JsonObject supplies_json(const Supplies& supplies){ result["wild_foes"] = strings(supplies.wild_foes); return result; } + + } // namespace + JsonValue story_checkpoint_export(const StoryCheckpoint& checkpoint){ JsonObject result; - result["format"] = "pokesim-native-checkpoint-export/1"; - result["game"] = "frlg"; - result["status"] = "native-table-export"; + result["format"] = std::string(StoryJsonInternal::FORMAT); + result["game"] = std::string(StoryJsonInternal::GAME); + result["status"] = std::string(StoryJsonInternal::STATUS); JsonObject native; - native["menu"] = std::string(menu_name(checkpoint.menu)); + native["menu"] = std::string(StoryJsonInternal::menu_name(checkpoint.menu)); native["supplies"] = supplies_json(checkpoint.supplies); result["native_fields"] = std::move(native); - result["state_basis"] = "native-table-bookkeeping"; - result["unbound"] = strings({"runtime_configuration", "map_pins", "executor_bindings", "entry_evidence", "effective_save_retry_policy"}); + result["state_basis"] = std::string(StoryJsonInternal::STATE_BASIS); + result["unbound"] = strings(StoryJsonInternal::unbound_fields()); return result; } + + } // namespace PokemonFRLG } // namespace NintendoSwitch } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/JsonWriter.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/JsonWriter.h new file mode 100644 index 0000000000..0fa8d2d229 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/JsonWriter.h @@ -0,0 +1,26 @@ +/* Legacy FRLG Checkpoint JSON Writer + * From: https://github.com/PokemonAutomation/ + */ + +#ifndef PokemonAutomation_PokemonFRLG_CheckpointJsonWriter_H +#define PokemonAutomation_PokemonFRLG_CheckpointJsonWriter_H + +#include "Common/Cpp/Json/JsonValue.h" +#include "VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// Migration diagnostics only: capture shared fields and FRLG bookkeeping. +// This is not the authored production format or observed entry requirements. +// Runtime configuration, map pins, policies and executor bindings remain unbound. +JsonValue story_checkpoint_export(const StoryCheckpoint& checkpoint); + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation + +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/Json_Tests.cpp similarity index 91% rename from SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.cpp rename to SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/Json_Tests.cpp index 41113be2bf..327158c5ba 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/Json_Tests.cpp @@ -1,27 +1,35 @@ /* FRLG Checkpoint Export Tests * From: https://github.com/PokemonAutomation/ */ + +#include "Common/Cpp/Exceptions.h" #include "Common/Cpp/Json/JsonArray.h" #include "Common/Cpp/Json/JsonObject.h" -#include "PokemonFRLG_AutoStoryExport.h" -#include "PokemonFRLG_AutoStoryTable.h" -#include "PokemonFRLG_AutoStoryExport_Tests.h" +#include "JsonWriter.h" +#include "VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.h" +#include "Json_Tests.h" + namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonFRLG{ + namespace{ + struct ExportInput{ std::string_view id; bool custom = false; }; + struct ExportExpectation{ std::string_view menu; std::string_view native_json = {}; }; + struct ExportCase{ ExportInput input; ExportExpectation expectation; }; + const ExportCase CASES[] = { {.input = {.id = "00.05"}, .expectation = {.menu = "before_pokedex"}}, {.input = {.id = "00.05", .custom = true}, .expectation = { @@ -29,6 +37,7 @@ const ExportCase CASES[] = { .native_json = R"({"menu":"full","supplies":{"potions":2,"antidotes":3,"parlyz_heals":4,"pocket":["antidote",""],"status":"PSN","hurt":true,"hp_red":true,"hp_fill":0.25,"lead":{"species":"wartortle","moves":[{"move":"bite","pp":0}],"moves_through_level":18,"moves_read":true,"level":19,"stats":{"hp":50,"attack":20,"defense":30,"sp_attack":40,"sp_defense":35,"speed":25}},"protected_slots":[0,2],"foes":["staryu"],"trainer":"misty","risk":"high","foe_out":"staryu","wild_foes":["pidgey"]}})" }}, }; + void customize(StoryCheckpoint& row){ row.menu = StartMenu::FULL; Supplies& state = row.supplies; @@ -50,6 +59,7 @@ void customize(StoryCheckpoint& row){ state.foe_out = "staryu"; state.wild_foes = {"pidgey"}; } + std::string check_export(StoryCheckpoint row, const ExportCase& test){ if (test.input.custom){ customize(row); @@ -65,6 +75,7 @@ std::string check_export(StoryCheckpoint row, const ExportCase& test){ } return ""; } + class ExportTest : public UnitTest{ public: explicit ExportTest(ExportCase test) @@ -81,12 +92,16 @@ class ExportTest : public UnitTest{ private: ExportCase m_test; }; + + } // namespace -void add_tests_AutoStoryExport(UnitTestDatabase& database){ + +void add_tests_AutoStoryJson(UnitTestDatabase& database){ for (const ExportCase& test : CASES){ database.add(test); } } + } // namespace PokemonFRLG } // namespace NintendoSwitch } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/Json_Tests.h similarity index 66% rename from SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.h rename to SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/Json_Tests.h index 90eabc74a9..b43a86f3d1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/Json_Tests.h @@ -1,14 +1,18 @@ /* FRLG Checkpoint Export Tests * From: https://github.com/PokemonAutomation/ */ -#ifndef PokemonAutomation_PokemonFRLG_AutoStoryExport_Tests_H -#define PokemonAutomation_PokemonFRLG_AutoStoryExport_Tests_H + +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryJson_Tests_H +#define PokemonAutomation_PokemonFRLG_AutoStoryJson_Tests_H + #include "Common/Cpp/TestRunners/UnitTestDatabase.h" + namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonFRLG{ + // Register evaluated-table and non-default native-extension preservation tests. -void add_tests_AutoStoryExport(UnitTestDatabase& database); +void add_tests_AutoStoryJson(UnitTestDatabase& database); } // namespace PokemonFRLG } // namespace NintendoSwitch } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/LegacyJsonFormat.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/LegacyJsonFormat.h new file mode 100644 index 0000000000..148012d1db --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/Data/LegacyJsonFormat.h @@ -0,0 +1,53 @@ +/* Legacy FRLG Checkpoint Export Format + * From: https://github.com/PokemonAutomation/ + */ + +#ifndef PokemonAutomation_PokemonFRLG_LegacyJsonFormat_H +#define PokemonAutomation_PokemonFRLG_LegacyJsonFormat_H + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace StoryJsonInternal{ + +// The envelope constants the export writes and the reader requires. +constexpr std::string_view FORMAT = "pokesim-native-checkpoint-export/1"; +// Game identifier in the legacy envelope. +constexpr std::string_view GAME = "frlg"; +// Marks a captured table row rather than an authored plan. +constexpr std::string_view STATUS = "native-table-export"; +// Describes the native bookkeeping snapshot, not observed game state. +constexpr std::string_view STATE_BASIS = "native-table-bookkeeping"; +// Fields deliberately left outside the captured native table state. +inline const std::vector& unbound_fields(){ + static const std::vector UNBOUND{ + "runtime_configuration", "map_pins", "executor_bindings", "entry_evidence", "effective_save_retry_policy", + }; + return UNBOUND; +} + +// Stable legacy spellings of the native START menu variants. +inline std::string_view menu_name(StartMenu menu){ + switch (menu){ + case StartMenu::BEFORE_POKEMON: return "before_pokemon"; + case StartMenu::BEFORE_POKEDEX: return "before_pokedex"; + case StartMenu::FULL: return "full"; + } + throw ParseException("Unsupported FRLG START menu."); +} + +} // namespace StoryJsonInternal + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation + +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCeladon_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCeladon_Tests.cpp new file mode 100644 index 0000000000..d82280fc15 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCeladon_Tests.cpp @@ -0,0 +1,457 @@ +/* AutoStory Celadon Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Rows from 00.26 on (StoryData/frlg-00.26.plan.json and after) on the + * overworld sim: 00.26 from where 00.25 ends, to the Fan Club's chairman, + * north past Route 6's trainers, through the tree into Cerulean, to the + * Bike Shop's clerk, the Mart and the nurse, with Oak's aide on + * Vermilion's (25, 7); and what its Mart step buys. 00.27 from where + * 00.26 ends, east over Route 9 to Route 10's Pokemon Center. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMap.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h" +#include "PokemonFRLG_AutoStoryEvents.h" +#include "PokemonFRLG_AutoStoryMart.h" +#include "PokemonFRLG_AutoStoryRowSim.h" +#include "PokemonFRLG_AutoStoryRows_Tests.h" +#include "PokemonFRLG_AutoStoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +const std::string ROUTE6 = "Route6"; +const std::string VERMILION = "VermilionCity"; +const std::string BIKE_SHOP = "CeruleanCity_BikeShop"; +const std::string CERULEAN_CENTER = "CeruleanCity_PokemonCenter_1F"; +const std::string CERULEAN_MART = "CeruleanCity_Mart"; +const std::string ROUTE10_CENTER = "Route10_PokemonCenter_1F"; +const std::string ROUTE9 = "Route9"; + +struct AutoStoryCeladonInput{ + std::string name; + std::function body; +}; + +struct AutoStoryCeladonExpectation{ +}; + +struct AutoStoryCeladonCase{ + AutoStoryCeladonInput input; + AutoStoryCeladonExpectation expectation; +}; + +class Test_AutoStoryCeladon : public UnitTest{ +public: + explicit Test_AutoStoryCeladon(AutoStoryCeladonCase test_case) + : UnitTest("PokemonFRLG::AutoStory - Simulation: " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + try{ + return m_case.input.body(); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + AutoStoryCeladonCase m_case; +}; + +size_t row_index(std::string_view id){ + const std::vector& rows = story_checkpoints(); + const auto row = std::find_if(rows.begin(), rows.end(), [&](const StoryCheckpoint& data){ return data.id == id; }); + if (row == rows.end()){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("No checkpoint {}.", id)); + } + return row - rows.begin(); +} + +const StoryCheckpoint& checkpoint(std::string_view id){ + return story_checkpoints()[row_index(id)]; +} + +MapData& map_named(std::vector& maps, const std::string& name){ + for (MapData& map : maps){ + if (map.name == name){ + return map; + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "No map " + name + "."); +} + +// The maps as 00.25 leaves them: story_maps() with Oak's aide on +// Vermilion's (25, 7), shown by Lt. Surge's win, and Route 6's Jeff +// beaten (00.21). Isabelle and Elijah are not. +std::vector maps_after_surge(){ + const std::vector all = overworld_maps(); + std::set flags = map_flag_set(all, "after_hall_of_fame"); + flags.merge(map_flag_set(all, "new_game")); + flags.erase("FLAG_HIDE_VERMILION_CITY_OAKS_AIDE"); + std::vector maps = overworld_maps(flags); + for (MapObject& object : map_named(maps, ROUTE6).objects){ + if (object.name == "camper" && object.trainer){ + object.trainer.reset(); + } + } + return maps; +} + +// Whoever stands `tiles` ahead of `pose`, or nothing. +const MapObject* object_ahead(const std::vector& maps, const StoryPose& pose, int tiles){ + TilePos tile = pose.tile; + for (int c = 0; c < tiles; c++){ + tile = tile.next(*pose.facing); + } + for (const MapData& map : maps){ + if (map.name == pose.map){ + return object_at(map, tile); + } + } + return nullptr; +} + +// The row's last route's target. +std::optional last_target(const StoryCheckpoint& row){ + for (auto step = row.steps.rbegin(); step != row.steps.rend(); ++step){ + if (step->kind == StoryStepKind::ROUTE && step->args.contains("target")){ + return story_pose(Pokemon::pose_of(Pokemon::story_arg(step->args, "target"))); + } + } + return std::nullopt; +} + + +// Row `id` comes right after row `previous` and starts where its last +// walk ends. +UnitTestResult starts_where_ends(const std::string& previous, const std::string& id){ + if (row_index(id) != row_index(previous) + 1){ + return "Expected " + id + " right after " + previous + "."; + } + const std::optional end = last_target(checkpoint(previous)); + const StoryCheckpoint& row = checkpoint(id); + const std::optional start = row.start ? story_pose(Pokemon::pose_of(*row.start)) : std::nullopt; + if (!end || !start || start->map != end->map || !(start->tile == end->tile) || start->facing != end->facing){ + return "Expected " + id + " to start where " + previous + "'s last walk ends."; + } + return true; +} + +// 00.26 starts at Vermilion's nurse, where 00.25 ends. +UnitTestResult test_starts_where_lt_surge_ends(){ + return starts_where_ends("00.25", "00.26"); +} + +// 00.27 starts at Cerulean's nurse, where 00.26 ends. +UnitTestResult test_starts_where_bicycle_ends(){ + return starts_where_ends("00.26", "00.27"); +} + +// 00.26 on the sim from Vermilion's nurse to Cerulean's, the maps as +// 00.25 leaves them: no trainer meets the player (Isabelle and Elijah +// stay unfought), no violation, each talk faces the one it's for (the +// chairman beside the player, the Bike Shop's clerk across the counter), +// and the walk ends at Cerulean's nurse. +UnitTestResult test_bike_voucher_row(){ + const StoryCheckpoint& row = checkpoint("00.26"); + const std::vector maps = maps_after_surge(); + const MapData& city = *std::find_if(maps.begin(), maps.end(), [](const MapData& map){ return map.name == VERMILION; }); + if (object_at(city, {25, 7}) == nullptr){ + return "Expected Oak's aide on (25, 7) after Lt. Surge."; + } + StoryPose at = *story_pose(Pokemon::pose_of(*row.start)); + OverworldSim sim(maps, MapPosition{at.map, at.tile}, *at.facing); + std::vector talked_to; + for (const StoryStep& step : row.steps){ + if (step.kind == StoryStepKind::TALK){ + const StoryPose here{sim.state().position.map, sim.state().position.point, sim.state().facing}; + const bool counter = here.map == BIKE_SHOP; + const MapObject* person = object_ahead(maps, here, counter ? 2 : 1); + if (person == nullptr || (counter && object_ahead(maps, here, 1) != nullptr)){ + return std::format("00.26/{}: no one to talk to from {}.", step.name, to_string(sim.state().position)); + } + talked_to.push_back(person->name); + } + if (const std::string failure = row_step_on_sim(sim, at, step, row.id); !failure.empty()){ + return failure; + } + // A walk the sim's player didn't finish, stopped by someone the + // maps the program plans on don't have. + if (!(sim.state().position == MapPosition{at.map, at.tile})){ + return std::format("00.26/{}: expected {}, on {}.", step.name, to_string(MapPosition{at.map, at.tile}), to_string(sim.state().position)); + } + } + if (talked_to != std::vector{"gentleman", "man"}){ + return "00.26: expected to talk to the chairman, then the Bike Shop's clerk."; + } + if (!sim.state().trainers_beaten.empty() || !sim.state().violations.empty()){ + return "00.26: a battle or a violation on the way" + (sim.state().violations.empty() ? std::string(".") : ": " + sim.state().violations.back()); + } + const MapPosition nurse{CERULEAN_CENTER, {7, 4}}; + if (!(sim.state().position == nurse)){ + return "00.26: ended on " + to_string(sim.state().position) + ", not Cerulean's nurse."; + } + return true; +} + +// 00.26's Mart step, at its money floor: 5 Repels and 2 Escape Ropes, +// and 3000 or more left. +UnitTestResult test_repels_and_escape_ropes(){ + const StoryCheckpoint& row = checkpoint("00.26"); + const auto step = std::find_if(row.steps.begin(), row.steps.end(), [](const StoryStep& data){ + return data.action == MART_ACTION; + }); + if (step == row.steps.end()){ + return "00.26: expected a Mart step."; + } + const std::optional parsed = mart_plan_of(step->args); + if (!parsed){ + return "00.26: expected a Mart plan."; + } + const MartPlan& plan = *parsed; + if (plan.map != CERULEAN_MART || !plan.money_floor){ + return "00.26: expected the Mart step at Cerulean's Mart, with a money floor."; + } + const std::optional> buys = plan_mart_buys(mart_stock(plan.map), plan, *plan.money_floor); + if (!buys){ + return "00.26: the Mart step names an item Cerulean's Mart doesn't sell."; + } + size_t repels = 0; + size_t escape_ropes = 0; + size_t spent = 0; + for (const MartBuy& buy : *buys){ + repels += buy.item == "repel" ? buy.quantity : 0; + escape_ropes += buy.item == "escape-rope" ? buy.quantity : 0; + spent += buy.cost; + } + if (repels < 5 || escape_ropes != 2 || *plan.money_floor < spent + 3000){ + return std::format("00.26: bought {} Repels and {} Escape Ropes for {} from {}.", repels, escape_ropes, spent, *plan.money_floor); + } + return true; +} + +// 00.27 on the sim from Cerulean's nurse to Route 10's, the maps as 00.26 +// leaves them, both trees grown back: the trees cut, Alicia, Jeremy and +// Conner fought where the table steps into their lines and no one else +// met (Brice, Caitlin and the rest unfought), no violation, each step +// ending where the table says, and the walk ending at Route 10's nurse. +UnitTestResult test_route10_row(){ + const StoryCheckpoint& row = checkpoint("00.27"); + const std::vector maps = maps_after_surge(); + StoryPose at = *story_pose(Pokemon::pose_of(*row.start)); + OverworldSim sim(maps, MapPosition{at.map, at.tile}, *at.facing); + for (const StoryStep& step : row.steps){ + if (const std::string failure = row_step_on_sim(sim, at, step, row.id); !failure.empty()){ + return failure; + } + // A step into a trainer's line ends where the battle after it says. + const bool into_line = step.kind == StoryStepKind::STEP && !step.action.empty(); + if (!into_line && !(sim.state().position == MapPosition{at.map, at.tile})){ + return std::format("00.27/{}: expected {}, on {}.", step.name, to_string(MapPosition{at.map, at.tile}), to_string(sim.state().position)); + } + } + if (sim.state().trainers_beaten != std::vector{"picnicker", "hiker", "bug_catcher"}){ + return "00.27: expected to fight Alicia, Jeremy and Conner, no one else; fought " + std::to_string(sim.state().trainers_beaten.size()) + "."; + } + if (!sim.state().violations.empty()){ + return "00.27: a violation on the way: " + sim.state().violations.back(); + } + const MapPosition nurse{ROUTE10_CENTER, {7, 4}}; + if (!(sim.state().position == nurse)){ + return "00.27: ended on " + to_string(sim.state().position) + ", not Route 10's nurse."; + } + return true; +} + +// The next row starts from 00.27's save in Route 10's Center, whose save +// card says ROUTE 10: a place of its own, not Route 1's. +UnitTestResult test_route10_save_place(){ + if (save_card_place(ROUTE10_CENTER) != "route-10" || save_card_place("Route1") == "route-10"){ + return "Wrong save card place for Route 10's Pokemon Center."; + } + return true; +} + +// 00.27's walk from Route 9 (70, 8) to Route 10's nurse: its steps from +// route10_ledge_end to route10_nurse. Console B, 2026-11-09, the walk +// then planned in one ended its open legs a tile from a blocker, and both +// runs ended "no fade in and out" at the Center's door (13, 20): +// - run 3: the crossing onto Route 10 wasn't held up, so the player +// stood a tile further east, row 11 ended on (17, 11), the leg down +// column 17 met the fence post (17, 12), and the walk ended in the +// grass on (14, 10). +// - run 2: the leg down column 16 ended a tile short, on (16, 20), west +// 3 met the Center's east wall (15, 20) and north 1 ended on (16, 19). +std::vector route10_walk(){ + const StoryCheckpoint& row = checkpoint("00.27"); + const auto named = [&row](std::string_view name){ + return std::find_if(row.steps.begin(), row.steps.end(), [&](const StoryStep& data){ return data.name == name; }); + }; + const auto first = named("route10_ledge_end"); + const auto last = named("route10_nurse"); + if (first == row.steps.end() || last == row.steps.end() || last < first){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "00.27: no steps route10_ledge_end to route10_nurse."); + } + return std::vector(first, last + 1); +} + +const StoryPose ROUTE10_WALK_START{ROUTE9, {70, 8}, Direction::EAST}; + +// `steps` from `first` on, on `sim` from `at`, where the table says the +// player stands: the failure, or empty once at Route 10's nurse. +std::string route10_walk_rest(OverworldSim& sim, StoryPose at, const std::vector& steps, size_t first){ + std::string failure; + for (size_t c = first; c < steps.size() && failure.empty(); c++){ + failure = row_step_on_sim(sim, at, steps[c], "00.27"); + } + const MapPosition nurse{ROUTE10_CENTER, {7, 4}}; + if (!failure.empty() || !(sim.state().position == nurse)){ + return std::format("ended on {}, not Route 10's nurse; {}", to_string(sim.state().position), failure); + } + return ""; +} + +std::string route10_walk_on_sim(const SimScript& script){ + OverworldSim sim(maps_after_surge(), MapPosition{ROUTE10_WALK_START.map, ROUTE10_WALK_START.tile}, Direction::EAST, script); + return route10_walk_rest(sim, ROUTE10_WALK_START, route10_walk(), 0); +} + +UnitTestResult test_route10_crossing_not_held_up(){ + SimScript script; + script.connections_stall = false; + if (const std::string failure = route10_walk_on_sim(script); !failure.empty()){ + return "The crossing onto Route 10 not held up (run 3): " + failure; + } + return true; +} + +UnitTestResult test_route10_column_16_short(){ + SimScript script; + // Holds: east 7, south 3 and east 12 onto row 11; west 1; south 12 + // down column 16. + script.short_holds[5] = 1; + if (const std::string failure = route10_walk_on_sim(script); !failure.empty()){ + return "The leg down column 16 a tile short (run 2): " + failure; + } + return true; +} + +// Where an open leg of two tiles or more ends a tile short or long, the +// next leg starts a tile off. From each such start (on an open tile), the +// rest of the walk still reaches the nurse. A leg to a wall ends on its +// tile, and a one-tile leg is one step (as grass_held() takes it), so the +// leg after either starts where planned. The one exception is the door's +// column: the three east from the wall (9, 21) set it, and nothing after +// them can. +UnitTestResult test_route10_tile_off_after_open_legs(){ + const std::vector steps = route10_walk(); + const std::vector& maps = story_maps(); + // above_caitlin_line ends east 6 on (70, 8), an open leg. + RouteStep before{.direction = Direction::EAST, .tiles = 6}; + StoryPose at = ROUTE10_WALK_START; + for (size_t s = 0; s < steps.size(); s++){ + const std::vector route = walked_route(at, steps[s]).value_or(std::vector{}); + MapPosition from{at.map, at.tile}; + for (size_t c = 0; c < route.size(); c++){ + const bool door_column = steps[s].name == "route10_nurse" && c == 0; + const bool off = before.action == RouteAction::WALK && !before.to_wall && !before.warps && before.tiles > 1 && !door_column; + const TilePos on = from.point.next(before.direction); + const TilePos short_of{2 * from.point.x - on.x, 2 * from.point.y - on.y}; + const auto map = std::find_if(maps.begin(), maps.end(), [&](const MapData& data){ return data.name == from.map; }); + for (const TilePos& start : off ? std::vector{short_of, on} : std::vector{}){ + const bool open = map != maps.end() && in_bounds(*map, start) && object_at(*map, start) == nullptr + && (tile_at(*map, start) == TileKind::OPEN || tile_at(*map, start) == TileKind::TALL_GRASS); + if (!open){ + continue; + } + OverworldSim sim(maps_after_surge(), MapPosition{from.map, start}, before.direction); + const std::vector rest(route.begin() + c, route.end()); + const RoutePlanning planning{.maps = &maps, .costs = story_route_costs(), .start = from, .label = steps[s].name}; + std::string failure = OverworldSimWalker(sim).run(rest, planning); + const StoryPose next{route.back().end.map, route.back().end.point, route.back().facing_after}; + failure = failure.empty() ? route10_walk_rest(sim, next, steps, s + 1) : failure; + if (!failure.empty()){ + return std::format("{} leg {} ({}) from {}, not {}: {}", steps[s].name, c + 1, to_string(route[c]), to_string(start), to_string(from.point), failure); + } + } + before = route[c]; + from = route[c].end; + } + at = route.empty() ? at : StoryPose{route.back().end.map, route.back().end.point, route.back().facing_after}; + } + return true; +} + +} // namespace + + +const AutoStoryCeladonCase AUTO_STORY_CELADON_CASES[] = { + { + .input = {"00.26 starts where 00.25 ends", test_starts_where_lt_surge_ends}, + .expectation = {}, + }, + { + .input = {"00.26 the Fan Club, Route 6 and the Underground Path north, the Bike Shop, past Oak's aide", test_bike_voucher_row}, + .expectation = {}, + }, + { + .input = {"00.26 Repels and Escape Ropes, 3000 left", test_repels_and_escape_ropes}, + .expectation = {}, + }, + { + .input = {"00.27 starts where 00.26 ends", test_starts_where_bicycle_ends}, + .expectation = {}, + }, + { + .input = {"00.27 the two trees, Route 9's Alicia, Jeremy and Conner, to Route 10's Pokemon Center", test_route10_row}, + .expectation = {}, + }, + { + .input = {"00.27 saves in Route 10's Pokemon Center, ROUTE 10 on the save card", test_route10_save_place}, + .expectation = {}, + }, + { + .input = {"00.27 route10_nurse: the crossing onto Route 10 not held up", test_route10_crossing_not_held_up}, + .expectation = {}, + }, + { + .input = {"00.27 route10_nurse: the leg down column 16 a tile short", test_route10_column_16_short}, + .expectation = {}, + }, + { + .input = {"00.27 route10_nurse: a tile off after each open leg", test_route10_tile_off_after_open_legs}, + .expectation = {}, + }, +}; + +void add_tests_AutoStoryCeladon(UnitTestDatabase& database){ + for (const auto& item : AUTO_STORY_CELADON_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCheckpoints.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCheckpoints.h index 032ae90229..42859607c1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCheckpoints.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCheckpoints.h @@ -106,11 +106,6 @@ std::optional> fixed_route( // into a sign would read it. std::optional wall_beside(const MapData& map, const TilePos& tile); -// `items` as orders at the Viridian Mart before the Boulder Badge: the -// rows of POKE BALL, POTION, ANTIDOTE, PARLYZ HEAL. Nothing if it does -// not sell one of them. -std::optional> viridian_mart_orders(const std::vector& items); - } // namespace PokemonFRLG diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index 9d5365ca01..87d3fee06d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -23,10 +23,12 @@ #include "PokemonFRLG_AutoStoryEvents.h" #include "PokemonFRLG_AutoStoryGrind.h" #include "PokemonFRLG_AutoStoryGameArgs.h" +#include "PokemonFRLG_AutoStoryScreenEvents.h" #include "PokemonFRLG_AutoStorySummary.h" #include "PokemonFRLG_AutoStoryTable.h" #include "PokemonFRLG_AutoStoryTeachScreens.h" #include "PokemonFRLG_AutoStoryTrashCans.h" +#include "PokemonFRLG_AutoStoryViridianMart.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -114,6 +116,17 @@ std::optional route_start(const Pokemon::Pose& pose){ return MapPosition{pose.map, tiles[0]}; } +// The story's interrupts for `step`'s walk, with FRLG's screen observer, +// which places a wild battle seen only after an open leg's hold +// (OverworldInterruptSet::make_observer). +OverworldInterruptSet walk_interrupts(StoryEnv& env, const StoryStep& step){ + OverworldInterruptSet out = story_interrupts(env.supplies, arg(step, "fight_wild"), env.checks, env.language); + out.make_observer = []() -> std::unique_ptr{ + return std::make_unique(); + }; + return out; +} + // `maps`: where the route is planned; the story's but where a step knows // better (the trash cans' walks, past trainers who have walked up). Outcome walk_route(StoryEnv& env, RunState& state, const StoryStep& step, const std::vector& maps = story_maps()){ @@ -144,7 +157,7 @@ Outcome walk_route(StoryEnv& env, RunState& state, const StoryStep& step, const if (!start){ env.console.log("Story: " + step.name + ": on one of " + Pokemon::to_string(state.pose) + "; holds to walls first, to one tile."); } - const OverworldInterruptSet interrupts = story_interrupts(env.supplies, arg(step, "fight_wild"), env.checks, env.language); + const OverworldInterruptSet interrupts = walk_interrupts(env, step); // Planning lets the walker recover a stopped open leg: it plans back to // the leg's end, then walks the rest as given. From a set of tiles // the start isn't known: the walker reads the facing. @@ -169,7 +182,7 @@ Outcome walk_legs(StoryEnv& env, RunState& state, const StoryStep& step){ if (!facing || !route || route->empty()){ return Pokemon::outcome_failed("bad_legs", to_string(start)); } - const OverworldInterruptSet interrupts = story_interrupts(env.supplies, arg(step, "fight_wild"), env.checks, env.language); + const OverworldInterruptSet interrupts = walk_interrupts(env, step); // Planning lets the walker recover a stopped open leg: it plans back to // the leg's end, then walks the rest as given. const RoutePlanning planning{.maps = &story_maps(), .costs = story_route_costs(), .start = start, .label = step.name, .wall_check = wall_check(env)}; @@ -253,75 +266,9 @@ Outcome get_ready(StoryEnv& env, RunState& state, const StoryStep& step){ return walked; } -// Viridian Mart before the Boulder Badge (pokefirered ViridianCity_Mart_Items): -// POKE BALL, POTION (300), ANTIDOTE (100), PARLYZ HEAL (200). Each item -// the story buys: its row, and where the bag's count is kept. -struct MartItem{ - std::string_view item; - int row; - int Supplies::* count; - // The name the item list reader reads (read_item_list_name()). - std::string_view slug; -}; -constexpr MartItem VIRIDIAN_MART_ITEMS[] = { - {.item = "potion", .row = 1, .count = &Supplies::potions, .slug = "potion"}, - {.item = "antidote", .row = 2, .count = &Supplies::antidotes, .slug = "antidote"}, - {.item = "parlyz_heal", .row = 3, .count = &Supplies::parlyz_heals, .slug = "parlyz-heal"}, -}; - -const MartItem* viridian_mart_item(const std::string& name){ - for (const MartItem& item : VIRIDIAN_MART_ITEMS){ - if (name == item.item){ - return &item; - } - } - return nullptr; -} - -Outcome shop(StoryEnv& env, const StoryStep& step){ - const std::optional> orders = viridian_mart_orders(arg>(step, "items")); - if (!orders){ - return Pokemon::outcome_failed("unknown_item", step.name); - } - // 2000 of the 3000 a new game starts with, plus the rival's prize. - buy_items(env.console, env.context, *orders, env.checks, env.language); - for (const Pokemon::StoryItem& item : arg>(step, "items")){ - env.supplies.*viridian_mart_item(item.item)->count += (int)item.quantity; - } - if (!checks_screen(env.checks)){ - return Pokemon::outcome_ok(); - } - // The counts from the bag, not from the orders: a wrong row once bought - // Parlyz Heals for Antidotes unnoticed (console B, 2026-11-01). - const std::vector rows = read_items_pocket(env.console, env.context, env.language); - if (rows.empty()){ - env.console.log("Story: couldn't read the Items pocket; keeping the counts ordered.", COLOR_ORANGE); - return Pokemon::outcome_ok(); - } - for (const std::string& item : set_from_bag(env.supplies, rows)){ - env.console.log(std::format( - "Story: couldn't read how many {} the bag holds; counting {}.", - item, item_count(env.supplies, item) - ), COLOR_ORANGE); - } - return Pokemon::outcome_ok(); -} - } // namespace -std::optional> viridian_mart_orders(const std::vector& items){ - std::vector orders; - for (const Pokemon::StoryItem& wanted : items){ - const MartItem* found = viridian_mart_item(wanted.item); - if (found == nullptr){ - return std::nullopt; - } - orders.push_back(MartOrder{found->row, (int)wanted.quantity, found->slug}); - } - return orders; -} - const RouteCosts& story_route_costs(){ static const RouteCosts COSTS = []{ RouteCosts costs = default_route_costs(); @@ -371,6 +318,7 @@ std::string save_card_place(const std::string& map){ {"Route24", "route-24"}, {"Route25", "route-25"}, {"VermilionCity", "vermilion-city"}, + {"Route10", "route-10"}, }; for (const auto& [prefix, place] : PLACES){ if (map == prefix || map.rfind(prefix + "_", 0) == 0){ @@ -748,7 +696,7 @@ ConsoleAction console_action_of(StoryActionId id){ return Pokemon::outcome_ok(); }; case StoryActionId::SHOP: - return [](StoryEnv& env, RunState&, const StoryStep& step){ return shop(env, step); }; + return [](StoryEnv& env, RunState&, const StoryStep& step){ return viridian_shop(env, step); }; case StoryActionId::READY: return get_ready; default: diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.h deleted file mode 100644 index f2cc73c6b2..0000000000 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.h +++ /dev/null @@ -1,18 +0,0 @@ -/* FRLG Authored Checkpoint Export - * From: https://github.com/PokemonAutomation/ - */ -#ifndef PokemonAutomation_PokemonFRLG_AutoStoryExport_H -#define PokemonAutomation_PokemonFRLG_AutoStoryExport_H -#include "Common/Cpp/Json/JsonValue.h" -#include "PokemonFRLG_AutoStoryEvents.h" -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonFRLG{ -// Capture evaluated shared fields and every FRLG checkpoint extension. -// Native supplies are bookkeeping, not observed state or entry requirements. -// Runtime options, map pins, effective policies and execution remain unbound. -JsonValue story_checkpoint_export(const StoryCheckpoint& checkpoint); -} // namespace PokemonFRLG -} // namespace NintendoSwitch -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.cpp index a3d999939d..1043ce59bf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.cpp @@ -15,6 +15,7 @@ #include "Pokemon/AutoStory/Pokemon_TilePose.h" #include "PokemonFRLG_AutoStoryActions.h" #include "PokemonFRLG_AutoStoryRowSim.h" +#include "PokemonFRLG_AutoStoryViridianMart.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -394,9 +395,13 @@ std::string OverworldStepSim::walk(const StoryStep& step){ return failure; } -// A step one tap at a time, or for a step into a trainer's line by +// A hop() or step_onto() (a plain step the table says the end of); else +// a step one tap at a time, or for a step into a trainer's line by // step_until_seen(), each watched for the trainer. std::string OverworldStepSim::step_ahead(const StoryStep& step){ + if (step.action.empty() && step.pose_after){ + return step_onto(step); + } const Direction direction = Pokemon::story_arg(step.args, "direction"); const auto tap = [this, direction]{ if (m_sim.state().facing != direction){ @@ -411,6 +416,17 @@ std::string OverworldStepSim::step_ahead(const StoryStep& step){ return step_until_seen(tiles, tap, seen) ? "" : "no trainer saw the player."; } +// A hop() or step_onto(): one step, ending where the table says. +std::string OverworldStepSim::step_onto(const StoryStep& step){ + const Direction direction = Pokemon::story_arg(step.args, "direction"); + if (m_sim.state().facing != direction){ + m_sim.tap(direction); + } + m_sim.tap(direction); + m_at = *story_pose(Pokemon::pose_of(*step.pose_after)); + return m_sim.state().position == MapPosition{m_at.map, m_at.tile} ? "" : "expected to end on " + to_string(MapPosition{m_at.map, m_at.tile}) + "."; +} + // A CUT_TREE_VARIANT step: turned to the tree, cut, and a step onto its // tile. std::string OverworldStepSim::cut(const StoryStep& step){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.h index e3183f1ffa..37d165b79a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.h @@ -113,10 +113,12 @@ std::optional> walked_route(const StoryPose& at, const St // A row's steps on the OverworldSim as the program does them: routes and // legs by the sim's walker from where the table says the player stands, -// fighting a trainer who sees the player on the way; a step one tap at a -// time, or for a step into a trainer's line by step_until_seen(), each -// watched for the trainer; a battle from the trainer's message, which -// must be up; a cut tree cut and stepped onto. The rest is stubbed. +// fighting a trainer who sees the player on the way; a hop down a ledge +// (hop()) or a step onto the tile before one (step_onto()), to where the +// table says; a step one tap at a time, or for a step into a trainer's +// line by step_until_seen(), each watched for the trainer; a battle from +// the trainer's message, which must be up; a cut tree cut and stepped +// onto. The rest is stubbed. class OverworldStepSim : public Pokemon::TableExecutor{ public: // `sim` must outlive the executor. @@ -140,6 +142,7 @@ class OverworldStepSim : public Pokemon::TableExecutor{ Pokemon::StoryAction action(StoryActionId id); std::string walk(const StoryStep& step); std::string step_ahead(const StoryStep& step); + std::string step_onto(const StoryStep& step); std::string cut(const StoryStep& step); std::string battle(const StoryStep& step); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h new file mode 100644 index 0000000000..2d07d000b6 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h @@ -0,0 +1,31 @@ +/* AutoStory Row Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Registration of the tests that run story rows on the overworld sim, + * one file per stretch of the story. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryRows_Tests_H +#define PokemonAutomation_PokemonFRLG_AutoStoryRows_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// Rows 00.21 and 00.22 on the overworld sim (PokemonFRLG_AutoStoryVermilion_Tests.cpp). +void add_tests_AutoStoryVermilion(UnitTestDatabase& database); +// Rows 00.23 to 00.25 on the overworld sim (PokemonFRLG_AutoStoryVermilionGym_Tests.cpp). +void add_tests_AutoStoryVermilionGym(UnitTestDatabase& database); +// Rows from 00.26 on on the overworld sim (PokemonFRLG_AutoStoryCeladon_Tests.cpp). +void add_tests_AutoStoryCeladon(UnitTestDatabase& database); + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRunPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRunPlan_Tests.cpp index 9047c74ea3..c555d8690b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRunPlan_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRunPlan_Tests.cpp @@ -139,7 +139,7 @@ class Test_RunPlan : public UnitTest{ const RunPlanScenario m_scenario; }; -const std::string TODAY = "00.01 00.02 00.03 00.04 00.05 00.06 00.07 00.08 00.09 00.10 00.11 00.12 00.12c 00.13 00.14 00.14b 00.14c 00.15 00.15b 00.16 00.17 00.18 00.19 00.20 00.21 00.22 00.23 00.24 00.25"; +const std::string TODAY = "00.01 00.02 00.03 00.04 00.05 00.06 00.07 00.08 00.09 00.10 00.11 00.12 00.12c 00.13 00.14 00.14b 00.14c 00.15 00.15b 00.16 00.17 00.18 00.19 00.20 00.21 00.22 00.23 00.24 00.25 00.26 00.27"; const RunPlanScenario RUN_PLAN_CASES[] = { { @@ -230,11 +230,11 @@ const RunPlanScenario RUN_PLAN_CASES[] = { }, { .input = {.name = "Gift RNG off: 00.12b is passed over", .first = "00.11"}, - .expectation = {.expected_ids = "00.11 00.12 00.12c 00.13 00.14 00.14b 00.14c 00.15 00.15b 00.16 00.17 00.18 00.19 00.20 00.21 00.22 00.23 00.24 00.25"}, + .expectation = {.expected_ids = "00.11 00.12 00.12c 00.13 00.14 00.14b 00.14c 00.15 00.15b 00.16 00.17 00.18 00.19 00.20 00.21 00.22 00.23 00.24 00.25 00.26 00.27"}, }, { .input = {.name = "Gift RNG off: 00.12b runs when started at", .first = "00.12b"}, - .expectation = {.expected_ids = "00.12b 00.12c 00.13 00.14 00.14b 00.14c 00.15 00.15b 00.16 00.17 00.18 00.19 00.20 00.21 00.22 00.23 00.24 00.25"}, + .expectation = {.expected_ids = "00.12b 00.12c 00.13 00.14 00.14b 00.14c 00.15 00.15b 00.16 00.17 00.18 00.19 00.20 00.21 00.22 00.23 00.24 00.25 00.26 00.27"}, }, { .input = { .name = "Gift RNG: started at 00.12b, from the save beside the salesman", @@ -259,7 +259,7 @@ const RunPlanScenario RUN_PLAN_CASES[] = { }, { .input = {.name = "Starter RNG alone: 00.12b runs when started at", .first = "00.12b", .handoffs = {.starter_rng = true}}, - .expectation = {.expected_ids = "00.12b 00.12c 00.13 00.14 00.14b 00.14c 00.15 00.15b 00.16 00.17 00.18 00.19 00.20 00.21 00.22 00.23 00.24 00.25"}, + .expectation = {.expected_ids = "00.12b 00.12c 00.13 00.14 00.14b 00.14c 00.15 00.15b 00.16 00.17 00.18 00.19 00.20 00.21 00.22 00.23 00.24 00.25 00.26 00.27"}, }, { .input = { .name = "Starter RNG first, with both on", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySimulation_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySimulation_Tests.cpp index 012da57a88..62449a4277 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySimulation_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySimulation_Tests.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" @@ -303,18 +304,25 @@ UnitTestResult test_retry_from_save(){ } // console B, FRLG 00.27, 2026-11-09: the save stood below Cerulean Center's -// door, and the run walked as if from the counter where 00.27 starts. 00.09 -// from below Pewter Center's door (17, 25) stops before any step or save. +// door (22, 19), and the run walked as if from the counter where 00.27 +// starts. It, and 00.09 from below Pewter Center's door (17, 25), stop +// before any step or save. UnitTestResult test_save_off_start_pose(){ - Simulation sim; - sim.game.save_stands = StoryPose{"PewterCity", TilePos{17, 26}, Direction::SOUTH}; - const Pokemon::SegmentReport report = sim.run({.from = "00.09", .to = "00.09"}); - const auto stepped = [](const std::string& item){ return item.starts_with("00.09/"); }; - const auto told = [](const std::string& line){ return line.starts_with("start pose mismatch: "); }; - if (report.completed || report.failure.failure != "start_failed" || !sim.saves.empty() - || std::ranges::any_of(sim.game.trace, stepped) || std::ranges::none_of(sim.lines, told) - ){ - return "Expected 00.09 stopped at its start check, the mismatch logged:" + joined(sim.game.trace) + joined(sim.lines); + const std::pair CASES[] = { + {"00.09", StoryPose{"PewterCity", TilePos{17, 26}, Direction::SOUTH}}, + {"00.27", StoryPose{"CeruleanCity", TilePos{22, 20}, Direction::SOUTH}}, + }; + for (const auto& [id, save] : CASES){ + Simulation sim; + sim.game.save_stands = save; + const Pokemon::SegmentReport report = sim.run({.from = id, .to = id}); + const auto stepped = [&id](const std::string& item){ return item.starts_with(id + "/"); }; + const auto told = [](const std::string& line){ return line.starts_with("start pose mismatch: "); }; + if (report.completed || report.failure.failure != "start_failed" || !sim.saves.empty() + || std::ranges::any_of(sim.game.trace, stepped) || std::ranges::none_of(sim.lines, told) + ){ + return "Expected " + id + " stopped at its start check, the mismatch logged:" + joined(sim.game.trace) + joined(sim.lines); + } } return true; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.h index 467da1a4f3..ec960d1f99 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.h @@ -59,7 +59,7 @@ const std::vector>& autostory_checkpoint_jso // action args it doesn't take, or has notes for a step it doesn't have. StoryCheckpoint read_story_checkpoint_file(const JsonValue& value, std::string_view source); -// Checkpoints 00.01 to 00.25, in order: every file in StoryData/. With +// Checkpoints 00.01 to 00.27, in order: every file in StoryData/. With // 00.01b, the way back in after SID Helper, after 00.01, 00.04 split in two at Squirtle's ball for // Starter RNG: 00.04 and 00.04b, 00.12b, the way back in after Gift RNG, // after 00.12, 00.12c, the Magikarp put in BOX 1, after it, 00.14b and @@ -75,10 +75,6 @@ void add_tests_AutoStory(UnitTestDatabase& database); void add_tests_AutoStoryRunPlan(UnitTestDatabase& database); // The table run end to end against a fake game (PokemonFRLG_AutoStorySimulation_Tests.cpp). void add_tests_AutoStorySimulation(UnitTestDatabase& database); -// Rows 00.21 and 00.22 on the overworld sim (PokemonFRLG_AutoStoryVermilion_Tests.cpp). -void add_tests_AutoStoryVermilion(UnitTestDatabase& database); -// Rows 00.23 to 00.25 on the overworld sim (PokemonFRLG_AutoStoryVermilionGym_Tests.cpp). -void add_tests_AutoStoryVermilionGym(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVermilionGym_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVermilionGym_Tests.cpp index c73b72b976..0eb2a173b0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVermilionGym_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVermilionGym_Tests.cpp @@ -29,6 +29,7 @@ #include "PokemonFRLG_AutoStoryGrind.h" #include "PokemonFRLG_AutoStoryParty.h" #include "PokemonFRLG_AutoStoryRowSim.h" +#include "PokemonFRLG_AutoStoryRows_Tests.h" #include "PokemonFRLG_AutoStoryTable.h" #include "PokemonFRLG_AutoStoryTrashCans.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVermilion_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVermilion_Tests.cpp index 61a889806c..d06dcbec85 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVermilion_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryVermilion_Tests.cpp @@ -20,6 +20,7 @@ #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h" #include "PokemonFRLG_AutoStoryEvents.h" #include "PokemonFRLG_AutoStoryRowSim.h" +#include "PokemonFRLG_AutoStoryRows_Tests.h" #include "PokemonFRLG_AutoStoryTable.h" namespace PokemonAutomation{ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryViridianMart.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryViridianMart.cpp new file mode 100644 index 0000000000..0c02e86a0d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryViridianMart.cpp @@ -0,0 +1,94 @@ +/* AutoStory Viridian Mart + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "PokemonFRLG_AutoStoryActions.h" +#include "PokemonFRLG_AutoStoryBattle.h" +#include "PokemonFRLG_AutoStoryViridianMart.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// Viridian Mart before the Boulder Badge (pokefirered ViridianCity_Mart_Items): +// POKE BALL, POTION (300), ANTIDOTE (100), PARLYZ HEAL (200). Each item +// the story buys: its row, and where the bag's count is kept. +struct MartItem{ + std::string_view item; + int row; + int Supplies::* count; + // The name the item list reader reads (read_item_list_name()). + std::string_view slug; +}; +constexpr MartItem VIRIDIAN_MART_ITEMS[] = { + {.item = "potion", .row = 1, .count = &Supplies::potions, .slug = "potion"}, + {.item = "antidote", .row = 2, .count = &Supplies::antidotes, .slug = "antidote"}, + {.item = "parlyz_heal", .row = 3, .count = &Supplies::parlyz_heals, .slug = "parlyz-heal"}, +}; + +const MartItem* viridian_mart_item(const std::string& name){ + for (const MartItem& item : VIRIDIAN_MART_ITEMS){ + if (name == item.item){ + return &item; + } + } + return nullptr; +} + +} // namespace + + +Pokemon::Outcome viridian_shop(StoryEnv& env, const StoryStep& step){ + const std::optional> orders = viridian_mart_orders(Pokemon::story_arg>(step.args, "items")); + if (!orders){ + return Pokemon::outcome_failed("unknown_item", step.name); + } + // 2000 of the 3000 a new game starts with, plus the rival's prize. + buy_items(env.console, env.context, *orders, env.checks, env.language); + for (const Pokemon::StoryItem& item : Pokemon::story_arg>(step.args, "items")){ + env.supplies.*viridian_mart_item(item.item)->count += (int)item.quantity; + } + if (!checks_screen(env.checks)){ + return Pokemon::outcome_ok(); + } + // The counts from the bag, not from the orders: a wrong row once bought + // Parlyz Heals for Antidotes unnoticed (console B, 2026-11-01). + const std::vector rows = read_items_pocket(env.console, env.context, env.language); + if (rows.empty()){ + env.console.log("Story: couldn't read the Items pocket; keeping the counts ordered.", COLOR_ORANGE); + return Pokemon::outcome_ok(); + } + for (const std::string& item : set_from_bag(env.supplies, rows)){ + env.console.log(std::format( + "Story: couldn't read how many {} the bag holds; counting {}.", + item, item_count(env.supplies, item) + ), COLOR_ORANGE); + } + return Pokemon::outcome_ok(); +} + +std::optional> viridian_mart_orders(const std::vector& items){ + std::vector orders; + for (const Pokemon::StoryItem& wanted : items){ + const MartItem* found = viridian_mart_item(wanted.item); + if (found == nullptr){ + return std::nullopt; + } + orders.push_back(MartOrder{found->row, (int)wanted.quantity, found->slug.data()}); + } + return orders; +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryViridianMart.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryViridianMart.h new file mode 100644 index 0000000000..6582d62ecb --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryViridianMart.h @@ -0,0 +1,36 @@ +/* AutoStory Viridian Mart + * + * From: https://github.com/PokemonAutomation/ + * + * The SHOP step: Viridian Mart before the Boulder Badge, whose rows the + * story knows, and the bag's counts read back after it. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryViridianMart_H +#define PokemonAutomation_PokemonFRLG_AutoStoryViridianMart_H + +#include +#include +#include "PokemonFRLG_AutoStoryActions.h" +#include "PokemonFRLG_AutoStoryEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// `items` as orders at the Viridian Mart before the Boulder Badge: the +// rows of POKE BALL, POTION, ANTIDOTE, PARLYZ HEAL. Nothing if it does +// not sell one of them. +std::optional> viridian_mart_orders(const std::vector& items); + +// A SHOP step: its "items" bought at the Viridian Mart, counted into +// `env.supplies`, and with Thorough checks the counts read from the bag. +Pokemon::Outcome viridian_shop(StoryEnv& env, const StoryStep& step); + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp index d0e430ce9b..c0dd7c79fa 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp @@ -234,8 +234,8 @@ UnitTestResult check_checkpoint(const StoryCheckpoint& checkpoint, StoryPose& po UnitTestResult test_routes_and_chain(){ const std::vector& checkpoints = story_checkpoints(); - if (checkpoints.size() != 32 || checkpoints[0].start){ - return "Expected 32 checkpoints, the first from a new game."; + if (checkpoints.size() != 34 || checkpoints[0].start){ + return "Expected 34 checkpoints, the first from a new game."; } StoryPose pose; for (size_t c = 0; c < checkpoints.size(); c++){ @@ -605,7 +605,7 @@ UnitTestResult check_lines(const StoryCheckpoint& checkpoint, std::set