From a8746ebf21a32cf8e5c3440e0096b5ec79922dc9 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 01:40:04 -0800 Subject: [PATCH 1/7] WIP PokemonFRLG: 11.02 slope steps (local, unbuilt) --- .../AutoStory/PokemonFRLG_AutoStoryEvents.cpp | 5 + .../AutoStory/PokemonFRLG_AutoStoryEvents.h | 4 + .../AutoStory/PokemonFRLG_AutoStorySlope.cpp | 211 ++++++++++++++++++ .../AutoStory/PokemonFRLG_AutoStorySlope.h | 155 +++++++++++++ .../PokemonFRLG_AutoStorySlopeRun.cpp | 149 +++++++++++++ .../AutoStory/PokemonFRLG_AutoStorySlopeRun.h | 57 +++++ 6 files changed, 581 insertions(+) create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.h diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index b2007ef1ea..a71d671fdb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -28,6 +28,7 @@ #include "PokemonFRLG_AutoStoryHideout.h" #include "PokemonFRLG_AutoStoryLegs.h" #include "PokemonFRLG_AutoStoryScreenEvents.h" +#include "PokemonFRLG_AutoStorySlopeRun.h" #include "PokemonFRLG_AutoStorySummary.h" #include "PokemonFRLG_AutoStoryTable.h" #include "PokemonFRLG_AutoStoryTeachScreens.h" @@ -667,6 +668,10 @@ Outcome other_game_step(StoryEnv& env, RunState& state, const StoryStep& step){ if (!done){ done = run_abilities(env, state, step); } + if (!done){ + SlopeEnv slope{env.console, env.context, env.supplies, env.checks, env.language, env.counted_items}; + done = run_slope_step(slope, step); + } if (done && done->succeeded() && !done->pose){ // Pacing leaves the player on either of its tiles. done->pose = paced_pose(state.pose, step); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h index 50e0668b63..a80c8f7982 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h @@ -21,6 +21,7 @@ #define PokemonAutomation_PokemonFRLG_AutoStoryEvents_H #include +#include #include #include #include @@ -89,6 +90,9 @@ struct StoryEnv{ // The player a new game picks. The readers tell the boy from the girl // on their own, so a save of either needs no setting. PlayerGender player_gender = PlayerGender::BOY; + // The Items pocket's counts a count_items step kept + // (PokemonFRLG_AutoStorySlopeRun.h), for the next to compare. + std::map counted_items = {}; }; // After a save is loaded for a checkpoint that starts on `start_map`: the diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp new file mode 100644 index 0000000000..c551f2f6cb --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp @@ -0,0 +1,211 @@ +/* AutoStory Slope + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonFRLG_AutoStoryGameArgs.h" +#include "PokemonFRLG_AutoStorySlope.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using Pokemon::Direction; +using namespace SlopeMechanics; + + +namespace{ + +std::vector split_words(std::string_view text){ + std::vector words; + while (!text.empty()){ + const size_t start = text.find_first_not_of(' '); + if (start == std::string_view::npos){ + break; + } + text.remove_prefix(start); + const size_t end = std::min(text.find(' '), text.size()); + words.push_back(text.substr(0, end)); + text.remove_prefix(end); + } + return words; +} + +std::optional parse_direction(std::string_view word){ + for (Direction direction : Pokemon::ALL_DIRECTIONS){ + if (Pokemon::direction_name(direction) == word){ + return direction; + } + } + return std::nullopt; +} + +std::optional parse_tiles(std::string_view word){ + size_t tiles = 0; + const auto [end, error] = std::from_chars(word.data(), word.data() + word.size(), tiles); + if (error != std::errc() || end != word.data() + word.size() || tiles == 0){ + return std::nullopt; + } + return tiles; +} + +// "roll " or "brake ". +std::optional parse_roll(SlopeMoveKind kind, const std::vector& words){ + const std::optional tiles = words.size() == 2 ? parse_tiles(words[1]) : std::nullopt; + if (!tiles){ + return std::nullopt; + } + return SlopeMove{.kind = kind, .tiles = *tiles}; +} + +// "step " or "search ". +std::optional parse_directed(SlopeMoveKind kind, const std::vector& words){ + const size_t count = kind == SlopeMoveKind::SEARCH ? 4 : 3; + if (words.size() != count){ + return std::nullopt; + } + const std::optional direction = parse_direction(words[1]); + const std::optional tiles = parse_tiles(words[2]); + if (!direction || !tiles){ + return std::nullopt; + } + SlopeMove move{.kind = kind, .direction = *direction, .tiles = *tiles}; + if (kind == SlopeMoveKind::SEARCH){ + move.item = std::string(words[3]); + } + return move; +} + +SlopePress held_b(Milliseconds hold){ + return SlopePress{.b = true, .hold = hold}; +} + +} // namespace + + +std::optional parse_slope_move(std::string_view text){ + const std::vector words = split_words(text); + if (words.empty()){ + return std::nullopt; + } + if (words[0] == "roll"){ + return parse_roll(SlopeMoveKind::ROLL, words); + } + if (words[0] == "brake"){ + return parse_roll(SlopeMoveKind::BRAKE, words); + } + if (words[0] == "step"){ + return parse_directed(SlopeMoveKind::STEP, words); + } + if (words[0] == "search"){ + return parse_directed(SlopeMoveKind::SEARCH, words); + } + return std::nullopt; +} + +std::optional> parse_slope_step(const Pokemon::StoryStep& step){ + const std::vector* words = step.action == SLOPE_VARIANT + ? game_arg>(step.args, "moves") + : nullptr; + if (words == nullptr || words->empty()){ + return std::nullopt; + } + std::vector moves; + for (const std::string& text : *words){ + std::optional move = parse_slope_move(text); + if (!move){ + return std::nullopt; + } + moves.push_back(std::move(*move)); + } + return moves; +} + +Pokemon::StoryActionSpec slope_action_spec(){ + return {.kind = Pokemon::StoryStepKind::GAME, .name = SLOPE_VARIANT, .args = { + required_arg("moves", Pokemon::StoryArgType::WORDS, + "the moves on Route 17's slope, in order: \"step \", \"roll \", \"brake \", " + "\"search \" (PokemonFRLG_AutoStorySlope.h)"), + }}; +} + +std::vector slope_move_presses(const SlopeMove& move){ + std::vector presses; + switch (move.kind){ + case SlopeMoveKind::STEP: + for (size_t c = 0; c < move.tiles; c++){ + presses.push_back(SlopePress{.b = true, .dpad = move.direction, .hold = STEP_TAP}); + presses.push_back(held_b(STEP_HOLD)); + } + break; + case SlopeMoveKind::ROLL: + presses.push_back(SlopePress{.hold = ROLL_TILE_SLOWEST * move.tiles + BRAKE_SETTLE}); + break; + case SlopeMoveKind::BRAKE: + presses.push_back(SlopePress{.hold = ROLL_TILE * move.tiles}); + presses.push_back(held_b(BRAKE_SETTLE)); + break; + case SlopeMoveKind::SEARCH: + for (size_t c = 0; c < move.tiles; c++){ + presses.push_back(SlopePress{.b = true, .dpad = move.direction, .hold = STEP_TAP}); + presses.push_back(held_b(STEP_HOLD)); + presses.push_back(SlopePress{.b = true, .a = true, .hold = SEARCH_A}); + presses.push_back(held_b(SEARCH_WAIT)); + } + break; + } + return presses; +} + +std::vector close_message_presses(){ + std::vector presses; + for (size_t c = 0; c < CLOSE_PRESSES; c++){ + presses.push_back(SlopePress{.b = true, .a = true, .hold = CLOSE_PRESS}); + presses.push_back(held_b(CLOSE_WAIT)); + } + return presses; +} + +SlopeResult ride_slope(const std::vector& moves, const SlopeCalls& calls){ + SlopeResult result; + std::vector batch; + size_t first = 0; + for (size_t m = 0; m < moves.size(); m++){ + const std::vector presses = slope_move_presses(moves[m]); + batch.insert(batch.end(), presses.begin(), presses.end()); + const bool search = moves[m].kind == SlopeMoveKind::SEARCH; + if (!search && m + 1 < moves.size()){ + continue; + } + calls.log(std::format("Slope: moves {} to {}, {} presses.", first + 1, m + 1, batch.size())); + const SlopeSent sent = calls.send(batch, search); + batch.clear(); + first = m + 1; + if (sent == SlopeSent::BATTLE){ + return SlopeResult{.end = SlopeEnd::BATTLE, .found = result.found, .move = m}; + } + if (search && sent != SlopeSent::MESSAGE){ + return SlopeResult{.end = SlopeEnd::NOT_FOUND, .found = result.found, .move = m}; + } + if (search){ + calls.log("Slope: " + moves[m].item + "'s message opened."); + result.found.push_back(moves[m].item); + batch = close_message_presses(); + } + } + if (!batch.empty()){ + // A search was the last move: its message is closed with B held. + calls.send(batch, false); + } + return result; +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h new file mode 100644 index 0000000000..76f8fda263 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h @@ -0,0 +1,155 @@ +/* AutoStory Slope + * + * From: https://github.com/PokemonAutomation/ + * + * Route 17, the Cycling Road: its slope (pokefirered + * MB_CYCLING_ROAD_PULL_DOWN) pulls a rider south a tile at a time unless B + * is held. The route has no runtime map (user, 2026-11-12: "needs to be + * mapless"): the story rides it with presses, counted from where its + * stops leave the player. Each stop is water or a wall the slope rolls the + * player onto, or the message of a hidden item found with A. + * + * The SLOPE_VARIANT GAME step's `moves` are words, one move each: + * "step " B held, n taps of the D-pad, a tile each. + * "roll " B released: the slope pulls the player to the + * stop n tiles south, and the wait is n tiles'. + * "brake " B released for n tiles' roll, then held. + * "search " B held: a tap, then A, up to n times, + * until the hidden item's message opens. + * The controller's queue is never empty between two moves, nor while B + * holds the player on the slope (a gap rolls them): the presses up to a + * search, and from its message to the next, go to the controller in one + * batch. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStorySlope_H +#define PokemonAutomation_PokemonFRLG_AutoStorySlope_H + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" +#include "Pokemon/Navigation/Pokemon_Direction.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +inline const std::string SLOPE_VARIANT = "slope"; + +// Not measured on the console (11.02 has not run yet): each is a guess, +// to be measured on Route 17 before the first run counts. +namespace SlopeMechanics{ +// A tile of the slope's pull with B released. pokefirered's downhill +// ride is the bike's top speed; on flat ground the Bicycle took 100ms a +// tile from rest (MovementMechanics::BIKE_TILE_TIME). Sets where "brake" +// stops the player. +inline constexpr Milliseconds ROLL_TILE(100); +// The slowest a roll may be, for the wait of "roll" to its stop: an +// overlong wait is harmless, the player stands at the stop. +inline constexpr Milliseconds ROLL_TILE_SLOWEST(150); +// After B is pressed mid-roll, until the player stands: the tile under +// way finishes. Whether B stops the player at once is not known. +inline constexpr Milliseconds BRAKE_SETTLE(300); +// A D-pad tap with B held: whether it moves exactly a tile on the slope +// is not known (on flat ground a 40ms tap does, BIKE_TAP). +inline constexpr Milliseconds STEP_TAP(40); +// B alone after each tap, start to start BIKE_TAP_PERIOD and more. +inline constexpr Milliseconds STEP_HOLD(160); +// A with B held: whether the game takes the A with B down is not known. +inline constexpr Milliseconds SEARCH_A(160); +// B alone after the A, for the item's message to open. +inline constexpr Milliseconds SEARCH_WAIT(900); +// A and B pressed together, then B alone, this many times to close the +// item's two pages ("found a ..." and "put the ... in the ITEMS POCKET"). +// An A more faces a tile with nothing on it. +inline constexpr size_t CLOSE_PRESSES = 4; +inline constexpr Milliseconds CLOSE_PRESS(160); +inline constexpr Milliseconds CLOSE_WAIT(700); +} + +enum class SlopeMoveKind{ + STEP, + ROLL, + BRAKE, + SEARCH, +}; + +struct SlopeMove{ + SlopeMoveKind kind = SlopeMoveKind::ROLL; + // STEP's and SEARCH's. + Pokemon::Direction direction = Pokemon::Direction::SOUTH; + // STEP: taps; ROLL: tiles to the stop; BRAKE: tiles rolled before B; + // SEARCH: most taps. + size_t tiles = 0; + // SEARCH's item, its bag slug ("rare-candy"). + std::string item = {}; +}; + +// One move's words, or nothing if they aren't one. +std::optional parse_slope_move(std::string_view words); +// A SLOPE_VARIANT step's moves, or nothing if it isn't one or a move +// doesn't parse. +std::optional> parse_slope_step(const Pokemon::StoryStep& step); +// SLOPE_VARIANT and its args. +Pokemon::StoryActionSpec slope_action_spec(); + +// One state of the controller, held for `hold`. +struct SlopePress{ + bool b = false; + bool a = false; + std::optional dpad = std::nullopt; + Milliseconds hold = Milliseconds(0); +}; + +// The presses of `move`, back to back. +std::vector slope_move_presses(const SlopeMove& move); +// The presses that close a found item's message, ending with B held. +std::vector close_message_presses(); + +// How a batch of presses went: all sent, or cut short by a message (an +// item's, after a search's A) or a battle. +enum class SlopeSent{ + ALL, + MESSAGE, + BATTLE, +}; +// The console side of ride_slope(): `send` sends `presses` back to back, +// watching for a message only when `message` is set, and for a battle. +struct SlopeCalls{ + std::function& presses, bool message)> send; + std::function log; +}; + +enum class SlopeEnd{ + DONE, + // A search's taps all went with no message: the item isn't where the + // moves before it left the player. + NOT_FOUND, + // A battle cut the presses short; where it left the player is unknown. + BATTLE, +}; +struct SlopeResult{ + SlopeEnd end = SlopeEnd::DONE; + // The items whose messages opened, in order. + std::vector found = {}; + // The move under way when it ended other than DONE, from 0. + size_t move = 0; +}; +// Rides `moves`: the presses up to and including each search in one +// batch, and from its message on with the next. +SlopeResult ride_slope(const std::vector& moves, const SlopeCalls& calls); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp new file mode 100644 index 0000000000..679a4f3aa3 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp @@ -0,0 +1,149 @@ +/* AutoStory Slope Run + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.h" +#include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalkMoves.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "PokemonFRLG_AutoStoryBattle.h" +#include "PokemonFRLG_AutoStoryGameArgs.h" +#include "PokemonFRLG_AutoStoryItemsPocket.h" +#include "PokemonFRLG_AutoStoryPresses.h" +#include "PokemonFRLG_AutoStorySlope.h" +#include "PokemonFRLG_AutoStorySlopeRun.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using Pokemon::StoryStep; + + +namespace{ + +// A battle's script and its end, back to the overworld. +constexpr Milliseconds SLOPE_BATTLE_EXIT = std::chrono::seconds(20); + +Button slope_buttons(const SlopePress& press){ + Button buttons = BUTTON_NONE; + if (press.b){ + buttons |= BUTTON_B; + } + if (press.a){ + buttons |= BUTTON_A; + } + return buttons; +} + +// Sends `presses` back to back, one batch with no wait between, watching +// for a battle and, if `message`, for a message. +SlopeSent send_slope_presses(SlopeEnv& env, const std::vector& presses, bool message){ + WhiteDialogWatcher opened(COLOR_RED); + BattleScreenWatcher battle(COLOR_RED); + std::vector watchers{battle}; + if (message){ + watchers.push_back(opened); + } + const int seen = run_until( + env.console, env.context, + [&presses](ProControllerContext& context){ + for (const SlopePress& press : presses){ + const DpadPosition dpad = press.dpad ? OverworldWalkSteps::dpad(*press.dpad) : DPAD_NONE; + pbf_controller_state(context, slope_buttons(press), dpad, {0, 0}, {0, 0}, press.hold); + } + }, + watchers + ); + if (seen == 0){ + return SlopeSent::BATTLE; + } + return seen == 1 ? SlopeSent::MESSAGE : SlopeSent::ALL; +} + +std::string slope_end_name(SlopeEnd end){ + switch (end){ + case SlopeEnd::DONE: return "done"; + case SlopeEnd::NOT_FOUND: return "not_found"; + case SlopeEnd::BATTLE: return "battle"; + } + return "unknown"; +} + +Pokemon::Outcome ride(SlopeEnv& env, const StoryStep& step, const std::vector& moves){ + const SlopeCalls calls{ + .send = [&env](const std::vector& presses, bool message){ + return send_slope_presses(env, presses, message); + }, + .log = [&env](const std::string& text){ env.console.log(text); }, + }; + const SlopeResult result = ride_slope(moves, calls); + if (result.end == SlopeEnd::BATTLE){ + env.console.log("Slope: a Biker's battle; fighting it.", COLOR_ORANGE); + fight_battle(env.console, env.context, env.supplies, BattleKind::TRAINER, env.checks, env.language); + mash_until_overworld(env.console, env.context, SLOPE_BATTLE_EXIT); + } + if (result.end != SlopeEnd::DONE){ + return Pokemon::outcome_failed("slope_" + slope_end_name(result.end), std::format("move {}", result.move + 1)); + } + return step.pose_after ? Pokemon::outcome_ok(Pokemon::pose_of(*step.pose_after)) : Pokemon::outcome_ok(); +} + +int read_count(const std::vector& rows, const std::string& item){ + const auto row = std::find_if(rows.begin(), rows.end(), [&](const BagRow& data){ return data.item == item; }); + return row == rows.end() ? 0 : row->count; +} + +Pokemon::Outcome count_items(SlopeEnv& env, const StoryStep& step){ + const std::vector* items = game_arg>(step.args, "items"); + const bool* found = game_arg(step.args, "found"); + if (items == nullptr){ + return Pokemon::unsupported_step(step); + } + const std::vector rows = read_items_pocket(env.console, env.context, env.language); + for (const std::string& item : *items){ + const int count = read_count(rows, item); + env.console.log(std::format("Bag: {} x{}.", item, count)); + if (count < 0){ + return Pokemon::outcome_failed("count_unread", item); + } + if (found != nullptr && *found && count != env.counted_items[item] + 1){ + return Pokemon::outcome_failed("item_not_found", std::format("{}: {}, was {}", item, count, env.counted_items[item])); + } + env.counted_items[item] = count; + } + return Pokemon::outcome_ok(); +} + +} // namespace + + +Pokemon::StoryActionSpec count_items_action_spec(){ + return {.kind = Pokemon::StoryStepKind::GAME, .name = COUNT_ITEMS_VARIANT, .args = { + required_arg("items", Pokemon::StoryArgType::WORDS, "the items' bag slugs (\"rare-candy\")"), + optional_arg("found", Pokemon::StoryArgType::BOOLEAN, false, "each must be one more than the last count kept"), + }}; +} + +std::optional run_slope_step(SlopeEnv& env, const StoryStep& step){ + if (step.action == COUNT_ITEMS_VARIANT){ + return count_items(env, step); + } + const std::optional> moves = parse_slope_step(step); + if (!moves){ + return std::nullopt; + } + return ride(env, step, *moves); +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.h new file mode 100644 index 0000000000..8de22792e0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.h @@ -0,0 +1,57 @@ +/* AutoStory Slope Run + * + * From: https://github.com/PokemonAutomation/ + * + * On the console: the Cycling Road's slope (PokemonFRLG_AutoStorySlope.h), + * and the Items pocket's counts that confirm the items found on it. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStorySlopeRun_H +#define PokemonAutomation_PokemonFRLG_AutoStorySlopeRun_H + +#include +#include +#include +#include "CommonFramework/Language.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "Pokemon/AutoStory/Pokemon_StoryEvents.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" +#include "PokemonFRLG_AutoStorySupplies.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The GAME step that reads the Items pocket's counts of `items` (bag +// slugs). Without `found` it keeps them; with it, each must be one more +// than kept: the item was found since. +inline const std::string COUNT_ITEMS_VARIANT = "count_items"; +// COUNT_ITEMS_VARIANT and its args. +Pokemon::StoryActionSpec count_items_action_spec(); + +// What a slope or count step needs from the story. +struct SlopeEnv{ + ConsoleHandle& console; + ProControllerContext& context; + Supplies& supplies; + Pokemon::StoryCheckLevel checks; + Language language; + // The counts COUNT_ITEMS_VARIANT kept. + std::map& counted_items; +}; + +// On the console, a SLOPE_VARIANT or COUNT_ITEMS_VARIANT step; nothing for +// another step. A Biker who catches the player on the slope is fought; +// the step then fails, as where the battle left the player is unknown. +std::optional run_slope_step(SlopeEnv& env, const Pokemon::StoryStep& step); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif From 39b92fcb1e2ff1b24bbdcf1e70696f5dc3426d46 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 01:56:21 -0800 Subject: [PATCH 2/7] PokemonFRLG: 11.02, down the Cycling Road mapless with its Rare Candy and Max Elixir, to the Safari Zone's door Route 17 has no runtime map: a slope GAME step rides it with presses (PokemonFRLG_AutoStorySlope.h), each batch ending at water, a wall or a hidden item's message, B held throughout a batch so the controller's queue never empties on the slope. count_items reads the Items pocket before and after: each found item must be one more. Route 18, its east gate and Fuchsia City get runtime maps. Sim: a Route 17 model from pokefirered rides the same presses (both items, no Biker sight, a queue gap rolls, a late brake fails the search); Route 16 and Route 18 to Fuchsia on the overworld sim. Slope speeds and B's brake are guesses until measured on console B. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Menus/PokemonFRLG_SaveCardReader.cpp | 1 + .../Overworld/PokemonFRLG_OverworldMaps.cpp | 6 + .../PokemonFRLG/PokemonFRLG_Tests.cpp | 1 + .../AutoStory/PokemonFRLG_AutoStoryEvents.cpp | 1 + .../PokemonFRLG_AutoStoryFaultSweep_Tests.cpp | 2 + .../PokemonFRLG_AutoStoryPlanGolden11.h | 23 + .../PokemonFRLG_AutoStoryRows_Tests.h | 2 + .../PokemonFRLG_AutoStoryRunPlan_Tests.cpp | 8 +- .../AutoStory/PokemonFRLG_AutoStorySlope.cpp | 422 +++++++++--------- .../AutoStory/PokemonFRLG_AutoStorySlope.h | 310 ++++++------- .../PokemonFRLG_AutoStorySlopeRun.cpp | 298 ++++++------- .../AutoStory/PokemonFRLG_AutoStorySlopeRun.h | 114 ++--- .../PokemonFRLG_AutoStorySlope_Tests.cpp | 370 +++++++++++++++ .../PokemonFRLG_AutoStoryStepTable.cpp | 3 + .../AutoStory/PokemonFRLG_AutoStory_Tests.cpp | 4 +- .../AutoStory/StoryData/frlg-11.02.plan.json | 81 ++++ SerialPrograms/cmake/SourceFiles.cmake | 5 + 17 files changed, 1073 insertions(+), 578 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-11.02.plan.json diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp index 46420ba7a2..d00e6e19eb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp @@ -82,6 +82,7 @@ class PlaceNameMatcher : public OCR::SmallDictionaryMatcher{ {"rocket-hideout", "Rocket Hideout"}, {"pokemon-tower", "Pokemon Tower"}, {"route-16", "Route 16"}, + {"fuchsia-city", "Fuchsia City"}, }){ JsonArray names; names.push_back(std::string(name)); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.cpp index cc3802327d..7dd074f2f1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.cpp @@ -122,6 +122,12 @@ constexpr std::string_view MAP_NAMES[] = { "Route16", "Route16_NorthEntrance_1F", "Route16_House", + // Route 18 from the Cycling Road's foot (Route 17 has no map: its + // slope is ridden by presses, PokemonFRLG_AutoStorySlope.h), its gate, + // and Fuchsia City to the Safari Zone's door. + "Route18", + "Route18_EastEntrance_1F", + "FuchsiaCity", "CeruleanCave_1F", "CeruleanCave_2F", "CeruleanCave_B1F", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index 12467bccfa..238b3405fa 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -328,6 +328,7 @@ static void add_tests_AutoStoryRows(UnitTestDatabase& database){ add_tests_AutoStoryVermilionGym(database); add_tests_AutoStoryCeladon(database); add_tests_AutoStorySnorlax(database); + add_tests_AutoStorySlope(database); } void add_tests(UnitTestDatabase& database){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index a71d671fdb..5fd3aafa18 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -368,6 +368,7 @@ std::string save_card_place(const std::string& map){ {"RocketHideout", "rocket-hideout"}, {"PokemonTower", "pokemon-tower"}, {"Route16", "route-16"}, + {"FuchsiaCity", "fuchsia-city"}, }; for (const auto& [prefix, place] : PLACES){ if (map == prefix || map.rfind(prefix + "_", 0) == 0){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFaultSweep_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFaultSweep_Tests.cpp index 08b6eb0537..40e7f4bd7b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFaultSweep_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFaultSweep_Tests.cpp @@ -119,6 +119,8 @@ const std::map KNOWN_FAILING{ "below_fuji_house plans from (4, 7), where Mr. Fuji's script warps the player (warp 4, 7, exact); from a tile off, no wall in his house funnels the walk onto (3, 4): legs that re-anchor, if the console shows the warp can land elsewhere"}, {"PokemonFRLG::AutoStory - Fault sweep: 11.01, warp no-op", "fly_celadon: a Fly that doesn't leave Lavender Town goes unseen, and east_of_snorlax walks from Lavender to Route 16 by Routes 8 and 7 instead: check the fly lands on its town's tile (FlyTown::landing) before the step ends"}, + {"PokemonFRLG::AutoStory - Fault sweep: 11.02, scene end pose", + "down_the_slope is mapless and ends against Route 18's water by its column's count alone; a tile off, safari_zone_door misses the gate's door (41, 9): a leg after the slope that re-anchors the column on a wall, once the console shows how far the slope's taps can be off"}, }; // The land encounter rates of the maps the story walks (pokefirered diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden11.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden11.h index 89f81fbbb8..77f70f6cf8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden11.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden11.h @@ -30,6 +30,29 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN_11[] = { "11.01 walk west 20 -> Route16 (32, 13) [connection 5]", "11.01 east_of_cycling_road_gate from Route16 (32, 13) facing west to Route16 (28, 13) facing west", "11.01 walk west 4 -> Route16 (28, 13)", + "11.02 top_of_cycling_road from Route16 (28, 13) facing west to Route16 (14, 19) facing south", + "11.02 walk west 2 (warp) -> Route16_NorthEntrance_1F (11, 12)", + "11.02 walk west 11 (warp) -> Route16 (20, 13)", + "11.02 walk west 7 -> Route16 (13, 13)", + "11.02 walk south 6 -> Route16 (13, 19)", + "11.02 walk east 1 (to wall) -> Route16 (14, 19)", + "11.02 face south -> Route16 (14, 19)", + "11.02 safari_zone_door from Route18 (12, 10) facing south to FuchsiaCity (24, 6) facing north", + "11.02 walk north 1 -> Route18 (12, 9)", + "11.02 walk east 30 (warp) -> Route18_EastEntrance_1F (1, 6)", + "11.02 walk east 11 (warp) -> Route18 (48, 9)", + "11.02 walk east 18 (to wall) -> FuchsiaCity (6, 19) [connection 12]", + "11.02 walk south 6 (to wall) -> FuchsiaCity (6, 25)", + "11.02 walk east 23 -> FuchsiaCity (29, 25)", + "11.02 walk north 8 (to wall) -> FuchsiaCity (29, 17)", + "11.02 walk east 7 -> FuchsiaCity (36, 17)", + "11.02 walk south 1 (to wall) -> FuchsiaCity (36, 18)", + "11.02 walk east 9 (to wall) -> FuchsiaCity (45, 18)", + "11.02 walk north 13 (to wall) -> FuchsiaCity (45, 5)", + "11.02 walk west 17 (to wall) -> FuchsiaCity (28, 5)", + "11.02 walk south 1 (to wall) -> FuchsiaCity (28, 6)", + "11.02 walk west 4 -> FuchsiaCity (24, 6)", + "11.02 face north -> FuchsiaCity (24, 6)", }; } // namespace PokemonFRLG diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h index 791b20785a..416b5b4b21 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h @@ -30,6 +30,8 @@ void add_tests_AutoStoryLavender(UnitTestDatabase& database); void add_tests_AutoStoryTower(UnitTestDatabase& database); // Row 11.01 on the overworld sim (PokemonFRLG_AutoStorySnorlax_Tests.cpp). void add_tests_AutoStorySnorlax(UnitTestDatabase& database); +// Row 11.02 on the sims (PokemonFRLG_AutoStorySlope_Tests.cpp). +void add_tests_AutoStorySlope(UnitTestDatabase& database); } // namespace PokemonFRLG 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 90444c3e15..ae97d65a01 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 = "01.01 01.03 01.04 01.05 02.01 02.02 02.03 03.01 03.02 03.03 04.01 04.02 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01"; +const std::string TODAY = "01.01 01.03 01.04 01.05 02.01 02.02 02.03 03.01 03.02 03.03 04.01 04.02 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01 11.02"; const RunPlanScenario RUN_PLAN_CASES[] = { { @@ -230,11 +230,11 @@ const RunPlanScenario RUN_PLAN_CASES[] = { }, { .input = {.name = "Gift RNG off: 04.03 is passed over", .first = "04.01"}, - .expectation = {.expected_ids = "04.01 04.02 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01"}, + .expectation = {.expected_ids = "04.01 04.02 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01 11.02"}, }, { .input = {.name = "Gift RNG off: 04.03 runs when started at", .first = "04.03"}, - .expectation = {.expected_ids = "04.03 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01"}, + .expectation = {.expected_ids = "04.03 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01 11.02"}, }, { .input = { .name = "Gift RNG: started at 04.03, from the save beside the salesman", @@ -273,7 +273,7 @@ const RunPlanScenario RUN_PLAN_CASES[] = { }, { .input = {.name = "Starter RNG alone: 04.03 runs when started at", .first = "04.03", .handoffs = {.starter_rng = true}}, - .expectation = {.expected_ids = "04.03 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01"}, + .expectation = {.expected_ids = "04.03 04.04 04.05 04.06 05.01 05.02 05.03 05.04 05.05 05.06 05.07 05.08 05.09 05.10 06.01 06.02 06.03 06.04 07.01 07.02 08.01 08.02 08.03 09.01 09.02 09.03 10.01 10.02 10.03 10.04 10.05 10.06 11.01 11.02"}, }, { .input = { .name = "Starter RNG first, with both on", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp index c551f2f6cb..c50b41a0ab 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp @@ -1,211 +1,211 @@ -/* AutoStory Slope - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include -#include -#include -#include "PokemonFRLG_AutoStoryGameArgs.h" -#include "PokemonFRLG_AutoStorySlope.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonFRLG{ - -using Pokemon::Direction; -using namespace SlopeMechanics; - - -namespace{ - -std::vector split_words(std::string_view text){ - std::vector words; - while (!text.empty()){ - const size_t start = text.find_first_not_of(' '); - if (start == std::string_view::npos){ - break; - } - text.remove_prefix(start); - const size_t end = std::min(text.find(' '), text.size()); - words.push_back(text.substr(0, end)); - text.remove_prefix(end); - } - return words; -} - -std::optional parse_direction(std::string_view word){ - for (Direction direction : Pokemon::ALL_DIRECTIONS){ - if (Pokemon::direction_name(direction) == word){ - return direction; - } - } - return std::nullopt; -} - -std::optional parse_tiles(std::string_view word){ - size_t tiles = 0; - const auto [end, error] = std::from_chars(word.data(), word.data() + word.size(), tiles); - if (error != std::errc() || end != word.data() + word.size() || tiles == 0){ - return std::nullopt; - } - return tiles; -} - -// "roll " or "brake ". -std::optional parse_roll(SlopeMoveKind kind, const std::vector& words){ - const std::optional tiles = words.size() == 2 ? parse_tiles(words[1]) : std::nullopt; - if (!tiles){ - return std::nullopt; - } - return SlopeMove{.kind = kind, .tiles = *tiles}; -} - -// "step " or "search ". -std::optional parse_directed(SlopeMoveKind kind, const std::vector& words){ - const size_t count = kind == SlopeMoveKind::SEARCH ? 4 : 3; - if (words.size() != count){ - return std::nullopt; - } - const std::optional direction = parse_direction(words[1]); - const std::optional tiles = parse_tiles(words[2]); - if (!direction || !tiles){ - return std::nullopt; - } - SlopeMove move{.kind = kind, .direction = *direction, .tiles = *tiles}; - if (kind == SlopeMoveKind::SEARCH){ - move.item = std::string(words[3]); - } - return move; -} - -SlopePress held_b(Milliseconds hold){ - return SlopePress{.b = true, .hold = hold}; -} - -} // namespace - - -std::optional parse_slope_move(std::string_view text){ - const std::vector words = split_words(text); - if (words.empty()){ - return std::nullopt; - } - if (words[0] == "roll"){ - return parse_roll(SlopeMoveKind::ROLL, words); - } - if (words[0] == "brake"){ - return parse_roll(SlopeMoveKind::BRAKE, words); - } - if (words[0] == "step"){ - return parse_directed(SlopeMoveKind::STEP, words); - } - if (words[0] == "search"){ - return parse_directed(SlopeMoveKind::SEARCH, words); - } - return std::nullopt; -} - -std::optional> parse_slope_step(const Pokemon::StoryStep& step){ - const std::vector* words = step.action == SLOPE_VARIANT - ? game_arg>(step.args, "moves") - : nullptr; - if (words == nullptr || words->empty()){ - return std::nullopt; - } - std::vector moves; - for (const std::string& text : *words){ - std::optional move = parse_slope_move(text); - if (!move){ - return std::nullopt; - } - moves.push_back(std::move(*move)); - } - return moves; -} - -Pokemon::StoryActionSpec slope_action_spec(){ - return {.kind = Pokemon::StoryStepKind::GAME, .name = SLOPE_VARIANT, .args = { - required_arg("moves", Pokemon::StoryArgType::WORDS, - "the moves on Route 17's slope, in order: \"step \", \"roll \", \"brake \", " - "\"search \" (PokemonFRLG_AutoStorySlope.h)"), - }}; -} - -std::vector slope_move_presses(const SlopeMove& move){ - std::vector presses; - switch (move.kind){ - case SlopeMoveKind::STEP: - for (size_t c = 0; c < move.tiles; c++){ - presses.push_back(SlopePress{.b = true, .dpad = move.direction, .hold = STEP_TAP}); - presses.push_back(held_b(STEP_HOLD)); - } - break; - case SlopeMoveKind::ROLL: - presses.push_back(SlopePress{.hold = ROLL_TILE_SLOWEST * move.tiles + BRAKE_SETTLE}); - break; - case SlopeMoveKind::BRAKE: - presses.push_back(SlopePress{.hold = ROLL_TILE * move.tiles}); - presses.push_back(held_b(BRAKE_SETTLE)); - break; - case SlopeMoveKind::SEARCH: - for (size_t c = 0; c < move.tiles; c++){ - presses.push_back(SlopePress{.b = true, .dpad = move.direction, .hold = STEP_TAP}); - presses.push_back(held_b(STEP_HOLD)); - presses.push_back(SlopePress{.b = true, .a = true, .hold = SEARCH_A}); - presses.push_back(held_b(SEARCH_WAIT)); - } - break; - } - return presses; -} - -std::vector close_message_presses(){ - std::vector presses; - for (size_t c = 0; c < CLOSE_PRESSES; c++){ - presses.push_back(SlopePress{.b = true, .a = true, .hold = CLOSE_PRESS}); - presses.push_back(held_b(CLOSE_WAIT)); - } - return presses; -} - -SlopeResult ride_slope(const std::vector& moves, const SlopeCalls& calls){ - SlopeResult result; - std::vector batch; - size_t first = 0; - for (size_t m = 0; m < moves.size(); m++){ - const std::vector presses = slope_move_presses(moves[m]); - batch.insert(batch.end(), presses.begin(), presses.end()); - const bool search = moves[m].kind == SlopeMoveKind::SEARCH; - if (!search && m + 1 < moves.size()){ - continue; - } - calls.log(std::format("Slope: moves {} to {}, {} presses.", first + 1, m + 1, batch.size())); - const SlopeSent sent = calls.send(batch, search); - batch.clear(); - first = m + 1; - if (sent == SlopeSent::BATTLE){ - return SlopeResult{.end = SlopeEnd::BATTLE, .found = result.found, .move = m}; - } - if (search && sent != SlopeSent::MESSAGE){ - return SlopeResult{.end = SlopeEnd::NOT_FOUND, .found = result.found, .move = m}; - } - if (search){ - calls.log("Slope: " + moves[m].item + "'s message opened."); - result.found.push_back(moves[m].item); - batch = close_message_presses(); - } - } - if (!batch.empty()){ - // A search was the last move: its message is closed with B held. - calls.send(batch, false); - } - return result; -} - - - -} // namespace PokemonFRLG -} // namespace NintendoSwitch -} // namespace PokemonAutomation +/* AutoStory Slope + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonFRLG_AutoStoryGameArgs.h" +#include "PokemonFRLG_AutoStorySlope.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using Pokemon::Direction; +using namespace SlopeMechanics; + + +namespace{ + +std::vector split_words(std::string_view text){ + std::vector words; + while (!text.empty()){ + const size_t start = text.find_first_not_of(' '); + if (start == std::string_view::npos){ + break; + } + text.remove_prefix(start); + const size_t end = std::min(text.find(' '), text.size()); + words.push_back(text.substr(0, end)); + text.remove_prefix(end); + } + return words; +} + +std::optional parse_direction(std::string_view word){ + for (Direction direction : Pokemon::ALL_DIRECTIONS){ + if (Pokemon::direction_name(direction) == word){ + return direction; + } + } + return std::nullopt; +} + +std::optional parse_tiles(std::string_view word){ + size_t tiles = 0; + const auto [end, error] = std::from_chars(word.data(), word.data() + word.size(), tiles); + if (error != std::errc() || end != word.data() + word.size() || tiles == 0){ + return std::nullopt; + } + return tiles; +} + +// "roll " or "brake ". +std::optional parse_roll(SlopeMoveKind kind, const std::vector& words){ + const std::optional tiles = words.size() == 2 ? parse_tiles(words[1]) : std::nullopt; + if (!tiles){ + return std::nullopt; + } + return SlopeMove{.kind = kind, .tiles = *tiles}; +} + +// "step " or "search ". +std::optional parse_directed(SlopeMoveKind kind, const std::vector& words){ + const size_t count = kind == SlopeMoveKind::SEARCH ? 4 : 3; + if (words.size() != count){ + return std::nullopt; + } + const std::optional direction = parse_direction(words[1]); + const std::optional tiles = parse_tiles(words[2]); + if (!direction || !tiles){ + return std::nullopt; + } + SlopeMove move{.kind = kind, .direction = *direction, .tiles = *tiles}; + if (kind == SlopeMoveKind::SEARCH){ + move.item = std::string(words[3]); + } + return move; +} + +SlopePress held_b(Milliseconds hold){ + return SlopePress{.b = true, .hold = hold}; +} + +} // namespace + + +std::optional parse_slope_move(std::string_view text){ + const std::vector words = split_words(text); + if (words.empty()){ + return std::nullopt; + } + if (words[0] == "roll"){ + return parse_roll(SlopeMoveKind::ROLL, words); + } + if (words[0] == "brake"){ + return parse_roll(SlopeMoveKind::BRAKE, words); + } + if (words[0] == "step"){ + return parse_directed(SlopeMoveKind::STEP, words); + } + if (words[0] == "search"){ + return parse_directed(SlopeMoveKind::SEARCH, words); + } + return std::nullopt; +} + +std::optional> parse_slope_step(const Pokemon::StoryStep& step){ + const std::vector* words = step.action == SLOPE_VARIANT + ? game_arg>(step.args, "moves") + : nullptr; + if (words == nullptr || words->empty()){ + return std::nullopt; + } + std::vector moves; + for (const std::string& text : *words){ + std::optional move = parse_slope_move(text); + if (!move){ + return std::nullopt; + } + moves.push_back(std::move(*move)); + } + return moves; +} + +Pokemon::StoryActionSpec slope_action_spec(){ + return {.kind = Pokemon::StoryStepKind::GAME, .name = SLOPE_VARIANT, .args = { + required_arg("moves", Pokemon::StoryArgType::WORDS, + "the moves on Route 17's slope, in order: \"step \", \"roll \", \"brake \", " + "\"search \" (PokemonFRLG_AutoStorySlope.h)"), + }}; +} + +std::vector slope_move_presses(const SlopeMove& move){ + std::vector presses; + switch (move.kind){ + case SlopeMoveKind::STEP: + for (size_t c = 0; c < move.tiles; c++){ + presses.push_back(SlopePress{.b = true, .dpad = move.direction, .hold = STEP_TAP}); + presses.push_back(held_b(STEP_HOLD)); + } + break; + case SlopeMoveKind::ROLL: + presses.push_back(SlopePress{.hold = ROLL_TILE_SLOWEST * move.tiles + BRAKE_SETTLE}); + break; + case SlopeMoveKind::BRAKE: + presses.push_back(SlopePress{.hold = ROLL_TILE * move.tiles}); + presses.push_back(held_b(BRAKE_SETTLE)); + break; + case SlopeMoveKind::SEARCH: + for (size_t c = 0; c < move.tiles; c++){ + presses.push_back(SlopePress{.b = true, .dpad = move.direction, .hold = STEP_TAP}); + presses.push_back(held_b(STEP_HOLD)); + presses.push_back(SlopePress{.b = true, .a = true, .hold = SEARCH_A}); + presses.push_back(held_b(SEARCH_WAIT)); + } + break; + } + return presses; +} + +std::vector close_message_presses(){ + std::vector presses; + for (size_t c = 0; c < CLOSE_PRESSES; c++){ + presses.push_back(SlopePress{.b = true, .a = true, .hold = CLOSE_PRESS}); + presses.push_back(held_b(CLOSE_WAIT)); + } + return presses; +} + +SlopeResult ride_slope(const std::vector& moves, const SlopeCalls& calls){ + SlopeResult result; + std::vector batch; + size_t first = 0; + for (size_t m = 0; m < moves.size(); m++){ + const std::vector presses = slope_move_presses(moves[m]); + batch.insert(batch.end(), presses.begin(), presses.end()); + const bool search = moves[m].kind == SlopeMoveKind::SEARCH; + if (!search && m + 1 < moves.size()){ + continue; + } + calls.log(std::format("Slope: moves {} to {}, {} presses.", first + 1, m + 1, batch.size())); + const SlopeSent sent = calls.send(batch, search); + batch.clear(); + first = m + 1; + if (sent == SlopeSent::BATTLE){ + return SlopeResult{.end = SlopeEnd::BATTLE, .found = result.found, .move = m}; + } + if (search && sent != SlopeSent::MESSAGE){ + return SlopeResult{.end = SlopeEnd::NOT_FOUND, .found = result.found, .move = m}; + } + if (search){ + calls.log("Slope: " + moves[m].item + "'s message opened."); + result.found.push_back(moves[m].item); + batch = close_message_presses(); + } + } + if (!batch.empty()){ + // A search was the last move: its message is closed with B held. + calls.send(batch, false); + } + return result; +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h index 76f8fda263..7c789cc8f8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h @@ -1,155 +1,155 @@ -/* AutoStory Slope - * - * From: https://github.com/PokemonAutomation/ - * - * Route 17, the Cycling Road: its slope (pokefirered - * MB_CYCLING_ROAD_PULL_DOWN) pulls a rider south a tile at a time unless B - * is held. The route has no runtime map (user, 2026-11-12: "needs to be - * mapless"): the story rides it with presses, counted from where its - * stops leave the player. Each stop is water or a wall the slope rolls the - * player onto, or the message of a hidden item found with A. - * - * The SLOPE_VARIANT GAME step's `moves` are words, one move each: - * "step " B held, n taps of the D-pad, a tile each. - * "roll " B released: the slope pulls the player to the - * stop n tiles south, and the wait is n tiles'. - * "brake " B released for n tiles' roll, then held. - * "search " B held: a tap, then A, up to n times, - * until the hidden item's message opens. - * The controller's queue is never empty between two moves, nor while B - * holds the player on the slope (a gap rolls them): the presses up to a - * search, and from its message to the next, go to the controller in one - * batch. - * - */ - -#ifndef PokemonAutomation_PokemonFRLG_AutoStorySlope_H -#define PokemonAutomation_PokemonFRLG_AutoStorySlope_H - -#include -#include -#include -#include -#include -#include -#include "Common/Cpp/Time.h" -#include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" -#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" -#include "Pokemon/Navigation/Pokemon_Direction.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonFRLG{ - - -inline const std::string SLOPE_VARIANT = "slope"; - -// Not measured on the console (11.02 has not run yet): each is a guess, -// to be measured on Route 17 before the first run counts. -namespace SlopeMechanics{ -// A tile of the slope's pull with B released. pokefirered's downhill -// ride is the bike's top speed; on flat ground the Bicycle took 100ms a -// tile from rest (MovementMechanics::BIKE_TILE_TIME). Sets where "brake" -// stops the player. -inline constexpr Milliseconds ROLL_TILE(100); -// The slowest a roll may be, for the wait of "roll" to its stop: an -// overlong wait is harmless, the player stands at the stop. -inline constexpr Milliseconds ROLL_TILE_SLOWEST(150); -// After B is pressed mid-roll, until the player stands: the tile under -// way finishes. Whether B stops the player at once is not known. -inline constexpr Milliseconds BRAKE_SETTLE(300); -// A D-pad tap with B held: whether it moves exactly a tile on the slope -// is not known (on flat ground a 40ms tap does, BIKE_TAP). -inline constexpr Milliseconds STEP_TAP(40); -// B alone after each tap, start to start BIKE_TAP_PERIOD and more. -inline constexpr Milliseconds STEP_HOLD(160); -// A with B held: whether the game takes the A with B down is not known. -inline constexpr Milliseconds SEARCH_A(160); -// B alone after the A, for the item's message to open. -inline constexpr Milliseconds SEARCH_WAIT(900); -// A and B pressed together, then B alone, this many times to close the -// item's two pages ("found a ..." and "put the ... in the ITEMS POCKET"). -// An A more faces a tile with nothing on it. -inline constexpr size_t CLOSE_PRESSES = 4; -inline constexpr Milliseconds CLOSE_PRESS(160); -inline constexpr Milliseconds CLOSE_WAIT(700); -} - -enum class SlopeMoveKind{ - STEP, - ROLL, - BRAKE, - SEARCH, -}; - -struct SlopeMove{ - SlopeMoveKind kind = SlopeMoveKind::ROLL; - // STEP's and SEARCH's. - Pokemon::Direction direction = Pokemon::Direction::SOUTH; - // STEP: taps; ROLL: tiles to the stop; BRAKE: tiles rolled before B; - // SEARCH: most taps. - size_t tiles = 0; - // SEARCH's item, its bag slug ("rare-candy"). - std::string item = {}; -}; - -// One move's words, or nothing if they aren't one. -std::optional parse_slope_move(std::string_view words); -// A SLOPE_VARIANT step's moves, or nothing if it isn't one or a move -// doesn't parse. -std::optional> parse_slope_step(const Pokemon::StoryStep& step); -// SLOPE_VARIANT and its args. -Pokemon::StoryActionSpec slope_action_spec(); - -// One state of the controller, held for `hold`. -struct SlopePress{ - bool b = false; - bool a = false; - std::optional dpad = std::nullopt; - Milliseconds hold = Milliseconds(0); -}; - -// The presses of `move`, back to back. -std::vector slope_move_presses(const SlopeMove& move); -// The presses that close a found item's message, ending with B held. -std::vector close_message_presses(); - -// How a batch of presses went: all sent, or cut short by a message (an -// item's, after a search's A) or a battle. -enum class SlopeSent{ - ALL, - MESSAGE, - BATTLE, -}; -// The console side of ride_slope(): `send` sends `presses` back to back, -// watching for a message only when `message` is set, and for a battle. -struct SlopeCalls{ - std::function& presses, bool message)> send; - std::function log; -}; - -enum class SlopeEnd{ - DONE, - // A search's taps all went with no message: the item isn't where the - // moves before it left the player. - NOT_FOUND, - // A battle cut the presses short; where it left the player is unknown. - BATTLE, -}; -struct SlopeResult{ - SlopeEnd end = SlopeEnd::DONE; - // The items whose messages opened, in order. - std::vector found = {}; - // The move under way when it ended other than DONE, from 0. - size_t move = 0; -}; -// Rides `moves`: the presses up to and including each search in one -// batch, and from its message on with the next. -SlopeResult ride_slope(const std::vector& moves, const SlopeCalls& calls); - - - -} // namespace PokemonFRLG -} // namespace NintendoSwitch -} // namespace PokemonAutomation -#endif +/* AutoStory Slope + * + * From: https://github.com/PokemonAutomation/ + * + * Route 17, the Cycling Road: its slope (pokefirered + * MB_CYCLING_ROAD_PULL_DOWN) pulls a rider south a tile at a time unless B + * is held. The route has no runtime map (user, 2026-11-12: "needs to be + * mapless"): the story rides it with presses, counted from where its + * stops leave the player. Each stop is water or a wall the slope rolls the + * player onto, or the message of a hidden item found with A. + * + * The SLOPE_VARIANT GAME step's `moves` are words, one move each: + * "step " B held, n taps of the D-pad, a tile each. + * "roll " B released: the slope pulls the player to the + * stop n tiles south, and the wait is n tiles'. + * "brake " B released for n tiles' roll, then held. + * "search " B held: a tap, then A, up to n times, + * until the hidden item's message opens. + * The controller's queue is never empty between two moves, nor while B + * holds the player on the slope (a gap rolls them): the presses up to a + * search, and from its message to the next, go to the controller in one + * batch. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStorySlope_H +#define PokemonAutomation_PokemonFRLG_AutoStorySlope_H + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" +#include "Pokemon/Navigation/Pokemon_Direction.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +inline const std::string SLOPE_VARIANT = "slope"; + +// Not measured on the console (11.02 has not run yet): each is a guess, +// to be measured on Route 17 before the first run counts. +namespace SlopeMechanics{ +// A tile of the slope's pull with B released. pokefirered's downhill +// ride is the bike's top speed; on flat ground the Bicycle took 100ms a +// tile from rest (MovementMechanics::BIKE_TILE_TIME). Sets where "brake" +// stops the player. +inline constexpr Milliseconds ROLL_TILE(100); +// The slowest a roll may be, for the wait of "roll" to its stop: an +// overlong wait is harmless, the player stands at the stop. +inline constexpr Milliseconds ROLL_TILE_SLOWEST(150); +// After B is pressed mid-roll, until the player stands: the tile under +// way finishes. Whether B stops the player at once is not known. +inline constexpr Milliseconds BRAKE_SETTLE(300); +// A D-pad tap with B held: whether it moves exactly a tile on the slope +// is not known (on flat ground a 40ms tap does, BIKE_TAP). +inline constexpr Milliseconds STEP_TAP(40); +// B alone after each tap, start to start BIKE_TAP_PERIOD and more. +inline constexpr Milliseconds STEP_HOLD(160); +// A with B held: whether the game takes the A with B down is not known. +inline constexpr Milliseconds SEARCH_A(160); +// B alone after the A, for the item's message to open. +inline constexpr Milliseconds SEARCH_WAIT(900); +// A and B pressed together, then B alone, this many times to close the +// item's two pages ("found a ..." and "put the ... in the ITEMS POCKET"). +// An A more faces a tile with nothing on it. +inline constexpr size_t CLOSE_PRESSES = 4; +inline constexpr Milliseconds CLOSE_PRESS(160); +inline constexpr Milliseconds CLOSE_WAIT(700); +} + +enum class SlopeMoveKind{ + STEP, + ROLL, + BRAKE, + SEARCH, +}; + +struct SlopeMove{ + SlopeMoveKind kind = SlopeMoveKind::ROLL; + // STEP's and SEARCH's. + Pokemon::Direction direction = Pokemon::Direction::SOUTH; + // STEP: taps; ROLL: tiles to the stop; BRAKE: tiles rolled before B; + // SEARCH: most taps. + size_t tiles = 0; + // SEARCH's item, its bag slug ("rare-candy"). + std::string item = {}; +}; + +// One move's words, or nothing if they aren't one. +std::optional parse_slope_move(std::string_view words); +// A SLOPE_VARIANT step's moves, or nothing if it isn't one or a move +// doesn't parse. +std::optional> parse_slope_step(const Pokemon::StoryStep& step); +// SLOPE_VARIANT and its args. +Pokemon::StoryActionSpec slope_action_spec(); + +// One state of the controller, held for `hold`. +struct SlopePress{ + bool b = false; + bool a = false; + std::optional dpad = std::nullopt; + Milliseconds hold = Milliseconds(0); +}; + +// The presses of `move`, back to back. +std::vector slope_move_presses(const SlopeMove& move); +// The presses that close a found item's message, ending with B held. +std::vector close_message_presses(); + +// How a batch of presses went: all sent, or cut short by a message (an +// item's, after a search's A) or a battle. +enum class SlopeSent{ + ALL, + MESSAGE, + BATTLE, +}; +// The console side of ride_slope(): `send` sends `presses` back to back, +// watching for a message only when `message` is set, and for a battle. +struct SlopeCalls{ + std::function& presses, bool message)> send; + std::function log; +}; + +enum class SlopeEnd{ + DONE, + // A search's taps all went with no message: the item isn't where the + // moves before it left the player. + NOT_FOUND, + // A battle cut the presses short; where it left the player is unknown. + BATTLE, +}; +struct SlopeResult{ + SlopeEnd end = SlopeEnd::DONE; + // The items whose messages opened, in order. + std::vector found = {}; + // The move under way when it ended other than DONE, from 0. + size_t move = 0; +}; +// Rides `moves`: the presses up to and including each search in one +// batch, and from its message on with the next. +SlopeResult ride_slope(const std::vector& moves, const SlopeCalls& calls); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp index 679a4f3aa3..9ccac26812 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp @@ -1,149 +1,149 @@ -/* AutoStory Slope Run - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include -#include -#include "CommonTools/Async/InferenceRoutines.h" -#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" -#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.h" -#include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h" -#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalkMoves.h" -#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" -#include "PokemonFRLG_AutoStoryBattle.h" -#include "PokemonFRLG_AutoStoryGameArgs.h" -#include "PokemonFRLG_AutoStoryItemsPocket.h" -#include "PokemonFRLG_AutoStoryPresses.h" -#include "PokemonFRLG_AutoStorySlope.h" -#include "PokemonFRLG_AutoStorySlopeRun.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonFRLG{ - -using Pokemon::StoryStep; - - -namespace{ - -// A battle's script and its end, back to the overworld. -constexpr Milliseconds SLOPE_BATTLE_EXIT = std::chrono::seconds(20); - -Button slope_buttons(const SlopePress& press){ - Button buttons = BUTTON_NONE; - if (press.b){ - buttons |= BUTTON_B; - } - if (press.a){ - buttons |= BUTTON_A; - } - return buttons; -} - -// Sends `presses` back to back, one batch with no wait between, watching -// for a battle and, if `message`, for a message. -SlopeSent send_slope_presses(SlopeEnv& env, const std::vector& presses, bool message){ - WhiteDialogWatcher opened(COLOR_RED); - BattleScreenWatcher battle(COLOR_RED); - std::vector watchers{battle}; - if (message){ - watchers.push_back(opened); - } - const int seen = run_until( - env.console, env.context, - [&presses](ProControllerContext& context){ - for (const SlopePress& press : presses){ - const DpadPosition dpad = press.dpad ? OverworldWalkSteps::dpad(*press.dpad) : DPAD_NONE; - pbf_controller_state(context, slope_buttons(press), dpad, {0, 0}, {0, 0}, press.hold); - } - }, - watchers - ); - if (seen == 0){ - return SlopeSent::BATTLE; - } - return seen == 1 ? SlopeSent::MESSAGE : SlopeSent::ALL; -} - -std::string slope_end_name(SlopeEnd end){ - switch (end){ - case SlopeEnd::DONE: return "done"; - case SlopeEnd::NOT_FOUND: return "not_found"; - case SlopeEnd::BATTLE: return "battle"; - } - return "unknown"; -} - -Pokemon::Outcome ride(SlopeEnv& env, const StoryStep& step, const std::vector& moves){ - const SlopeCalls calls{ - .send = [&env](const std::vector& presses, bool message){ - return send_slope_presses(env, presses, message); - }, - .log = [&env](const std::string& text){ env.console.log(text); }, - }; - const SlopeResult result = ride_slope(moves, calls); - if (result.end == SlopeEnd::BATTLE){ - env.console.log("Slope: a Biker's battle; fighting it.", COLOR_ORANGE); - fight_battle(env.console, env.context, env.supplies, BattleKind::TRAINER, env.checks, env.language); - mash_until_overworld(env.console, env.context, SLOPE_BATTLE_EXIT); - } - if (result.end != SlopeEnd::DONE){ - return Pokemon::outcome_failed("slope_" + slope_end_name(result.end), std::format("move {}", result.move + 1)); - } - return step.pose_after ? Pokemon::outcome_ok(Pokemon::pose_of(*step.pose_after)) : Pokemon::outcome_ok(); -} - -int read_count(const std::vector& rows, const std::string& item){ - const auto row = std::find_if(rows.begin(), rows.end(), [&](const BagRow& data){ return data.item == item; }); - return row == rows.end() ? 0 : row->count; -} - -Pokemon::Outcome count_items(SlopeEnv& env, const StoryStep& step){ - const std::vector* items = game_arg>(step.args, "items"); - const bool* found = game_arg(step.args, "found"); - if (items == nullptr){ - return Pokemon::unsupported_step(step); - } - const std::vector rows = read_items_pocket(env.console, env.context, env.language); - for (const std::string& item : *items){ - const int count = read_count(rows, item); - env.console.log(std::format("Bag: {} x{}.", item, count)); - if (count < 0){ - return Pokemon::outcome_failed("count_unread", item); - } - if (found != nullptr && *found && count != env.counted_items[item] + 1){ - return Pokemon::outcome_failed("item_not_found", std::format("{}: {}, was {}", item, count, env.counted_items[item])); - } - env.counted_items[item] = count; - } - return Pokemon::outcome_ok(); -} - -} // namespace - - -Pokemon::StoryActionSpec count_items_action_spec(){ - return {.kind = Pokemon::StoryStepKind::GAME, .name = COUNT_ITEMS_VARIANT, .args = { - required_arg("items", Pokemon::StoryArgType::WORDS, "the items' bag slugs (\"rare-candy\")"), - optional_arg("found", Pokemon::StoryArgType::BOOLEAN, false, "each must be one more than the last count kept"), - }}; -} - -std::optional run_slope_step(SlopeEnv& env, const StoryStep& step){ - if (step.action == COUNT_ITEMS_VARIANT){ - return count_items(env, step); - } - const std::optional> moves = parse_slope_step(step); - if (!moves){ - return std::nullopt; - } - return ride(env, step, *moves); -} - - - -} // namespace PokemonFRLG -} // namespace NintendoSwitch -} // namespace PokemonAutomation +/* AutoStory Slope Run + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.h" +#include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalkMoves.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "PokemonFRLG_AutoStoryBattle.h" +#include "PokemonFRLG_AutoStoryGameArgs.h" +#include "PokemonFRLG_AutoStoryItemsPocket.h" +#include "PokemonFRLG_AutoStoryPresses.h" +#include "PokemonFRLG_AutoStorySlope.h" +#include "PokemonFRLG_AutoStorySlopeRun.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using Pokemon::StoryStep; + + +namespace{ + +// A battle's script and its end, back to the overworld. +constexpr Milliseconds SLOPE_BATTLE_EXIT = std::chrono::seconds(20); + +Button slope_buttons(const SlopePress& press){ + Button buttons = BUTTON_NONE; + if (press.b){ + buttons |= BUTTON_B; + } + if (press.a){ + buttons |= BUTTON_A; + } + return buttons; +} + +// Sends `presses` back to back, one batch with no wait between, watching +// for a battle and, if `message`, for a message. +SlopeSent send_slope_presses(SlopeEnv& env, const std::vector& presses, bool message){ + WhiteDialogWatcher opened(COLOR_RED); + BattleScreenWatcher battle(COLOR_RED); + std::vector watchers{battle}; + if (message){ + watchers.push_back(opened); + } + const int seen = run_until( + env.console, env.context, + [&presses](ProControllerContext& context){ + for (const SlopePress& press : presses){ + const DpadPosition dpad = press.dpad ? OverworldWalkSteps::dpad(*press.dpad) : DPAD_NONE; + pbf_controller_state(context, slope_buttons(press), dpad, {0, 0}, {0, 0}, press.hold); + } + }, + watchers + ); + if (seen == 0){ + return SlopeSent::BATTLE; + } + return seen == 1 ? SlopeSent::MESSAGE : SlopeSent::ALL; +} + +std::string slope_end_name(SlopeEnd end){ + switch (end){ + case SlopeEnd::DONE: return "done"; + case SlopeEnd::NOT_FOUND: return "not_found"; + case SlopeEnd::BATTLE: return "battle"; + } + return "unknown"; +} + +Pokemon::Outcome ride(SlopeEnv& env, const StoryStep& step, const std::vector& moves){ + const SlopeCalls calls{ + .send = [&env](const std::vector& presses, bool message){ + return send_slope_presses(env, presses, message); + }, + .log = [&env](const std::string& text){ env.console.log(text); }, + }; + const SlopeResult result = ride_slope(moves, calls); + if (result.end == SlopeEnd::BATTLE){ + env.console.log("Slope: a Biker's battle; fighting it.", COLOR_ORANGE); + fight_battle(env.console, env.context, env.supplies, BattleKind::TRAINER, env.checks, env.language); + mash_until_overworld(env.console, env.context, SLOPE_BATTLE_EXIT); + } + if (result.end != SlopeEnd::DONE){ + return Pokemon::outcome_failed("slope_" + slope_end_name(result.end), std::format("move {}", result.move + 1)); + } + return step.pose_after ? Pokemon::outcome_ok(Pokemon::pose_of(*step.pose_after)) : Pokemon::outcome_ok(); +} + +int read_count(const std::vector& rows, const std::string& item){ + const auto row = std::find_if(rows.begin(), rows.end(), [&](const BagRow& data){ return data.item == item; }); + return row == rows.end() ? 0 : row->count; +} + +Pokemon::Outcome count_items(SlopeEnv& env, const StoryStep& step){ + const std::vector* items = game_arg>(step.args, "items"); + const bool* found = game_arg(step.args, "found"); + if (items == nullptr){ + return Pokemon::unsupported_step(step); + } + const std::vector rows = read_items_pocket(env.console, env.context, env.language); + for (const std::string& item : *items){ + const int count = read_count(rows, item); + env.console.log(std::format("Bag: {} x{}.", item, count)); + if (count < 0){ + return Pokemon::outcome_failed("count_unread", item); + } + if (found != nullptr && *found && count != env.counted_items[item] + 1){ + return Pokemon::outcome_failed("item_not_found", std::format("{}: {}, was {}", item, count, env.counted_items[item])); + } + env.counted_items[item] = count; + } + return Pokemon::outcome_ok(); +} + +} // namespace + + +Pokemon::StoryActionSpec count_items_action_spec(){ + return {.kind = Pokemon::StoryStepKind::GAME, .name = COUNT_ITEMS_VARIANT, .args = { + required_arg("items", Pokemon::StoryArgType::WORDS, "the items' bag slugs (\"rare-candy\")"), + optional_arg("found", Pokemon::StoryArgType::BOOLEAN, false, "each must be one more than the last count kept"), + }}; +} + +std::optional run_slope_step(SlopeEnv& env, const StoryStep& step){ + if (step.action == COUNT_ITEMS_VARIANT){ + return count_items(env, step); + } + const std::optional> moves = parse_slope_step(step); + if (!moves){ + return std::nullopt; + } + return ride(env, step, *moves); +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.h index 8de22792e0..964ffe05f1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.h @@ -1,57 +1,57 @@ -/* AutoStory Slope Run - * - * From: https://github.com/PokemonAutomation/ - * - * On the console: the Cycling Road's slope (PokemonFRLG_AutoStorySlope.h), - * and the Items pocket's counts that confirm the items found on it. - * - */ - -#ifndef PokemonAutomation_PokemonFRLG_AutoStorySlopeRun_H -#define PokemonAutomation_PokemonFRLG_AutoStorySlopeRun_H - -#include -#include -#include -#include "CommonFramework/Language.h" -#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" -#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" -#include "Pokemon/AutoStory/Pokemon_StoryEvents.h" -#include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" -#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" -#include "PokemonFRLG_AutoStorySupplies.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonFRLG{ - - -// The GAME step that reads the Items pocket's counts of `items` (bag -// slugs). Without `found` it keeps them; with it, each must be one more -// than kept: the item was found since. -inline const std::string COUNT_ITEMS_VARIANT = "count_items"; -// COUNT_ITEMS_VARIANT and its args. -Pokemon::StoryActionSpec count_items_action_spec(); - -// What a slope or count step needs from the story. -struct SlopeEnv{ - ConsoleHandle& console; - ProControllerContext& context; - Supplies& supplies; - Pokemon::StoryCheckLevel checks; - Language language; - // The counts COUNT_ITEMS_VARIANT kept. - std::map& counted_items; -}; - -// On the console, a SLOPE_VARIANT or COUNT_ITEMS_VARIANT step; nothing for -// another step. A Biker who catches the player on the slope is fought; -// the step then fails, as where the battle left the player is unknown. -std::optional run_slope_step(SlopeEnv& env, const Pokemon::StoryStep& step); - - - -} // namespace PokemonFRLG -} // namespace NintendoSwitch -} // namespace PokemonAutomation -#endif +/* AutoStory Slope Run + * + * From: https://github.com/PokemonAutomation/ + * + * On the console: the Cycling Road's slope (PokemonFRLG_AutoStorySlope.h), + * and the Items pocket's counts that confirm the items found on it. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStorySlopeRun_H +#define PokemonAutomation_PokemonFRLG_AutoStorySlopeRun_H + +#include +#include +#include +#include "CommonFramework/Language.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "Pokemon/AutoStory/Pokemon_StoryEvents.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" +#include "PokemonFRLG_AutoStorySupplies.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The GAME step that reads the Items pocket's counts of `items` (bag +// slugs). Without `found` it keeps them; with it, each must be one more +// than kept: the item was found since. +inline const std::string COUNT_ITEMS_VARIANT = "count_items"; +// COUNT_ITEMS_VARIANT and its args. +Pokemon::StoryActionSpec count_items_action_spec(); + +// What a slope or count step needs from the story. +struct SlopeEnv{ + ConsoleHandle& console; + ProControllerContext& context; + Supplies& supplies; + Pokemon::StoryCheckLevel checks; + Language language; + // The counts COUNT_ITEMS_VARIANT kept. + std::map& counted_items; +}; + +// On the console, a SLOPE_VARIANT or COUNT_ITEMS_VARIANT step; nothing for +// another step. A Biker who catches the player on the slope is fought; +// the step then fails, as where the battle left the player is unknown. +std::optional run_slope_step(SlopeEnv& env, const Pokemon::StoryStep& step); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp new file mode 100644 index 0000000000..a3b29c86e6 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp @@ -0,0 +1,370 @@ +/* AutoStory Slope Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Row 11.02 on the sims: Route 16 and Route 18 to Fuchsia City on the + * overworld sim, and Route 17's slope, which has no runtime map, on a + * model of it here, from pokefirered's Route17 layout (map.bin, the + * celadon_city tileset's behaviors) and map.json: its walls, water, + * signs, ledges, hidden items and Bikers. The model rides the presses the + * console sends (slope_move_presses()) through ride_slope(). + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h" +#include "PokemonFRLG_AutoStoryEvents.h" +#include "PokemonFRLG_AutoStoryRowSim.h" +#include "PokemonFRLG_AutoStoryRows_Tests.h" +#include "PokemonFRLG_AutoStorySlope.h" +#include "PokemonFRLG_AutoStoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +using Pokemon::Direction; + +// Rows of the model: Route 17's 0..159, then Route 18's from 160. Row -1 +// is Route 16's (14, 19), above the slope. +constexpr int ROUTE18_ROW = 160; + +struct Biker{ + int x; + int y; + std::vector, Direction>> looks; + int sight; +}; + +// pokefirered Route17 map.json's Bikers, with the tiles they stand on +// and the ways they look: walkers a tile either way of their start. +std::vector route17_bikers(){ + const auto walker = [](int x, int y){ + Biker biker{x, y, {}, 1}; + for (int dy = -1; dy <= 1; dy++){ + biker.looks.push_back({{x, y + dy}, Direction::NORTH}); + biker.looks.push_back({{x, y + dy}, Direction::SOUTH}); + } + return biker; + }; + return { + walker(4, 18), walker(11, 15), walker(18, 41), walker(18, 94), walker(4, 116), + Biker{15, 24, {{{15, 24}, Direction::EAST}, {{16, 24}, Direction::SOUTH}, {{16, 25}, Direction::WEST}, {{15, 25}, Direction::NORTH}}, 1}, + Biker{7, 38, {{{7, 38}, Direction::WEST}, {{7, 38}, Direction::EAST}}, 5}, + Biker{2, 61, {{{2, 61}, Direction::EAST}}, 4}, + Biker{21, 61, {{{21, 61}, Direction::WEST}}, 3}, + Biker{16, 134, {{{15, 134}, Direction::WEST}, {{15, 135}, Direction::SOUTH}, {{16, 135}, Direction::EAST}, {{16, 134}, Direction::NORTH}}, 1}, + }; +} + +bool in_box(int x, int y, int x0, int x1, int y0, int y1){ + return x >= x0 && x <= x1 && y >= y0 && y <= y1; +} + +// Route 17's walls, water and signs (and Route 18's west end). +bool blocked(int x, int y){ + if (y < 0){ + return x != 14; + } + if (y >= ROUTE18_ROW){ + return !in_box(x, y, 8, 16, ROUTE18_ROW, ROUTE18_ROW + 10); + } + if (x < 2 || x > 21 || (y <= 9 && x >= 15)){ + return true; + } + if (in_box(x, y, 13, 16, 30, 97) || in_box(x, y, 6, 9, 44, 93) + || in_box(x, y, 8, 16, 98, 121) || in_box(x, y, 8, 16, 126, 132) + ){ + return true; + } + for (const auto& [sx, sy] : std::vector>{{12, 53}, {12, 68}, {12, 83}, {12, 97}, {12, 125}, {11, 157}}){ + if (x == sx && y == sy){ + return true; + } + } + if (y == 137){ + return !in_box(x, y, 8, 10, 137, 137) && !in_box(x, y, 14, 16, 137, 137); + } + return y >= 138 && (x <= 7 || x >= 17); +} + +bool ledge(int x, int y){ + return y == 158 && x >= 8 && x <= 16 && x != 12; +} + +bool slope(int x, int y){ + return y >= 0 && y <= 158 && !blocked(x, y) && !ledge(x, y); +} + +int dx_of(Direction direction){ + return direction == Direction::EAST ? 1 : direction == Direction::WEST ? -1 : 0; +} +int dy_of(Direction direction){ + return direction == Direction::SOUTH ? 1 : direction == Direction::NORTH ? -1 : 0; +} + +// Route 17 with the player on the Bicycle. +class SlopeModel{ +public: + SlopeModel(int x, int y) + : m_x(x), m_y(y) + , m_items{{{18, 83}, "rare-candy"}, {{14, 157}, "max-elixir"}} + , m_bikers(route17_bikers()) + { + for (const Biker& biker : m_bikers){ + m_biker_tiles.insert({biker.x, biker.y}); + } + } + + SlopeSent send(const std::vector& presses, bool message){ + for (const SlopePress& press : presses){ + if (press.dpad){ + move(*press.dpad); + }else if (!press.b){ + roll(press.hold); + } + if (press.a && message && face_item()){ + return SlopeSent::MESSAGE; + } + } + return SlopeSent::ALL; + } + + int x() const{ return m_x; } + int y() const{ return m_y; } + const std::vector& seen() const{ return m_seen; } + +private: + bool open(int x, int y) const{ + return !blocked(x, y) && !m_biker_tiles.contains({x, y}); + } + // One tile `direction`; south onto a ledge jumps it. + bool move(Direction direction){ + m_facing = direction; + const int x = m_x + dx_of(direction); + const int y = m_y + dy_of(direction); + if (ledge(x, y)){ + if (direction != Direction::SOUTH){ + return false; + } + m_y = y + 1; + check_sight(); + return true; + } + if (!open(x, y)){ + return false; + } + m_x = x; + m_y = y; + check_sight(); + return true; + } + // B released on the slope: a tile south each ROLL_TILE. + void roll(Milliseconds hold){ + for (Milliseconds t(0); t + SlopeMechanics::ROLL_TILE <= hold && slope(m_x, m_y); t += SlopeMechanics::ROLL_TILE){ + if (!move(Direction::SOUTH)){ + break; + } + } + } + bool face_item(){ + const auto item = m_items.find({m_x + dx_of(m_facing), m_y + dy_of(m_facing)}); + if (item == m_items.end()){ + return false; + } + m_items.erase(item); + return true; + } + void check_sight(){ + for (const Biker& biker : m_bikers){ + for (const auto& [at, look] : biker.looks){ + for (int c = 1; c <= biker.sight; c++){ + if (at.first + dx_of(look) * c == m_x && at.second + dy_of(look) * c == m_y){ + m_seen.push_back(std::format("({}, {}) by the Biker at ({}, {})", m_x, m_y, biker.x, biker.y)); + } + } + } + } + } + + int m_x; + int m_y; + Direction m_facing = Direction::SOUTH; + std::map, std::string> m_items; + std::vector m_bikers; + std::set> m_biker_tiles; + std::vector m_seen; +}; + +const StoryCheckpoint& checkpoint(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; +} + +const StoryStep* slope_step(const StoryCheckpoint& row){ + const auto step = std::find_if(row.steps.begin(), row.steps.end(), [](const StoryStep& data){ + return parse_slope_step(data).has_value(); + }); + return step == row.steps.end() ? nullptr : &*step; +} + +SlopeResult ride_on_model(SlopeModel& model, const std::vector& moves){ + const SlopeCalls calls{ + .send = [&model](const std::vector& presses, bool message){ return model.send(presses, message); }, + .log = [](const std::string&){}, + }; + return ride_slope(moves, calls); +} + +// 11.02's slope: from above Route 17's column 14 to the water on Route +// 18's (12, 10), both items found, no Biker's sight entered. +UnitTestResult test_slope_moves(){ + const StoryStep* step = slope_step(checkpoint("11.02")); + if (step == nullptr){ + return "11.02: no slope step."; + } + SlopeModel model(14, -1); + const SlopeResult result = ride_on_model(model, *parse_slope_step(*step)); + if (result.end != SlopeEnd::DONE || result.found != std::vector{"rare-candy", "max-elixir"}){ + return std::format("11.02: the slope ended at move {}, at ({}, {}), {} item(s) found.", result.move + 1, model.x(), model.y(), result.found.size()); + } + if (model.x() != 12 || model.y() != ROUTE18_ROW + 10){ + return std::format("11.02: the slope ended at ({}, {}), not Route 18's (12, 10).", model.x(), model.y()); + } + if (!model.seen().empty()){ + return "11.02: a Biker sees the player at " + model.seen().front(); + } + return true; +} + +// A gap in the controller's queue while B holds the player on the slope +// rolls them: three steps east with B released between them, as separate +// presses with a wait would send them, leave the player rolled down +// column 14 to the water instead of at (17, 29) (red: what the batch +// avoids). +UnitTestResult test_gap_rolls(){ + SlopeModel batched(14, 20); + batched.send(slope_move_presses(SlopeMove{.kind = SlopeMoveKind::STEP, .direction = Direction::EAST, .tiles = 3}), false); + SlopeModel gapped(14, 20); + for (int c = 0; c < 3; c++){ + gapped.send(slope_move_presses(SlopeMove{.kind = SlopeMoveKind::STEP, .direction = Direction::EAST, .tiles = 1}), false); + gapped.send({SlopePress{.hold = Milliseconds(300)}}, false); + } + if (batched.x() != 17 || batched.y() != 20){ + return std::format("B held: ended ({}, {}), expected (17, 20).", batched.x(), batched.y()); + } + if (gapped.y() == 20){ + return "A gap with B released should roll the player."; + } + // Every press of a STEP or SEARCH holds B. + for (SlopeMoveKind kind : {SlopeMoveKind::STEP, SlopeMoveKind::SEARCH}){ + for (const SlopePress& press : slope_move_presses(SlopeMove{.kind = kind, .tiles = 2, .item = "x"})){ + if (!press.b){ + return "A step or search released B."; + } + } + } + return true; +} + +// A brake out of the window (rows 62..81 on column 17) misses the RARE +// CANDY: the search runs out of taps and the step fails. +UnitTestResult test_search_misses(){ + std::vector moves = *parse_slope_step(*slope_step(checkpoint("11.02"))); + moves[3].tiles = 60; + SlopeModel model(14, -1); + const SlopeResult result = ride_on_model(model, moves); + if (result.end != SlopeEnd::NOT_FOUND || result.move != 5){ + return std::format("A brake past row 83: ended {}, move {}.", int(result.end), result.move + 1); + } + return true; +} + +UnitTestResult test_parse(){ + if (parse_slope_move("step east") || parse_slope_move("roll 0") || parse_slope_move("search south 3") + || parse_slope_move("jump 2") || !parse_slope_move("search south 3 rare-candy") + ){ + return "Slope moves parsed wrongly."; + } + return true; +} + +// 11.02's routes: Route 16 through the gate to the slope's top, and from +// Route 18's water to the Safari Zone's door, the slope taken as declared. +UnitTestResult test_routes(){ + const StoryCheckpoint& row = checkpoint("11.02"); + StoryPose at = *story_pose(Pokemon::pose_of(*row.start)); + std::unique_ptr sim = std::make_unique(story_maps(), MapPosition{at.map, at.tile}, *at.facing); + for (const StoryStep& step : row.steps){ + if (step.action == SLOPE_VARIANT){ + at = *story_pose(Pokemon::pose_of(*step.pose_after)); + sim = std::make_unique(story_maps(), MapPosition{at.map, at.tile}, *at.facing); + continue; + } + if (step.kind != Pokemon::StoryStepKind::ROUTE){ + continue; + } + if (const std::string failure = row_step_on_sim(*sim, at, step, row.id); !failure.empty()){ + return std::format("11.02: ended on {}; {}", to_string(sim->state().position), failure); + } + } + const MapPosition end{"FuchsiaCity", {24, 6}}; + if (!(sim->state().position == end)){ + return std::format("11.02: ended on {}, not {}.", to_string(sim->state().position), to_string(end)); + } + return true; +} + +class Test_AutoStorySlope : public UnitTest{ +public: + Test_AutoStorySlope(std::string name, std::function body) + : UnitTest(std::format("PokemonFRLG::AutoStory - Simulation: {}", name)) + , m_body(std::move(body)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + try{ + return m_body(); + }catch (Exception& e){ + return std::format("{}: {}", e.name(), e.message()); + } + } + +private: + std::function m_body; +}; + +} // namespace + + +void add_tests_AutoStorySlope(UnitTestDatabase& database){ + database.add("11.02 down the Cycling Road's slope with both items", test_slope_moves); + database.add("11.02 a queue gap while braking rolls the player", test_gap_rolls); + database.add("11.02 a brake past the RARE CANDY fails the search", test_search_misses); + database.add("11.02 slope moves parse", test_parse); + database.add("11.02 Route 16 to the slope, Route 18 to the Safari Zone", test_routes); +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable.cpp index 092a229670..9402a4f78c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStepTable.cpp @@ -17,6 +17,8 @@ #include "PokemonFRLG_AutoStoryMart.h" #include "PokemonFRLG_AutoStoryMember.h" #include "PokemonFRLG_AutoStoryPcStorage.h" +#include "PokemonFRLG_AutoStorySlope.h" +#include "PokemonFRLG_AutoStorySlopeRun.h" #include "PokemonFRLG_AutoStoryTeach.h" #include "PokemonFRLG_AutoStoryStepTable.h" #include "PokemonFRLG_AutoStoryTrashCans.h" @@ -214,6 +216,7 @@ std::span story_action_specs(){ for (Pokemon::StoryActionSpec spec : { pc_action_spec(), capture_action_spec(), fossil_action_spec(), level_action_spec(), mart_action_spec(), teach_action_spec(), grind_action_spec(), grind_check_action_spec(), + slope_action_spec(), count_items_action_spec(), }){ specs.push_back(std::move(spec)); } 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 9a07073af1..bd92ed21ef 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp @@ -282,8 +282,8 @@ UnitTestResult check_checkpoint(const StoryCheckpoint& checkpoint, StoryPose& po // before leaves the player, and adds the plans to `golden`. UnitTestResult chain_story_routes(std::vector& golden){ const std::vector& checkpoints = story_checkpoints(); - if (checkpoints.size() != 48 || checkpoints[0].start){ - return "Expected 48 checkpoints, the first from a new game."; + if (checkpoints.size() != 49 || checkpoints[0].start){ + return "Expected 49 checkpoints, the first from a new game."; } StoryPose pose; for (size_t c = 0; c < checkpoints.size(); c++){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-11.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-11.02.plan.json new file mode 100644 index 0000000000..4355920e89 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-11.02.plan.json @@ -0,0 +1,81 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "frlg", + "checkpoint": { + "id": "11.02", + "title": "Down the Cycling Road (Route 17) with its RARE CANDY and MAX ELIXIR, to the Safari Zone's door in Fuchsia City", + "start": {"map": "Route16", "tiles": [[28, 13]], "facing": "west"}, + "events": [ + { + "id": "count_items_before", + "kind": "game_action", + "action": "count_items", + "args": {"items": ["rare-candy", "max-elixir"]}, + "why": "the Items pocket's counts before the slope, on flat ground: the items found on it are confirmed against them on Route 18" + }, + { + "id": "top_of_cycling_road", + "kind": "route", + "action": "bike", + "args": { + "target": {"map": "Route16", "tiles": [[14, 19]], "facing": "south"} + }, + "why": "west through the Cycling Road gate (27, 13) to (20, 13), on the Bicycle (both gates keep a rider on it), past the six Bikers (LOOK_AROUND, sight 1: no way past out of every one's sight; one that sees the player is fought) and down to the slope's top, above Route 17's column 14" + }, + { + "id": "down_the_slope", + "kind": "game_action", + "action": "slope", + "args": { + "moves": [ + "step south 1", + "roll 30", + "step east 3", + "brake 42", + "step east 1", + "search south 21 rare-candy", + "step east 1", + "roll 55", + "step west 3", + "step south 2", + "step west 2", + "search south 19 max-elixir", + "step west 2", + "roll 4", + "step south 12" + ] + }, + "declared_after": {"map": "Route18", "tiles": [[12, 10]], "facing": "south"}, + "why": "Route 17 has no map (mapless): onto the slope at (14, 0), rolled to the water (14, 29); 3 east to (17, 29), rolled and braked in rows 62..81 (past Zeek's row 61, above Corey), 1 east to column 18, south a tap and an A at a time until the hidden RARE CANDY (18, 83) answers at (18, 82); 1 east, rolled to the wall (19, 136); 3 west to (16, 136), 2 south through the gap (14..16, 137), 2 west to (14, 138), south a tap and an A at a time until the hidden MAX ELIXIR (14, 157) answers at (14, 156); 2 west to (12, 156), rolled through the ledges' gap (12, 158) to the slope's foot (12, 159), and south onto Route 18 to the water (12, 10)" + }, + { + "id": "count_items_after", + "kind": "game_action", + "action": "count_items", + "args": {"items": ["rare-candy", "max-elixir"], "found": true}, + "why": "each one more than before the slope: both items were found" + }, + { + "id": "safari_zone_door", + "kind": "route", + "action": "bike", + "args": { + "target": {"map": "FuchsiaCity", "tiles": [[24, 6]], "facing": "north"} + }, + "why": "east along Route 18 through its gate (Route18_EastEntrance_1F) into Fuchsia City and up to below the Safari Zone's door (24, 5); pokeguy goes straight there, no Center" + } + ] + }, + "execution": { + "menu": "full", + "lead": "after_brock", + "notes": { + "checkpoint": [ + "From east of the Cycling Road gate (11.01), pokeguy's way down Route 17: the hidden RARE CANDY (18, 83) and MAX ELIXIR (14, 157) only; the FULL RESTORE, PP UP and MAX REVIVE are left, and no Biker is fought by choice (pokefirered Route17 map.json). His brief START menu near the Bikers is left out: it changes nothing.", + "The slope's speeds and B's brake are not measured on the console yet (SlopeMechanics in PokemonFRLG_AutoStorySlope.h): measure them on Route 17 before a counted run.", + "The save is in front of the Safari Zone's door, FUCHSIA CITY on the save card." + ] + } + }, + "metadata": {"phase": "testing"} +} diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 7e3796e78f..941ebfd8a2 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2100,6 +2100,11 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryLavender_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTower_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySnorlax_Tests.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMartBuys.h From 4abdba7df03f3e39cdc70e3b7ee1750891ba8fc5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 02:14:29 -0800 Subject: [PATCH 3/7] PokemonFRLG: 11.02 legs past the Bikers, a wall re-anchor after the slope, slope battles fought and ridden on Route 16's six look-around Bikers: the way past them is one-tile legs where any of them may see the player, so after a battle the walk knows where it was stopped ("Route 3: only the planned trainers' lines" green). After the slope, up a tile, west to the wall and down to the water find the player from a tile either way before the gate's door (the 11.02 scene end pose sweep is green and off the known-failing list). A Biker who catches the player on the slope is fought, and the ride goes on from the roll that pulls them to a known stop where only steps south come between; caught on a step east or west, a brake or a search, the step still fails. The Fuchsia leg keeps the east side: Cut trees close the middle, noted in the plan. Slope timings stay unmeasured guesses. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonFRLG_AutoStoryFaultSweep_Tests.cpp | 2 - .../PokemonFRLG_AutoStoryPlanGolden11.h | 13 ++-- .../AutoStory/PokemonFRLG_AutoStorySlope.cpp | 62 +++++++++++++++-- .../AutoStory/PokemonFRLG_AutoStorySlope.h | 32 +++++++-- .../PokemonFRLG_AutoStorySlopeRun.cpp | 34 +++++++--- .../AutoStory/PokemonFRLG_AutoStorySlopeRun.h | 5 +- .../PokemonFRLG_AutoStorySlope_Tests.cpp | 68 +++++++++++++++++-- .../AutoStory/StoryData/frlg-11.02.plan.json | 29 ++++++-- 8 files changed, 206 insertions(+), 39 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFaultSweep_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFaultSweep_Tests.cpp index 40e7f4bd7b..08b6eb0537 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFaultSweep_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryFaultSweep_Tests.cpp @@ -119,8 +119,6 @@ const std::map KNOWN_FAILING{ "below_fuji_house plans from (4, 7), where Mr. Fuji's script warps the player (warp 4, 7, exact); from a tile off, no wall in his house funnels the walk onto (3, 4): legs that re-anchor, if the console shows the warp can land elsewhere"}, {"PokemonFRLG::AutoStory - Fault sweep: 11.01, warp no-op", "fly_celadon: a Fly that doesn't leave Lavender Town goes unseen, and east_of_snorlax walks from Lavender to Route 16 by Routes 8 and 7 instead: check the fly lands on its town's tile (FlyTown::landing) before the step ends"}, - {"PokemonFRLG::AutoStory - Fault sweep: 11.02, scene end pose", - "down_the_slope is mapless and ends against Route 18's water by its column's count alone; a tile off, safari_zone_door misses the gate's door (41, 9): a leg after the slope that re-anchors the column on a wall, once the console shows how far the slope's taps can be off"}, }; // The land encounter rates of the maps the story walks (pokefirered diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden11.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden11.h index 77f70f6cf8..ba9e256a21 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden11.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden11.h @@ -30,16 +30,13 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN_11[] = { "11.01 walk west 20 -> Route16 (32, 13) [connection 5]", "11.01 east_of_cycling_road_gate from Route16 (32, 13) facing west to Route16 (28, 13) facing west", "11.01 walk west 4 -> Route16 (28, 13)", - "11.02 top_of_cycling_road from Route16 (28, 13) facing west to Route16 (14, 19) facing south", + "11.02 east_of_bikers from Route16 (28, 13) facing west to Route16 (17, 13) facing west", "11.02 walk west 2 (warp) -> Route16_NorthEntrance_1F (11, 12)", "11.02 walk west 11 (warp) -> Route16 (20, 13)", - "11.02 walk west 7 -> Route16 (13, 13)", - "11.02 walk south 6 -> Route16 (13, 19)", - "11.02 walk east 1 (to wall) -> Route16 (14, 19)", - "11.02 face south -> Route16 (14, 19)", - "11.02 safari_zone_door from Route18 (12, 10) facing south to FuchsiaCity (24, 6) facing north", - "11.02 walk north 1 -> Route18 (12, 9)", - "11.02 walk east 30 (warp) -> Route18_EastEntrance_1F (1, 6)", + "11.02 walk west 3 -> Route16 (17, 13)", + "11.02 safari_zone_door from Route18 (8, 10) facing south to FuchsiaCity (24, 6) facing north", + "11.02 walk north 1 -> Route18 (8, 9)", + "11.02 walk east 34 (warp) -> Route18_EastEntrance_1F (1, 6)", "11.02 walk east 11 (warp) -> Route18 (48, 9)", "11.02 walk east 18 (to wall) -> FuchsiaCity (6, 19) [connection 12]", "11.02 walk south 6 (to wall) -> FuchsiaCity (6, 25)", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp index c50b41a0ab..74ba9deb84 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp @@ -170,25 +170,77 @@ std::vector close_message_presses(){ return presses; } +namespace{ + +// A move that keeps the player's column: a step south. +bool keeps_column(const SlopeMove& move){ + return move.kind == SlopeMoveKind::STEP && move.direction == Pokemon::Direction::SOUTH; +} + +// The move a batch's `press` falls in: `ends` holds, for each move of the +// batch from `first`, the press after its last. +size_t move_at_press(const std::vector& ends, size_t first, size_t press){ + for (size_t c = 0; c < ends.size(); c++){ + if (press < ends[c]){ + return first + c; + } + } + return first + ends.size() - 1; +} + +} // namespace + +std::optional slope_resume_move(const std::vector& moves, size_t move){ + size_t next = move; + while (next < moves.size() && keeps_column(moves[next])){ + next++; + } + if (next < moves.size()){ + return moves[next].kind == SlopeMoveKind::ROLL ? std::optional(next) : std::nullopt; + } + for (size_t back = move; back-- > 0;){ + if (moves[back].kind == SlopeMoveKind::ROLL){ + return back; + } + if (!keeps_column(moves[back])){ + return std::nullopt; + } + } + return std::nullopt; +} + SlopeResult ride_slope(const std::vector& moves, const SlopeCalls& calls){ SlopeResult result; std::vector batch; + std::vector ends; size_t first = 0; - for (size_t m = 0; m < moves.size(); m++){ + size_t battles = 0; + for (size_t m = first; m < moves.size(); m++){ const std::vector presses = slope_move_presses(moves[m]); batch.insert(batch.end(), presses.begin(), presses.end()); + ends.push_back(batch.size()); const bool search = moves[m].kind == SlopeMoveKind::SEARCH; if (!search && m + 1 < moves.size()){ continue; } calls.log(std::format("Slope: moves {} to {}, {} presses.", first + 1, m + 1, batch.size())); - const SlopeSent sent = calls.send(batch, search); + const SlopeSendResult sent = calls.send(batch, search); + const size_t under_way = move_at_press(ends, first, sent.press); batch.clear(); + ends.clear(); first = m + 1; - if (sent == SlopeSent::BATTLE){ - return SlopeResult{.end = SlopeEnd::BATTLE, .found = result.found, .move = m}; + if (sent.sent == SlopeSent::BATTLE){ + const bool won = calls.fight(); + const std::optional resume = slope_resume_move(moves, under_way); + if (!won || !resume || ++battles > SLOPE_MAX_BATTLES){ + return SlopeResult{.end = SlopeEnd::BATTLE, .found = result.found, .move = under_way}; + } + calls.log(std::format("Slope: a battle in move {}; on from move {}, rolled to its stop.", under_way + 1, *resume + 1)); + first = *resume; + m = first - 1; + continue; } - if (search && sent != SlopeSent::MESSAGE){ + if (search && sent.sent != SlopeSent::MESSAGE){ return SlopeResult{.end = SlopeEnd::NOT_FOUND, .found = result.found, .move = m}; } if (search){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h index 7c789cc8f8..c518af9b38 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h @@ -121,19 +121,42 @@ enum class SlopeSent{ MESSAGE, BATTLE, }; +// What `send` of SlopeCalls reports. +struct SlopeSendResult{ + SlopeSent sent = SlopeSent::ALL; + // BATTLE: the press under way when it started, from 0. + size_t press = 0; +}; // The console side of ride_slope(): `send` sends `presses` back to back, -// watching for a message only when `message` is set, and for a battle. +// watching for a message only when `message` is set, and for a battle; +// `fight` fights a battle that cut a batch short, back to the overworld, +// and tells whether it was won. struct SlopeCalls{ - std::function& presses, bool message)> send; + std::function& presses, bool message)> send; + std::function fight; std::function log; }; +// The most battles one ride goes on from. +inline constexpr size_t SLOPE_MAX_BATTLES = 3; +// After a battle that cut `moves[move]` short ("if a Biker catches us, +// fight", user 2026-11-12): the ROLL to go on from, which pulls the player +// down the column they were caught in to a stop the moves know. Only +// STEP south moves may come between the two, which keep the column: on +// to the next ROLL, or, with only those left, back to the last one +// (rolled again past its stop, the player stays where they are, and the +// steps south after it end at the same water). Nothing if a step east or +// west, a brake or a search comes first: the column or the row is not +// known, and the ride stops. +std::optional slope_resume_move(const std::vector& moves, size_t move); + enum class SlopeEnd{ DONE, // A search's taps all went with no message: the item isn't where the // moves before it left the player. NOT_FOUND, - // A battle cut the presses short; where it left the player is unknown. + // A battle cut the presses short where slope_resume_move() can't + // find the player again, was lost, or came after SLOPE_MAX_BATTLES. BATTLE, }; struct SlopeResult{ @@ -144,7 +167,8 @@ struct SlopeResult{ size_t move = 0; }; // Rides `moves`: the presses up to and including each search in one -// batch, and from its message on with the next. +// batch, and from its message on with the next. After a battle, fights it +// and goes on from slope_resume_move(). SlopeResult ride_slope(const std::vector& moves, const SlopeCalls& calls); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp index 9ccac26812..4e334cf2a2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp @@ -42,15 +42,29 @@ Button slope_buttons(const SlopePress& press){ return buttons; } +// The press under way `elapsed` after the first of `presses` began. +size_t press_at(const std::vector& presses, Milliseconds elapsed){ + Milliseconds end(0); + for (size_t c = 0; c < presses.size(); c++){ + end += presses[c].hold; + if (elapsed < end){ + return c; + } + } + return presses.empty() ? 0 : presses.size() - 1; +} + // Sends `presses` back to back, one batch with no wait between, watching -// for a battle and, if `message`, for a message. -SlopeSent send_slope_presses(SlopeEnv& env, const std::vector& presses, bool message){ +// for a battle and, if `message`, for a message. A battle's press is the +// one under way by the time since the batch began. +SlopeSendResult send_slope_presses(SlopeEnv& env, const std::vector& presses, bool message){ WhiteDialogWatcher opened(COLOR_RED); BattleScreenWatcher battle(COLOR_RED); std::vector watchers{battle}; if (message){ watchers.push_back(opened); } + const WallClock start = current_time(); const int seen = run_until( env.console, env.context, [&presses](ProControllerContext& context){ @@ -62,9 +76,10 @@ SlopeSent send_slope_presses(SlopeEnv& env, const std::vector& press watchers ); if (seen == 0){ - return SlopeSent::BATTLE; + const Milliseconds elapsed = std::chrono::duration_cast(current_time() - start); + return SlopeSendResult{.sent = SlopeSent::BATTLE, .press = press_at(presses, elapsed)}; } - return seen == 1 ? SlopeSent::MESSAGE : SlopeSent::ALL; + return SlopeSendResult{.sent = seen == 1 ? SlopeSent::MESSAGE : SlopeSent::ALL}; } std::string slope_end_name(SlopeEnd end){ @@ -81,14 +96,15 @@ Pokemon::Outcome ride(SlopeEnv& env, const StoryStep& step, const std::vector& presses, bool message){ return send_slope_presses(env, presses, message); }, + .fight = [&env]{ + env.console.log("Slope: a Biker's battle; fighting it.", COLOR_ORANGE); + const BattleEnd end = fight_battle(env.console, env.context, env.supplies, BattleKind::TRAINER, env.checks, env.language); + mash_until_overworld(env.console, env.context, SLOPE_BATTLE_EXIT); + return end == BattleEnd::WON; + }, .log = [&env](const std::string& text){ env.console.log(text); }, }; const SlopeResult result = ride_slope(moves, calls); - if (result.end == SlopeEnd::BATTLE){ - env.console.log("Slope: a Biker's battle; fighting it.", COLOR_ORANGE); - fight_battle(env.console, env.context, env.supplies, BattleKind::TRAINER, env.checks, env.language); - mash_until_overworld(env.console, env.context, SLOPE_BATTLE_EXIT); - } if (result.end != SlopeEnd::DONE){ return Pokemon::outcome_failed("slope_" + slope_end_name(result.end), std::format("move {}", result.move + 1)); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.h index 964ffe05f1..ae491c6e85 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.h @@ -45,8 +45,9 @@ struct SlopeEnv{ }; // On the console, a SLOPE_VARIANT or COUNT_ITEMS_VARIANT step; nothing for -// another step. A Biker who catches the player on the slope is fought; -// the step then fails, as where the battle left the player is unknown. +// another step. A Biker who catches the player on the slope is fought, +// and the ride goes on from the stop the slope then pulls them to, where +// slope_resume_move() knows it; elsewhere the step fails. std::optional run_slope_step(SlopeEnv& env, const Pokemon::StoryStep& step); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp index a3b29c86e6..0eac26e80f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp @@ -131,18 +131,29 @@ class SlopeModel{ } } - SlopeSent send(const std::vector& presses, bool message){ - for (const SlopePress& press : presses){ + SlopeSendResult send(const std::vector& presses, bool message){ + for (size_t c = 0; c < presses.size(); c++){ + const SlopePress& press = presses[c]; if (press.dpad){ move(*press.dpad); }else if (!press.b){ roll(press.hold); } + if (m_caught){ + m_caught = false; + return SlopeSendResult{.sent = SlopeSent::BATTLE, .press = c}; + } if (press.a && message && face_item()){ - return SlopeSent::MESSAGE; + return SlopeSendResult{.sent = SlopeSent::MESSAGE}; } } - return SlopeSent::ALL; + return SlopeSendResult{}; + } + + // A Biker catches the player the first time they come onto (x, y): + // the presses stop there, as the battle's start cuts the batch short. + void catch_on(int x, int y){ + m_catch = std::pair{x, y}; } int x() const{ return m_x; } @@ -172,11 +183,15 @@ class SlopeModel{ m_x = x; m_y = y; check_sight(); + if (m_catch == std::pair{m_x, m_y}){ + m_catch.reset(); + m_caught = true; + } return true; } // B released on the slope: a tile south each ROLL_TILE. void roll(Milliseconds hold){ - for (Milliseconds t(0); t + SlopeMechanics::ROLL_TILE <= hold && slope(m_x, m_y); t += SlopeMechanics::ROLL_TILE){ + for (Milliseconds t(0); t + SlopeMechanics::ROLL_TILE <= hold && slope(m_x, m_y) && !m_caught; t += SlopeMechanics::ROLL_TILE){ if (!move(Direction::SOUTH)){ break; } @@ -209,6 +224,8 @@ class SlopeModel{ std::vector m_bikers; std::set> m_biker_tiles; std::vector m_seen; + std::optional> m_catch; + bool m_caught = false; }; const StoryCheckpoint& checkpoint(std::string_view id){ @@ -230,6 +247,7 @@ const StoryStep* slope_step(const StoryCheckpoint& row){ SlopeResult ride_on_model(SlopeModel& model, const std::vector& moves){ const SlopeCalls calls{ .send = [&model](const std::vector& presses, bool message){ return model.send(presses, message); }, + .fight = []{ return true; }, .log = [](const std::string&){}, }; return ride_slope(moves, calls); @@ -299,6 +317,43 @@ UnitTestResult test_search_misses(){ return true; } +// A Biker who catches the player on (x, y) is fought, and the ride goes +// on from the stop the slope pulls them to (user, 2026-11-12: "if a +// Biker catches us, fight"): caught rolling down column 14 or on Route +// 18's last steps south, it still finds both items and ends on Route +// 18's (12, 10). +UnitTestResult check_caught_and_done(int x, int y){ + SlopeModel model(14, -1); + model.catch_on(x, y); + const SlopeResult result = ride_on_model(model, *parse_slope_step(*slope_step(checkpoint("11.02")))); + if (result.end != SlopeEnd::DONE || result.found != std::vector{"rare-candy", "max-elixir"}){ + return std::format("Caught on ({}, {}): ended {} at move {}, at ({}, {}), {} item(s) found.", x, y, int(result.end), result.move + 1, model.x(), model.y(), result.found.size()); + } + if (model.x() != 12 || model.y() != ROUTE18_ROW + 10){ + return std::format("Caught on ({}, {}): ended at ({}, {}), not Route 18's (12, 10).", x, y, model.x(), model.y()); + } + return true; +} +UnitTestResult test_caught_rolling(){ + return check_caught_and_done(14, 10); +} +UnitTestResult test_caught_on_route18(){ + return check_caught_and_done(12, ROUTE18_ROW + 5); +} + +// Caught part-way along a step east, the column the slope then pulls the +// player down is not known: the ride stops at that move (move 3, "step +// east 3"), never guessing. +UnitTestResult test_caught_stepping_east(){ + SlopeModel model(14, -1); + model.catch_on(16, 29); + const SlopeResult result = ride_on_model(model, *parse_slope_step(*slope_step(checkpoint("11.02")))); + if (result.end != SlopeEnd::BATTLE || result.move != 2){ + return std::format("Caught on (16, 29): ended {} at move {}, expected a battle at move 3.", int(result.end), result.move + 1); + } + return true; +} + UnitTestResult test_parse(){ if (parse_slope_move("step east") || parse_slope_move("roll 0") || parse_slope_move("search south 3") || parse_slope_move("jump 2") || !parse_slope_move("search south 3 rare-candy") @@ -359,6 +414,9 @@ void add_tests_AutoStorySlope(UnitTestDatabase& database){ database.add("11.02 down the Cycling Road's slope with both items", test_slope_moves); database.add("11.02 a queue gap while braking rolls the player", test_gap_rolls); database.add("11.02 a brake past the RARE CANDY fails the search", test_search_misses); + database.add("11.02 caught rolling down column 14, fought, on to both items", test_caught_rolling); + database.add("11.02 caught on Route 18's last steps, fought, on to the water", test_caught_on_route18); + database.add("11.02 caught part-way along a step east, the ride stops", test_caught_stepping_east); database.add("11.02 slope moves parse", test_parse); database.add("11.02 Route 16 to the slope, Route 18 to the Safari Zone", test_routes); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-11.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-11.02.plan.json index 4355920e89..69b034bcf0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-11.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-11.02.plan.json @@ -14,13 +14,25 @@ "why": "the Items pocket's counts before the slope, on flat ground: the items found on it are confirmed against them on Route 18" }, { - "id": "top_of_cycling_road", + "id": "east_of_bikers", "kind": "route", "action": "bike", "args": { - "target": {"map": "Route16", "tiles": [[14, 19]], "facing": "south"} + "target": {"map": "Route16", "tiles": [[17, 13]], "facing": "west"} }, - "why": "west through the Cycling Road gate (27, 13) to (20, 13), on the Bicycle (both gates keep a rider on it), past the six Bikers (LOOK_AROUND, sight 1: no way past out of every one's sight; one that sees the player is fought) and down to the slope's top, above Route 17's column 14" + "why": "west through the Cycling Road gate (27, 13) to (20, 13), on the Bicycle (both gates keep a rider on it), and on to (17, 13), east of the six Bikers' sight" + }, + { + "id": "top_of_cycling_road", + "kind": "legs", + "action": "bike", + "args": { + "legs": [ + {"direction": "west", "tiles": 1}, {"direction": "west", "tiles": 1}, {"direction": "west", "tiles": 1}, {"direction": "west", "tiles": 1}, + {"direction": "south", "tiles": 1}, {"direction": "south", "tiles": 1}, {"direction": "south", "tiles": 4}, {"direction": "east", "tiles": 1} + ] + }, + "why": "the six Bikers (LOOK_AROUND, sight 1) leave no way past out of every one's sight, so each leg that may be seen is one tile, the trainer seeing the player on its end, where the walk goes on from after the battle: along row 13 through Lao's (16, 13) and Hideki's (14, 13) to (13, 13), down column 13 through Hideki's (13, 14) to (13, 15), down to (13, 19) and east to the wall (14, 19), above Route 17's column 14 (pokeguy's way down: no Biker fought by choice)" }, { "id": "down_the_slope", @@ -55,6 +67,15 @@ "args": {"items": ["rare-candy", "max-elixir"], "found": true}, "why": "each one more than before the slope: both items were found" }, + { + "id": "route18_west_wall", + "kind": "legs", + "action": "bike", + "args": { + "legs": [{"direction": "north", "tiles": 1}, {"direction": "west", "tiles": 4}, {"direction": "south", "tiles": 1}] + }, + "why": "the slope's count of taps leaves the player on (12, 10) only if every tap moved a tile: up a tile, west to the wall (8, 9) and down to the water (8, 10) find them from a tile either way, before the gate's door (41, 9) needs the row" + }, { "id": "safari_zone_door", "kind": "route", @@ -62,7 +83,7 @@ "args": { "target": {"map": "FuchsiaCity", "tiles": [[24, 6]], "facing": "north"} }, - "why": "east along Route 18 through its gate (Route18_EastEntrance_1F) into Fuchsia City and up to below the Safari Zone's door (24, 5); pokeguy goes straight there, no Center" + "why": "east along Route 18 through its gate (Route18_EastEntrance_1F) into Fuchsia City and up to below the Safari Zone's door (24, 5); pokeguy goes straight there, no Center. Without Cut, Fuchsia's east side is the only way from Route 18 to the door: the Cut trees (24, 22) and (21, 13) close the middle (pokefirered FuchsiaCity map.json), and pokeguy's 31 s from the MAX ELIXIR to the Safari Zone's menu (1:12:53 to 1:13:24) fit a ride round the east side better than two Cuts" } ] }, From c6a4cdf426100ffa473b200a832633a60249147b Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 07:02:29 -0800 Subject: [PATCH 4/7] FRLG 11.02: slope model reads presses a frame at a time, as bike.c Red: the first console run's failure (move 6, the RARE CANDY's search, no message) on the model: with B held a tap is a 16-frame walking step, so 200ms taps lose every other tap and every search's A. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonFRLG_AutoStorySlope_Tests.cpp | 109 +++++++++++++----- 1 file changed, 82 insertions(+), 27 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp index 0eac26e80f..759ed3e2b9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp @@ -12,6 +12,7 @@ */ #include +#include #include #include #include @@ -118,7 +119,13 @@ int dy_of(Direction direction){ return direction == Direction::SOUTH ? 1 : direction == Direction::NORTH ? -1 : 0; } -// Route 17 with the player on the Bicycle. +// Route 17 with the player on the Bicycle, a frame at a time as +// pokefirered's bike.c reads the controller: only between movements. +// On a pull-down tile, with B released the player rolls south +// (PlayerWalkFaster, 4 frames), turning to face south between tiles; with +// B held a D-pad press is a walking step (PlayerWalkNormal, 16 frames), and +// B alone stops the player. A press that comes while a movement is under +// way is lost, and so is an A pressed then. class SlopeModel{ public: SlopeModel(int x, int y) @@ -134,17 +141,23 @@ class SlopeModel{ SlopeSendResult send(const std::vector& presses, bool message){ for (size_t c = 0; c < presses.size(); c++){ const SlopePress& press = presses[c]; - if (press.dpad){ - move(*press.dpad); - }else if (!press.b){ - roll(press.hold); - } - if (m_caught){ - m_caught = false; - return SlopeSendResult{.sent = SlopeSent::BATTLE, .press = c}; - } - if (press.a && message && face_item()){ - return SlopeSendResult{.sent = SlopeSent::MESSAGE}; + const double end = m_clock + std::chrono::duration(press.hold).count(); + bool first = true; + for (; m_clock < end; m_clock += FRAME_MS){ + const bool a = press.a && first; + first = false; + if (m_busy > 0){ + m_busy--; + continue; + } + frame(press); + if (m_caught){ + m_caught = false; + return SlopeSendResult{.sent = SlopeSent::BATTLE, .press = c}; + } + if (a && message && face_item()){ + return SlopeSendResult{.sent = SlopeSent::MESSAGE}; + } } } return SlopeSendResult{}; @@ -161,41 +174,79 @@ class SlopeModel{ const std::vector& seen() const{ return m_seen; } private: + static constexpr double FRAME_MS = 1000.0 / 59.7275; + static constexpr int WALK_FRAMES = 16; + static constexpr int ROLL_FRAMES = 4; + static constexpr int FLAT_FRAMES = 6; + static constexpr int JUMP_FRAMES = 32; + + enum class State{ + NORMAL, + SLOPE, + }; + + // bike.c's input handlers, for a frame with no movement under way. + void frame(const SlopePress& press){ + if (!slope(m_x, m_y)){ + m_state = State::NORMAL; + if (press.dpad){ + step(*press.dpad, FLAT_FRAMES); + } + return; + } + if (m_state == State::SLOPE && press.dpad != std::optional(m_moving)){ + // BikeInputHandler_Slope to _Turning: faces the D-pad's way, + // south with none, for this frame. A roll's tiles take 6 + // frames with it. + m_state = State::NORMAL; + m_moving = press.dpad.value_or(Direction::SOUTH); + m_facing = m_moving; + return; + } + if (m_state == State::SLOPE || !press.b){ + m_state = State::SLOPE; + if (!press.dpad || *press.dpad == Direction::SOUTH){ + step(Direction::SOUTH, ROLL_FRAMES); + }else{ + step(*press.dpad, WALK_FRAMES); + } + return; + } + if (press.dpad){ + m_state = State::SLOPE; + step(*press.dpad, WALK_FRAMES); + } + } bool open(int x, int y) const{ return !blocked(x, y) && !m_biker_tiles.contains({x, y}); } - // One tile `direction`; south onto a ledge jumps it. - bool move(Direction direction){ - m_facing = direction; + // One tile `direction`, taking `frames`; south onto a ledge jumps it. + // Nothing against a wall. + void step(Direction direction, int frames){ const int x = m_x + dx_of(direction); const int y = m_y + dy_of(direction); if (ledge(x, y)){ if (direction != Direction::SOUTH){ - return false; + return; } + m_facing = m_moving = direction; m_y = y + 1; + m_busy = JUMP_FRAMES; check_sight(); - return true; + return; } if (!open(x, y)){ - return false; + return; } + m_facing = m_moving = direction; m_x = x; m_y = y; + m_busy = frames; check_sight(); if (m_catch == std::pair{m_x, m_y}){ m_catch.reset(); m_caught = true; } - return true; - } - // B released on the slope: a tile south each ROLL_TILE. - void roll(Milliseconds hold){ - for (Milliseconds t(0); t + SlopeMechanics::ROLL_TILE <= hold && slope(m_x, m_y) && !m_caught; t += SlopeMechanics::ROLL_TILE){ - if (!move(Direction::SOUTH)){ - break; - } - } } bool face_item(){ const auto item = m_items.find({m_x + dx_of(m_facing), m_y + dy_of(m_facing)}); @@ -220,6 +271,10 @@ class SlopeModel{ int m_x; int m_y; Direction m_facing = Direction::SOUTH; + Direction m_moving = Direction::SOUTH; + State m_state = State::NORMAL; + double m_clock = 0; + int m_busy = 0; std::map, std::string> m_items; std::vector m_bikers; std::set> m_biker_tiles; From 38ea320fb1679fe44a3c650b5e3ee52eb6fcb075 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 07:02:29 -0800 Subject: [PATCH 5/7] FRLG 11.02: slope steps wait out the walking step before the next press B alone before a step's or search's first tap leaves the bike's slope state (a tap the other way only turns there), and taps are 360ms apart (16-frame step, a turn frame, two frames of B alone), so no tap and no search A lands while the player moves. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonFRLG_AutoStorySlope.cpp | 2 ++ .../AutoStory/PokemonFRLG_AutoStorySlope.h | 21 +++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp index 74ba9deb84..c37b9d0c26 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp @@ -137,6 +137,7 @@ std::vector slope_move_presses(const SlopeMove& move){ std::vector presses; switch (move.kind){ case SlopeMoveKind::STEP: + presses.push_back(held_b(STEP_SETTLE)); for (size_t c = 0; c < move.tiles; c++){ presses.push_back(SlopePress{.b = true, .dpad = move.direction, .hold = STEP_TAP}); presses.push_back(held_b(STEP_HOLD)); @@ -150,6 +151,7 @@ std::vector slope_move_presses(const SlopeMove& move){ presses.push_back(held_b(BRAKE_SETTLE)); break; case SlopeMoveKind::SEARCH: + presses.push_back(held_b(STEP_SETTLE)); for (size_t c = 0; c < move.tiles; c++){ presses.push_back(SlopePress{.b = true, .dpad = move.direction, .hold = STEP_TAP}); presses.push_back(held_b(STEP_HOLD)); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h index c518af9b38..d3dd3972de 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h @@ -44,8 +44,8 @@ namespace PokemonFRLG{ inline const std::string SLOPE_VARIANT = "slope"; -// Not measured on the console (11.02 has not run yet): each is a guess, -// to be measured on Route 17 before the first run counts. +// Not measured on the console: the steps' timings are from pokefirered's +// bike.c, the rest are guesses, to be measured on Route 17. namespace SlopeMechanics{ // A tile of the slope's pull with B released. pokefirered's downhill // ride is the bike's top speed; on flat ground the Bicycle took 100ms a @@ -58,11 +58,20 @@ inline constexpr Milliseconds ROLL_TILE_SLOWEST(150); // After B is pressed mid-roll, until the player stands: the tile under // way finishes. Whether B stops the player at once is not known. inline constexpr Milliseconds BRAKE_SETTLE(300); -// A D-pad tap with B held: whether it moves exactly a tile on the slope -// is not known (on flat ground a 40ms tap does, BIKE_TAP). +// B alone before a step's or a search's first tap. After a roll the bike +// is in its slope state, where a D-pad press the other way than the +// player moved only turns them (pokefirered bike.c, +// BikeInputHandler_Slope); B alone for a few frames leaves that state. +inline constexpr Milliseconds STEP_SETTLE(100); +// A D-pad tap with B held: a walking step, a tile in 16 frames (bike.c +// BikeTransition_Uphill, PlayerWalkNormal), not the Bicycle's pace. inline constexpr Milliseconds STEP_TAP(40); -// B alone after each tap, start to start BIKE_TAP_PERIOD and more. -inline constexpr Milliseconds STEP_HOLD(160); +// B alone after each tap, until the next tap or a search's A: the step's +// 16 frames (268ms), a frame of turning and two of B alone, with a +// margin. The game reads no press while the player moves: at 200ms +// apart (11.02's first run, 2026-11-12) every other tap and every +// search's A were lost. +inline constexpr Milliseconds STEP_HOLD(320); // A with B held: whether the game takes the A with B down is not known. inline constexpr Milliseconds SEARCH_A(160); // B alone after the A, for the item's message to open. From d069ff0b681b3fbd6381c04658789a4581916648 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 07:03:00 -0800 Subject: [PATCH 6/7] FRLG 11.02: hold STEP_HOLD before a slope step's first tap; no new constant Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp | 4 ++-- .../Programs/AutoStory/PokemonFRLG_AutoStorySlope.h | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp index c37b9d0c26..6a752bf900 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp @@ -137,7 +137,7 @@ std::vector slope_move_presses(const SlopeMove& move){ std::vector presses; switch (move.kind){ case SlopeMoveKind::STEP: - presses.push_back(held_b(STEP_SETTLE)); + presses.push_back(held_b(STEP_HOLD)); for (size_t c = 0; c < move.tiles; c++){ presses.push_back(SlopePress{.b = true, .dpad = move.direction, .hold = STEP_TAP}); presses.push_back(held_b(STEP_HOLD)); @@ -151,7 +151,7 @@ std::vector slope_move_presses(const SlopeMove& move){ presses.push_back(held_b(BRAKE_SETTLE)); break; case SlopeMoveKind::SEARCH: - presses.push_back(held_b(STEP_SETTLE)); + presses.push_back(held_b(STEP_HOLD)); for (size_t c = 0; c < move.tiles; c++){ presses.push_back(SlopePress{.b = true, .dpad = move.direction, .hold = STEP_TAP}); presses.push_back(held_b(STEP_HOLD)); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h index d3dd3972de..101e05dcde 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h @@ -58,11 +58,6 @@ inline constexpr Milliseconds ROLL_TILE_SLOWEST(150); // After B is pressed mid-roll, until the player stands: the tile under // way finishes. Whether B stops the player at once is not known. inline constexpr Milliseconds BRAKE_SETTLE(300); -// B alone before a step's or a search's first tap. After a roll the bike -// is in its slope state, where a D-pad press the other way than the -// player moved only turns them (pokefirered bike.c, -// BikeInputHandler_Slope); B alone for a few frames leaves that state. -inline constexpr Milliseconds STEP_SETTLE(100); // A D-pad tap with B held: a walking step, a tile in 16 frames (bike.c // BikeTransition_Uphill, PlayerWalkNormal), not the Bicycle's pace. inline constexpr Milliseconds STEP_TAP(40); @@ -70,7 +65,10 @@ inline constexpr Milliseconds STEP_TAP(40); // 16 frames (268ms), a frame of turning and two of B alone, with a // margin. The game reads no press while the player moves: at 200ms // apart (11.02's first run, 2026-11-12) every other tap and every -// search's A were lost. +// search's A were lost. Also held before a step's or a search's first +// tap: after a roll the bike is in its slope state, where a press the +// other way than the player moved only turns them (bike.c, +// BikeInputHandler_Slope), and B alone leaves that state. inline constexpr Milliseconds STEP_HOLD(320); // A with B held: whether the game takes the A with B down is not known. inline constexpr Milliseconds SEARCH_A(160); From 5f7be28e421555fdc6ede93679212991f3c490f3 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 07:46:23 -0800 Subject: [PATCH 7/7] FRLG 11.02: count reads Rare Candy; a search's message must open after the batch starts The Items pocket's reader had no Rare Candy: its row read "FARE CANCY", matched CANCEL, and the pocket's read ended above it, so the count saw 0 before and after the slope. The dictionary now has Route 17's hidden items, and a row with a count is never taken for the CANCEL. The slope's message watcher took the RARE CANDY's message, still closing under the next batch's first presses, for the MAX ELIXIR's 0.4 s later, and cut that search short. SlopeMessageGate counts a message only once it shows after a frame without one; the slope's model now holds the player on a message until its pages are closed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Menus/PokemonFRLG_ItemListReader.cpp | 67 +++++++++++++++++-- .../AutoStory/PokemonFRLG_AutoStorySlope.h | 6 +- .../PokemonFRLG_AutoStorySlopeMessage.h | 59 ++++++++++++++++ .../PokemonFRLG_AutoStorySlopeRun.cpp | 23 ++++++- .../PokemonFRLG_AutoStorySlope_Tests.cpp | 32 ++++++++- SerialPrograms/cmake/SourceFiles.cmake | 1 + 6 files changed, 177 insertions(+), 11 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeMessage.h diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp index e0af276247..74b8907a9c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp @@ -6,10 +6,12 @@ #include #include +#include #include #include "Common/Cpp/Filesystem/Filesystem.h" #include "Common/Cpp/Json/JsonArray.h" #include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/TestRunners/UnitTest.h" #include "CommonFramework/GlobalAutoPaths.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonFramework/Language.h" @@ -46,6 +48,15 @@ class ItemNameMatcher : public OCR::SmallDictionaryMatcher{ return matcher; } + std::string match(Language language, const std::string& text) const{ + if (!languages()[language]){ + return ""; + } + OCR::StringMatchResult result = match_substring(language, text, MAX_LOG10P_SPREAD); + result.clear_beyond_log10p(MAX_LOG10P); + return result.results.empty() ? "" : result.results.begin()->second.token; + } + std::string read(Logger& logger, Language language, const ImageViewRGB32& image) const{ if (!languages()[language]){ return ""; @@ -80,6 +91,12 @@ class ItemNameMatcher : public OCR::SmallDictionaryMatcher{ {"x-accuracy", "X Accuracy"}, {"guard-spec", "Guard Spec."}, {"bicycle", "Bicycle"}, + // Route 17's hidden items. Without them a RARE CANDY read + // "FARE CANCY" and matched CANCEL: the pocket's read ended + // above it, and 11.02 counted no RARE CANDY in a bag with 2 + // (console B, 2026-11-12). + {"rare-candy", "Rare Candy"}, + {"max-elixir", "Max Elixir"}, {"cancel", "Cancel"}, }){ JsonArray names; @@ -96,6 +113,16 @@ ImageViewRGB32 game_box(const ImageViewRGB32& screen){ return extract_box_reference(screen, GameSettings::instance().GAME_BOX); } +// A row from its name's and its count's reads: a row with a count that +// matched CANCEL is an item whose name can't be read, as the list's +// CANCEL shows no count. +BagRow bag_row(std::string item, int count){ + if (item == "cancel"){ + return count > 0 ? BagRow{"", count} : BagRow{std::move(item), 0}; + } + return BagRow{std::move(item), count}; +} + // Where row `row` of the bag shows its item count. ImageFloatBox bag_item_count_box(int row){ return ImageFloatBox(208.0 / 240, (ROW_TOP + ROW_STEP * row) / 160, 15.0 / 240, ROW_HEIGHT / 160); @@ -113,16 +140,13 @@ std::string read_item_list_name(Logger& logger, Language language, const ImageVi } BagRow read_bag_row(Logger& logger, Language language, const ImageViewRGB32& screen, int shown){ - const std::string item = read_item_list_name(logger, language, screen, shown); - if (item == "cancel"){ - return BagRow{item, 0}; - } + std::string item = read_item_list_name(logger, language, screen, shown); const ImageViewRGB32 count = extract_box_reference(game_box(screen), bag_item_count_box(shown)); // Not Tesseract on each waterfilled digit: the font joins some strokes // only at a corner, so the 5 and the 3 fell apart into pieces and read // as nothing (console B, 2026-11-02). The stat boxes' templates are // the same digits on the same cream. - return BagRow{item, read_digits_waterfill_template(logger, count, DigitTemplateType::StatBox)}; + return bag_row(std::move(item), read_digits_waterfill_template(logger, count, DigitTemplateType::StatBox)); } std::vector read_bag_pocket(Logger& logger, Language language, const ImageViewRGB32& screen){ @@ -186,6 +210,38 @@ class Test_BagPocket : public ImageUnitTest{ } }; +// 11.02's counts (console B, 2026-11-12): OCR read the RARE CANDY's row +// "FARE CANCY", which matched CANCEL with no Rare Candy in the dictionary, +// and the pocket's read ended above it. A row with a count is never the +// CANCEL, whatever its name matched. +class Test_Route17ItemNames : public UnitTest{ +public: + Test_Route17ItemNames() + : UnitTest("PokemonFRLG::ItemListReader - 11.02 the RARE CANDY's row is not the CANCEL") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const ItemNameMatcher& matcher = ItemNameMatcher::instance(); + for (const std::string& text : {"FARE CANCY", "RARE CANOY", "RARE CANDY"}){ + const std::string item = matcher.match(Language::English, text); + if (item != "rare-candy"){ + return std::format("Expected \"{}\" read as rare-candy, got \"{}\".", text, item); + } + } + if (matcher.match(Language::English, "MAX ELIXIR") != "max-elixir" || matcher.match(Language::English, "CANCEL") != "cancel"){ + return "Expected MAX ELIXIR and CANCEL read as themselves."; + } + const BagRow counted = bag_row("cancel", 2); + if (!counted.item.empty() || counted.count != 2){ + return std::format("Expected a CANCEL with 2 read as an unread item x2, got \"{}\" x{}.", counted.item, counted.count); + } + if (bag_row("cancel", -1).item != "cancel"){ + return "Expected a CANCEL with no count kept as the CANCEL."; + } + return true; + } +}; + // A capture of a Mart's BUY list, read from the top to its CANCEL. Its // golden file holds each row's item, one per line. class Test_MartList : public ImageUnitTest{ @@ -228,6 +284,7 @@ void add_tests_ItemListReader(UnitTestDatabase& database){ database.add("PokemonFRLG/ItemListReader/ItemsPocketMoonStone_eng.png"); // The Viridian Mart's BUY list, the same day, the cursor on POTION. database.add("PokemonFRLG/ItemListReader/MartBuyList_eng.png"); + database.add(); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h index 101e05dcde..ca95105248 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h @@ -135,9 +135,9 @@ struct SlopeSendResult{ size_t press = 0; }; // The console side of ride_slope(): `send` sends `presses` back to back, -// watching for a message only when `message` is set, and for a battle; -// `fight` fights a battle that cut a batch short, back to the overworld, -// and tells whether it was won. +// watching for a message (SlopeMessageGate) only when `message` is set, +// and for a battle; `fight` fights a battle that cut a batch short, back +// to the overworld, and tells whether it was won. struct SlopeCalls{ std::function& presses, bool message)> send; std::function fight; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeMessage.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeMessage.h new file mode 100644 index 0000000000..19de322f9f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeMessage.h @@ -0,0 +1,59 @@ +/* AutoStory Slope Message + * + * From: https://github.com/PokemonAutomation/ + * + * When a search on Route 17's slope (PokemonFRLG_AutoStorySlope.h) takes + * a message for its item's, shared by the console's watcher and the + * slope's model in the tests. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStorySlopeMessage_H +#define PokemonAutomation_PokemonFRLG_AutoStorySlopeMessage_H + +#include +#include "Common/Cpp/Time.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// Whether a search's message opened, from whether each frame of a batch +// shows one. A message already showing when the batch began is the last +// item's, still closing under the batch's first presses: taken for the +// MAX ELIXIR's 0.4 s after the RARE CANDY's opened, it cut that search +// short before its first tap, and the MAX ELIXIR was never picked up +// (console B, 2026-11-12). A message counts once it has shown for HOLD +// after a frame without one. +class SlopeMessageGate{ +public: + // As WhiteDialogWatcher holds its dialog. + static constexpr Milliseconds HOLD = Milliseconds(250); + + bool opened(bool shows, WallClock now){ + if (!shows){ + m_clear_seen = true; + m_since.reset(); + return false; + } + if (!m_clear_seen){ + return false; + } + if (!m_since){ + m_since = now; + } + return now - *m_since >= HOLD; + } + +private: + bool m_clear_seen = false; + std::optional m_since = std::nullopt; +}; + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp index 4e334cf2a2..00161d8a05 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp @@ -7,6 +7,7 @@ #include #include #include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.h" #include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h" @@ -17,6 +18,7 @@ #include "PokemonFRLG_AutoStoryItemsPocket.h" #include "PokemonFRLG_AutoStoryPresses.h" #include "PokemonFRLG_AutoStorySlope.h" +#include "PokemonFRLG_AutoStorySlopeMessage.h" #include "PokemonFRLG_AutoStorySlopeRun.h" namespace PokemonAutomation{ @@ -54,11 +56,30 @@ size_t press_at(const std::vector& presses, Milliseconds elapsed){ return presses.empty() ? 0 : presses.size() - 1; } +// A white dialog that opens during a batch (SlopeMessageGate). +class SlopeMessageWatcher : public VisualInferenceCallback{ +public: + SlopeMessageWatcher() + : VisualInferenceCallback("SlopeMessageWatcher") + , m_detector(COLOR_RED) + {} + virtual void make_overlays(VideoOverlaySet& items) const override{ + m_detector.make_overlays(items); + } + virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override{ + return m_gate.opened(m_detector.detect(frame), timestamp); + } + +private: + WhiteDialogDetector m_detector; + SlopeMessageGate m_gate; +}; + // Sends `presses` back to back, one batch with no wait between, watching // for a battle and, if `message`, for a message. A battle's press is the // one under way by the time since the batch began. SlopeSendResult send_slope_presses(SlopeEnv& env, const std::vector& presses, bool message){ - WhiteDialogWatcher opened(COLOR_RED); + SlopeMessageWatcher opened; BattleScreenWatcher battle(COLOR_RED); std::vector watchers{battle}; if (message){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp index 759ed3e2b9..f190bba61d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp @@ -30,6 +30,7 @@ #include "PokemonFRLG_AutoStoryRowSim.h" #include "PokemonFRLG_AutoStoryRows_Tests.h" #include "PokemonFRLG_AutoStorySlope.h" +#include "PokemonFRLG_AutoStorySlopeMessage.h" #include "PokemonFRLG_AutoStoryTable.h" namespace PokemonAutomation{ @@ -138,7 +139,11 @@ class SlopeModel{ } } + // A found item's message holds the player until its pages are closed + // with A, and the watcher sees it as the console's does + // (SlopeMessageGate), from the next batch's first presses on too. SlopeSendResult send(const std::vector& presses, bool message){ + SlopeMessageGate gate; for (size_t c = 0; c < presses.size(); c++){ const SlopePress& press = presses[c]; const double end = m_clock + std::chrono::duration(press.hold).count(); @@ -146,6 +151,13 @@ class SlopeModel{ for (; m_clock < end; m_clock += FRAME_MS){ const bool a = press.a && first; first = false; + if (message && gate.opened(m_pages > 0, now())){ + return SlopeSendResult{.sent = SlopeSent::MESSAGE}; + } + if (m_pages > 0){ + m_pages -= a ? 1 : 0; + continue; + } if (m_busy > 0){ m_busy--; continue; @@ -155,8 +167,8 @@ class SlopeModel{ m_caught = false; return SlopeSendResult{.sent = SlopeSent::BATTLE, .press = c}; } - if (a && message && face_item()){ - return SlopeSendResult{.sent = SlopeSent::MESSAGE}; + if (a && face_item()){ + m_pages = MESSAGE_PAGES; } } } @@ -172,6 +184,7 @@ class SlopeModel{ int x() const{ return m_x; } int y() const{ return m_y; } const std::vector& seen() const{ return m_seen; } + const std::vector& picked() const{ return m_picked; } private: static constexpr double FRAME_MS = 1000.0 / 59.7275; @@ -179,6 +192,12 @@ class SlopeModel{ static constexpr int ROLL_FRAMES = 4; static constexpr int FLAT_FRAMES = 6; static constexpr int JUMP_FRAMES = 32; + // "found a ..." and "put the ... in the ITEMS POCKET". + static constexpr int MESSAGE_PAGES = 2; + + WallClock now() const{ + return WallClock() + std::chrono::duration_cast(std::chrono::duration(m_clock)); + } enum class State{ NORMAL, @@ -253,6 +272,7 @@ class SlopeModel{ if (item == m_items.end()){ return false; } + m_picked.push_back(item->second); m_items.erase(item); return true; } @@ -279,6 +299,8 @@ class SlopeModel{ std::vector m_bikers; std::set> m_biker_tiles; std::vector m_seen; + std::vector m_picked; + int m_pages = 0; std::optional> m_catch; bool m_caught = false; }; @@ -320,6 +342,12 @@ UnitTestResult test_slope_moves(){ if (result.end != SlopeEnd::DONE || result.found != std::vector{"rare-candy", "max-elixir"}){ return std::format("11.02: the slope ended at move {}, at ({}, {}), {} item(s) found.", result.move + 1, model.x(), model.y(), result.found.size()); } + // 11.02's second run (console B, 2026-11-12): the RARE CANDY's message, + // still closing, was taken for the MAX ELIXIR's, which was never + // picked up, and the ride went on from the RARE CANDY's tile. + if (model.picked() != std::vector{"rare-candy", "max-elixir"}){ + return std::format("11.02: {} item(s) picked up, not both.", model.picked().size()); + } if (model.x() != 12 || model.y() != ROUTE18_ROW + 10){ return std::format("11.02: the slope ended at ({}, {}), not Route 18's (12, 10).", model.x(), model.y()); } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 941ebfd8a2..36cdc1ecbf 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2102,6 +2102,7 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySnorlax_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeMessage.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlopeRun.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySlope_Tests.cpp