From ac0bcf014b456190ed78e412bdc4db9732f8ee0c Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 22:34:55 -0800 Subject: [PATCH 1/5] FRLG sim: a tap off an exit mat its way takes the warp from any facing Console B, 2026-11-14 22:27, 15.01 west_of_tea_guard: the turn tap east on Route 7's gate door (15, 10), a walk_east warp, took the warp; its fade passed while the turn was read, and the hold and the two steps more walked on inside the gate. The sim's turn tap only turned. Now a tap or hold off an exit mat its way takes the warp with no turn first. New Saffron sim case from the gate's wall facing north is red: no fade in and out after walking onto the warp. --- .../Overworld/PokemonFRLG_OverworldSim.cpp | 10 ++--- .../PokemonFRLG_AutoStorySaffron_Tests.cpp | 41 +++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp index 56aea09510..4e90c23c30 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp @@ -488,8 +488,8 @@ void OverworldSim::walk_hold(Direction direction, Milliseconds duration, int sho const Milliseconds end = m_now + duration; Milliseconds pressed = m_now; if (m_state.facing != direction){ - // Holding through a turn lost a tile on the console, on foot. - pressed += m_state.on_bike ? Milliseconds(0) : tile; + // Holding through a turn lost a tile on the console, on foot; off an exit mat its way, none. + pressed += m_state.on_bike || exit_mat(map(), m_player.position) == direction ? Milliseconds(0) : tile; m_state.facing = direction; } take_steps(direction, pressed, end, (int)((end - pressed + tile - 1ms) / tile) - short_by); @@ -544,9 +544,9 @@ void OverworldSim::tap(Direction direction, Milliseconds duration, Milliseconds hold_on(direction, start + duration); }else if (m_state.on_bike && drops_bike_tap(start)){ m_last_tap.reset(); - }else if (m_state.facing != direction && !m_state.on_bike){ - // From a standstill a tap only turns the player, even one as long - // as LONGEST_TAP. Not on the Bicycle: there it steps. + }else if (m_state.facing != direction && !m_state.on_bike && exit_mat(map(), m_player.position) != direction){ + // From a standstill a tap only turns the player, even one as long as LONGEST_TAP. Not on the Bicycle: there it steps. + // Nor off an exit mat its way: there it takes the warp (console B, 2026-11-14: 14.02b Cinnabar Gym, 15.01 Route 7's gate). m_last_tap = TapPress{direction, start, start + duration, m_state.facing, 0}; if (drops_turn_tap()){ m_state.turn_taps_dropped++; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaffron_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaffron_Tests.cpp index 63f9ee7f47..cb28448a99 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaffron_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaffron_Tests.cpp @@ -148,6 +148,43 @@ UnitTestResult test_route7_gate_running_crossing_not_held_up(){ return true; } +// 15.01 west_of_tea_guard as console B ran it (2026-11-14 22:27, run 3): +// from the gate's wall (15, 11) facing north, "north 1" onto the door's +// tile (15, 10), an exit mat taken east, then "east 1 (warp)". The turn +// tap east took the warp, its fade passed while the turn was read, and the +// hold and the two steps more walked on inside the gate: "no fade in and +// out after walking onto the warp". +UnitTestResult test_route7_gate_door_turn_tap(){ + const StoryCheckpoint& row = checkpoint("15.01"); + StoryPose at{"Route7", {15, 11}, Direction::NORTH}; + SimScript script; + script.timing.tile = MovementMechanics::RUN_TILE_TIME; + OverworldSim sim(story_maps(), MapPosition{at.map, at.tile}, *at.facing, script); + SimWalkerRules rules; + rules.speed = MoveSpeed::RUN; + for (const StoryStep& step : row.steps){ + if (step.name != "west_of_tea_guard"){ + continue; + } + const std::optional> route = walked_route(at, step, story_maps(), sim.state().gates); + if (!route || route->empty()){ + return std::format("15.01 {}: no route from {}.", step.name, to_string(MapPosition{at.map, at.tile})); + } + const RoutePlanning planning{ + .maps = &story_maps(), .gates = sim.state().gates, .costs = story_route_costs(), + .start = MapPosition{at.map, at.tile}, .label = step.name + }; + if (const std::string failure = OverworldSimWalker(sim, rules).run(*route, planning); !failure.empty()){ + return std::format("15.01 {}: ended on {}; {}", step.name, to_string(sim.state().position), failure); + } + } + const MapPosition west_of_trigger{"Route7_EastEntrance", {5, 4}}; + if (!(sim.state().position == west_of_trigger)){ + return std::format("15.01: ended on {}, not west of the guard's trigger, {}.", to_string(sim.state().position), to_string(west_of_trigger)); + } + return true; +} + const AutoStorySaffronCase AUTO_STORY_SAFFRON_CASES[] = { { .input = {.name = "15.01 Route 7's gate with the TEA to Saffron's Center", .body = test_route7_to_saffron}, @@ -157,6 +194,10 @@ const AutoStorySaffronCase AUTO_STORY_SAFFRON_CASES[] = { .input = {.name = "15.01 running, the crossing onto Route 7 not held up", .body = test_route7_gate_running_crossing_not_held_up}, .expectation = {.description = "a column long after the crossing, the walk still ends at the gate's guard"}, }, + { + .input = {.name = "15.01 west_of_tea_guard from the gate's wall, the door an exit mat", .body = test_route7_gate_door_turn_tap}, + .expectation = {.description = "the press east off the door's tile takes the warp with no turn tap first; ends west of the guard's trigger"}, + }, }; } // namespace From b9d9ad52fbd597baf04ed68e647d40cd1273f368 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 22:34:55 -0800 Subject: [PATCH 2/5] FRLG walker: no turn tap on a warp leg off an exit mat RouteStep::off_exit_mat marks a warp leg that starts on an exit mat and goes its way; turns_first() skips the turn tap there, so the hold takes the warp and its fade is watched along the hold. The fade watcher and arrived_without_fade() were fine: the warp was taken before the leg's 'before' screen, which then showed the gate itself. --- .../PokemonFRLG/Overworld/PokemonFRLG_OverworldBike.h | 3 ++- .../PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp | 1 + .../Overworld/PokemonFRLG_OverworldRouteSteps.h | 7 +++++++ .../PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.cpp | 2 +- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldBike.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldBike.h index d1ab0be7fa..c182735e3d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldBike.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldBike.h @@ -31,7 +31,8 @@ namespace OverworldWalkSteps{ // Whether a WALK `step` at `speed` starts with a turn tap, for a player // facing `facing`: on foot facing another way (or an unknown way), so the // hold starts facing the right way. Never on the Bicycle, where a tap -// takes a step (MovementMechanics::BIKE_TAP_ONLY_TURNS). +// takes a step (MovementMechanics::BIKE_TAP_ONLY_TURNS). Never off an exit +// mat its way (RouteStep::off_exit_mat), where the tap takes the warp. bool turns_first(const RouteStep& step, MoveSpeed speed, std::optional facing); // Whether `step` at `speed` is walked a tap at a time, each a tile diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp index 57bebf820c..126d41dea0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp @@ -595,6 +595,7 @@ RouteStep to_route_step(const std::vector& maps, const Pokemon::Leg tiles = leg_tiles(maps, MapPosition{maps[leg.start.map].name, leg.start.state.position}, step); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteSteps.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteSteps.h index b5c926edb2..544c8bfb04 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteSteps.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteSteps.h @@ -86,6 +86,13 @@ struct RouteStep{ // floors, where a hold that ends a tile off slides the player // elsewhere (console B, 2026-11-10, 09.01 r1). bool tapped = false; + // WALK onto a warp: the leg starts on an exit mat (walk_) + // and goes its way, so the first press takes the warp from any facing, + // and the leg has no turn tap (turns_first()). The turn tap east on + // Route 7's gate door (15, 10) took the warp, its fade passed while the + // turn was read, and the hold walked on inside the gate (console B, + // 2026-11-14 22:27, 15.01 west_of_tea_guard). + bool off_exit_mat = false; // WALK, SURF: a wild Pokemon can appear on the way: the step crosses // tall grass or water. Elsewhere (indoors, streets) no wild battle can // start, so the walker doesn't watch for one. diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.cpp index 0f396fff3a..ed5265c02d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.cpp @@ -244,7 +244,7 @@ Milliseconds tile_time(const RouteStep& step, MoveSpeed speed){ } bool turns_first(const RouteStep& step, MoveSpeed speed, std::optional facing){ - if (facing == step.direction){ + if (facing == step.direction || step.off_exit_mat){ return false; } return step.surfing || step_speed(step, speed) != MoveSpeed::BIKE || BIKE_TAP_ONLY_TURNS; From d1b2c8b51e3f2ec624098f3fa1fde911b2a38d91 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 23:18:36 -0800 Subject: [PATCH 3/5] FRLG 09.01: arrive on B3F's stairs from B4F facing east, no turn west B3F's (15, 18) is MB_DOWN_LEFT_STAIR_WARP: the stair exit leaves the player facing east, and pokefirered's TryArrowWarp takes a stair warp from a held press its way, the same as an arrow warp. The plan's turn west there took the stairs back down to B4F on the sim since the exit mat change; the target now keeps the arrival facing. --- .../Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden.h | 3 +-- .../Programs/AutoStory/StoryData/frlg-09.01.plan.json | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden.h index 5ad12437d4..369cafaf4c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden.h @@ -837,9 +837,8 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN[] = { "09.01 face west -> RocketHideout_B4F (5, 3)", "09.01 below_lift_key from RocketHideout_B4F (3, 3) facing west to RocketHideout_B4F (3, 3) facing north", "09.01 face north -> RocketHideout_B4F (3, 3)", - "09.01 b3f_from_b4f from RocketHideout_B4F (11, 15) facing south to RocketHideout_B3F (15, 18) facing west", + "09.01 b3f_from_b4f from RocketHideout_B4F (11, 15) facing south to RocketHideout_B3F (15, 18) facing east", "09.01 walk east 1 (warp) -> RocketHideout_B3F (15, 18)", - "09.01 face west -> RocketHideout_B3F (15, 18)", "09.01 beside_b3f_rare_candy from RocketHideout_B3F (10, 13) facing north to RocketHideout_B3F (12, 13) facing north", "09.01 walk east 2 (to wall) -> RocketHideout_B3F (12, 13)", "09.01 face north -> RocketHideout_B3F (12, 13)", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-09.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-09.01.plan.json index d55ff51ed7..82544454f4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-09.01.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-09.01.plan.json @@ -164,9 +164,9 @@ "kind": "route", "action": "run", "args": { - "target": {"map": "RocketHideout_B3F", "tiles": [[15, 18]], "facing": "west"} + "target": {"map": "RocketHideout_B3F", "tiles": [[15, 18]], "facing": "east"} }, - "why": "east up B4F's stairs (11, 15) to B3F's (15, 18)" + "why": "east up B4F's stairs (11, 15) to B3F's (15, 18), arriving facing east: a turn west there would take the stairs back down (pokefirered TryArrowWarp: a held press the stairs' way warps)" }, { "id": "b3f_down_to_row_20", From a3a5c6506c430e0ec4b4f65b954dfb084b007ce7 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 23:54:01 -0800 Subject: [PATCH 4/5] FRLG 16.03: a beaten trainer stands where he walked up, not on his spawn tile Console B run 4 (2026-11-14 23:24): 16.03's "west 11" along SilphCo_5F row 21 stopped two tiles short of the pad, with the Rocket 16.02 had just beaten standing on (11, 21). He saw the player waiting on (12, 21) and walked up from his map tile (9, 21); the game leaves a beaten trainer where he stopped. The planner and the row sim kept him on (9, 21), so the route ran through the tile he actually blocks. A run flag, TRAINER_AT_FLAG ("trainer_at:=,"), records where a trainer stands after his fight; 16.02's battle sets it and 16.03 starts with it. run_story_maps() applies the moved trainers to the maps the run plans on (PokemonFRLG_AutoStoryMovedTrainers), and the row sim's walk plans on run_story_maps(flags) as the program does, instead of story_maps(). The Silph rows' sim stands the beaten Rocket on (11, 21), where the sim's trainer_sees leaves him (red: 16.03 blocked on (12, 21); green with the flag), and a new case checks the flag data and maps. The golden plan for 16.03 goes west 9, north 1, west 2 onto the pad. --- .../PokemonFRLG_AutoStoryMovedTrainers.cpp | 81 +++++++++++++++++++ .../PokemonFRLG_AutoStoryMovedTrainers.h | 59 ++++++++++++++ .../PokemonFRLG_AutoStoryPacedRoute.cpp | 38 ++++++++- .../PokemonFRLG_AutoStoryPlanGolden16.h | 5 +- .../AutoStory/PokemonFRLG_AutoStoryRowSim.cpp | 11 ++- .../AutoStory/PokemonFRLG_AutoStoryRowSim.h | 6 +- .../PokemonFRLG_AutoStorySilph_Tests.cpp | 65 ++++++++++++++- .../AutoStory/StoryData/frlg-16.02.plan.json | 5 +- .../AutoStory/StoryData/frlg-16.03.plan.json | 3 +- SerialPrograms/cmake/SourceFiles.cmake | 2 + 10 files changed, 257 insertions(+), 18 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMovedTrainers.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMovedTrainers.h diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMovedTrainers.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMovedTrainers.cpp new file mode 100644 index 0000000000..92579cfeb0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMovedTrainers.cpp @@ -0,0 +1,81 @@ +/* AutoStory Moved Trainers + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldObjects.h" +#include "PokemonFRLG_AutoStoryMovedTrainers.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +namespace{ + +// "," of a TRAINER_AT_FLAG, or nothing if it isn't that. +std::optional> parse_tile(std::string_view text){ + const size_t comma = text.find(','); + if (comma == std::string_view::npos){ + return std::nullopt; + } + int x = 0; + int y = 0; + const std::string_view x_text = text.substr(0, comma); + const std::string_view y_text = text.substr(comma + 1); + const std::from_chars_result x_read = std::from_chars(x_text.data(), x_text.data() + x_text.size(), x); + const std::from_chars_result y_read = std::from_chars(y_text.data(), y_text.data() + y_text.size(), y); + if (x_read.ec != std::errc() || x_read.ptr != x_text.data() + x_text.size() + || y_read.ec != std::errc() || y_read.ptr != y_text.data() + y_text.size()){ + return std::nullopt; + } + return std::pair{x, y}; +} + +} // namespace + + +std::vector moved_trainers(const std::set& run_flags){ + std::vector moved; + for (const std::string& flag : run_flags){ + if (!flag.starts_with(TRAINER_AT_FLAG)){ + continue; + } + const std::string_view rest = std::string_view(flag).substr(TRAINER_AT_FLAG.size()); + const size_t equals = rest.find('='); + const std::optional> tile = equals == std::string_view::npos ? std::nullopt : parse_tile(rest.substr(equals + 1)); + if (!tile || equals == 0){ + throw ParseException(std::format("run flag \"{}\" isn't \"{}=,\"", flag, TRAINER_AT_FLAG)); + } + moved.emplace_back(TrainerMove{.trainer = std::string(rest.substr(0, equals)), .x = tile->first, .y = tile->second}); + } + return moved; +} + +std::vector with_trainers_moved(std::vector maps, const std::vector& moved){ + for (const TrainerMove& move : moved){ + for (MapData& map : maps){ + for (Pokemon::MapObject& object : map.objects){ + if (!object.trainer || trainer_id(map, object) != move.trainer){ + continue; + } + object.x = move.x; + object.y = move.y; + object.wander.reset(); + object.trainer.reset(); + } + } + } + return maps; +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMovedTrainers.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMovedTrainers.h new file mode 100644 index 0000000000..e357cbde9b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMovedTrainers.h @@ -0,0 +1,59 @@ +/* AutoStory Moved Trainers + * + * From: https://github.com/PokemonAutomation/ + * + * Trainers who walked up to the player, and stand there from then on. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryMovedTrainers_H +#define PokemonAutomation_PokemonFRLG_AutoStoryMovedTrainers_H + +#include +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_MapData.h" + + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +// The maps a story is planned on (Pokemon_MapData.h). +using Pokemon::MapData; + + +// A trainer who saw the player walked up to the tile beside them and +// fought: the game leaves them standing there, not on the map's tile. The +// trainer (trainer_id(), from the map's tile) and where they stand now. +struct TrainerMove{ + std::string trainer; + int x = 0; + int y = 0; +}; + +// A run flag that says where a trainer stands since they walked up to the +// player: this prefix, then the trainer's trainer_id(), "=", the tile +// ("trainer_at:SilphCo_5F/rocket_m@9,21=11,21"). The step that fought +// them sets it in flags_after and the checkpoints after start with it: +// the Rocket of 16.02 walked from 5F's (9, 21) up to (11, 21) and stood +// across 16.03's leg along row 21, which the planner had through his +// map tile's neighbour (console B, 2026-11-14, run 4). +inline constexpr std::string_view TRAINER_AT_FLAG = "trainer_at:"; + +// The trainers `run_flags` says walked up to the player (TRAINER_AT_FLAG). +// A flag that doesn't parse throws ParseException. +std::vector moved_trainers(const std::set& run_flags); + +// `maps` with each trainer of `moved` standing on its new tile, no longer +// a trainer: they fought the player when they walked up, and they stop +// no one. A trainer not found is left as is. +std::vector with_trainers_moved(std::vector maps, const std::vector& moved); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPacedRoute.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPacedRoute.cpp index 1118e9182d..ec4b6d3b24 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPacedRoute.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPacedRoute.cpp @@ -4,9 +4,13 @@ * */ +#include +#include #include #include +#include "Common/Cpp/Concurrency/Mutex.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.h" +#include "PokemonFRLG_AutoStoryMovedTrainers.h" #include "PokemonFRLG_AutoStoryPacedRoute.h" namespace PokemonAutomation{ @@ -46,12 +50,38 @@ const std::vector& story_maps(){ return maps; } +namespace{ + +// story_maps() without the Rockets (SILPH_CLEARED_FLAG). +const std::vector& cleared_story_maps(){ + static const std::vector maps = make_story_maps(true); + return maps; +} + +} // namespace + const std::vector& run_story_maps(const std::set& run_flags){ - if (run_flags.contains(std::string(SILPH_CLEARED_FLAG))){ - static const std::vector cleared = make_story_maps(true); - return cleared; + const bool cleared = run_flags.contains(std::string(SILPH_CLEARED_FLAG)); + const std::vector& base = cleared ? cleared_story_maps() : story_maps(); + const std::vector moved = moved_trainers(run_flags); + if (moved.empty()){ + return base; + } + // One copy per set of trainers moved, kept while the program runs: + // callers hold references into it. `run_flags` is a set, so the key + // lists the moves in one order. + std::string key = cleared ? "cleared" : "all"; + for (const TrainerMove& move : moved){ + key += std::format("|{}={},{}", move.trainer, move.x, move.y); + } + static Mutex lock; + static std::map> moved_maps; + LockGuard guard(lock); + auto it = moved_maps.find(key); + if (it == moved_maps.end()){ + it = moved_maps.emplace(key, with_trainers_moved(base, moved)).first; } - return story_maps(); + return it->second; } std::set beaten_trainers(const std::set& run_flags){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden16.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden16.h index 8d7a4c7ad6..632818e157 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden16.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden16.h @@ -40,8 +40,9 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN_16[] = { "16.02 walk east 4 (warp) -> SilphCo_5F (10, 20)", "16.02 walk east 2 (to wall) -> SilphCo_5F (12, 20)", "16.03 to_3f_trainer from SilphCo_5F (21, 21) facing east to SilphCo_3F (22, 9) facing south", - "16.03 walk west 11 -> SilphCo_5F (10, 21)", - "16.03 walk north 1 (warp) -> SilphCo_9F (22, 18)", + "16.03 walk west 9 -> SilphCo_5F (12, 21)", + "16.03 walk north 1 -> SilphCo_5F (12, 20)", + "16.03 walk west 2 (warp) -> SilphCo_9F (22, 18)", "16.03 walk west 4 (to wall) -> SilphCo_9F (18, 18)", "16.03 walk east 4 (warp) -> SilphCo_5F (10, 20)", "16.03 walk north 12 -> SilphCo_5F (10, 8)", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.cpp index df81425fc8..b6379e390b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.cpp @@ -459,6 +459,7 @@ Pokemon::StoryAction OverworldStepSim::step_action(StoryActionId id){ return m_stubs.stub(); } return played([this, body](const StoryStep& step, RunState& state){ + m_flags = state.flags; m_beaten = beaten_trainers(state.flags); const std::string failure = (this->*body)(step); return failure.empty() ? Pokemon::outcome_ok() : Pokemon::outcome_failed("sim_failure", failure); @@ -545,10 +546,12 @@ void sim_wait_out_warp(OverworldSim& sim){ // A ROUTE or LEGS by the sim's walker, fighting a trainer who sees the // player on the way. std::string OverworldStepSim::walk(const StoryStep& step){ - // The story's maps, as the planner and the program have them (the grunt the sim has - // hidden stands on them, and the stairs a switch opens are open); the gates are the sim's, - // which the walker's steps open. - const std::vector& maps = story_maps(); + // The story's maps as the program plans on them with the step's run flags + // (run_story_maps(): a beaten trainer who walked up to the player stands + // where he stopped, the Rockets are gone once Silph Co. is cleared; the + // stairs a switch opens are open); the gates are the sim's, which the + // walker's steps open. + const std::vector& maps = run_story_maps(m_flags); const Pokemon::GateState& gates = m_sim.state().gates; const std::optional> route = walked_route(m_at, step, maps, gates, Pokemon::GateRules{.card_key_held = m_sim.card_key_held()}); if (route && route->empty()){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.h index ebd694d472..7137b89bb1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.h @@ -170,8 +170,10 @@ class OverworldStepSim : public Pokemon::TableExecutor{ OverworldSim& m_sim; StoryPose m_at; - // The trainers the step's run flags say are beaten (beaten_trainers()): - // the walker doesn't watch for them. + // The step's run flags: the maps the program plans on are theirs + // (run_story_maps()), and the walker doesn't watch for the trainers they + // say are beaten (m_beaten, beaten_trainers()). + std::set m_flags; std::set m_beaten; SimStepGuard m_guard; SimStubs m_stubs; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp index 206bd1ae36..af2c2c5b80 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp @@ -23,6 +23,7 @@ #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h" #include "PokemonFRLG_AutoStoryEvents.h" +#include "PokemonFRLG_AutoStoryMovedTrainers.h" #include "PokemonFRLG_AutoStoryPacedRoute.h" #include "PokemonFRLG_AutoStoryRooms.h" #include "PokemonFRLG_AutoStoryRowSim.h" @@ -114,6 +115,7 @@ class RowWalk{ : m_row(checkpoint(id)) , m_at(*story_pose(Pokemon::pose_of(*m_row.start))) , m_sim(maps, MapPosition{m_at.map, m_at.tile}, *m_at.facing, script_of(m_row)) + , m_flags(m_row.start_flags.begin(), m_row.start_flags.end()) {} // Each step in turn; the failure of the first that fails, or empty. @@ -127,9 +129,12 @@ class RowWalk{ m_floors.push_back(m_sim.state().position.map); continue; } - if (const std::string failure = row_step_on_sim(m_sim, m_at, step, m_row.id); !failure.empty()){ + // The run's flags as the program's state has them at the step: the + // row's start flags and what the steps before set (flags_after). + if (const std::string failure = row_step_on_sim(m_sim, m_at, step, m_row.id, m_flags); !failure.empty()){ return failure; } + m_flags.insert(step.flags_after.begin(), step.flags_after.end()); m_floors.push_back(m_sim.state().position.map); } return ""; @@ -164,6 +169,7 @@ class RowWalk{ const StoryCheckpoint& m_row; StoryPose m_at; OverworldSim m_sim; + std::set m_flags; std::vector m_floors; }; @@ -188,10 +194,30 @@ std::vector with_beaten(std::vector maps, std::string_view map return maps; } +// `maps` with the trainer at `at` on `map_name` beaten and standing on +// (`stop_x`, `stop_y`): where he stopped when he walked up to the player +// and sight ended, and where the game leaves him after his fight. +std::vector with_beaten_at(std::vector maps, std::string_view map_name, int x, int y, int stop_x, int stop_y){ + for (MapData& map : maps){ + for (MapObject& object : map.objects){ + if (map.name == map_name && object.x == x && object.y == y){ + object.x = stop_x; + object.y = stop_y; + object.wander.reset(); + object.trainer.reset(); + } + } + } + return maps; +} + // The maps a row after 16.02 starts on: the Rocket at 5F's (9, 21), beaten by -// 16.02 on its way, no longer sees the player (his fight is in the save). +// 16.02 on its way, no longer sees the player (his fight is in the save) and +// stands on (11, 21), where he stopped beside the player who waited on +// (12, 21) for him (console B, 2026-11-14 run 4: he blocked 16.03's walk +// west along row 21). std::vector maps_after_16_02(){ - return with_beaten(story_maps(), "SilphCo_5F", 9, 21); + return with_beaten_at(story_maps(), "SilphCo_5F", 9, 21, 11, 21); } // Row `id` walked on the sim: the steps run, no rule broken, the gates the @@ -418,6 +444,35 @@ UnitTestResult test_master_ball_option(){ return true; } +// The Rocket 16.02 fights walked up to the player from (9, 21) and stopped on +// (11, 21); a beaten trainer stays where he stopped and blocks that tile +// (console B, 2026-11-14 run 4: 16.03's "west 11" ended against him two +// tiles short of the pad). 16.02's battle records where he stands +// (TRAINER_AT_FLAG), 16.03 starts with it, and the maps the run plans on +// (run_story_maps()) have him there, not on his spawn tile. +UnitTestResult test_corridor_rocket_stands_where_he_stopped(){ + const std::string flag = std::string(TRAINER_AT_FLAG) + "SilphCo_5F/rocket_m@9,21=11,21"; + const std::vector& steps = checkpoint("16.02").steps; + const bool set_by_battle = std::any_of(steps.begin(), steps.end(), [&](const StoryStep& step){ + return step.kind == StoryStepKind::BATTLE && std::find(step.flags_after.begin(), step.flags_after.end(), flag) != step.flags_after.end(); + }); + if (!set_by_battle){ + return "16.02 has no battle that sets " + flag + "."; + } + const std::vector& start = checkpoint("16.03").start_flags; + if (std::find(start.begin(), start.end(), flag) == start.end()){ + return "16.03 does not start with " + flag + "."; + } + const std::vector& maps = run_story_maps({flag}); + if (!has_object_at(maps, "SilphCo_5F", 11, 21)){ + return "The run's maps have nobody on SilphCo_5F (11, 21), where the Rocket stopped."; + } + if (has_object_at(maps, "SilphCo_5F", 9, 21)){ + return "The run's maps still have the Rocket on his spawn tile SilphCo_5F (9, 21)."; + } + return true; +} + const AutoStorySilphCase AUTO_STORY_SILPH_CASES[] = { { .input = {.name = "16.01 Silph Co.'s stairs to 3F and its pad to 5F", .body = test_pad_to_5f}, @@ -455,6 +510,10 @@ const AutoStorySilphCase AUTO_STORY_SILPH_CASES[] = { .input = {.name = "16.02 fights the Rocket by the corridor for 16.03's start flag", .body = test_corridor_rocket_fought}, .expectation = {.description = "a trainer_wait step, then his battle with trainer_beaten:SilphCo_5F/rocket_m@9,21 after it; 16.03 starts with the flag"}, }, + { + .input = {.name = "the corridor Rocket stands where he stopped after 16.02", .body = test_corridor_rocket_stands_where_he_stopped}, + .expectation = {.description = "16.02's battle sets trainer_at:SilphCo_5F/rocket_m@9,21=11,21; 16.03 starts with it; the run's maps have him on (11, 21), not (9, 21)"}, + }, { .input = {.name = "silph_cleared is set by the Giovanni step", .body = test_silph_cleared_flag}, .expectation = {.description = "16.05's giovanni sets it; 16.06 starts with it"}, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.02.plan.json index cc9a4bfc96..f88f07c118 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.02.plan.json @@ -61,9 +61,10 @@ ], "facing": "south" }, - "why": "Grunt1 (SilphCo_5F_EventScript_Grunt1): Arbok Lv33; beaten here every run, so 16.03's start flag holds by construction", + "why": "Grunt1 (SilphCo_5F_EventScript_Grunt1): Arbok Lv33; beaten here every run, so 16.03's start flags hold by construction; he walked up two tiles to (11, 21) when he saw the player on (12, 21) and stands there from now on (trainer_at), across row 21 west of the player (console B, 2026-11-14, run 4: 16.03's leg west along row 21 stopped against him)", "flags_after": [ - "trainer_beaten:SilphCo_5F/rocket_m@9,21" + "trainer_beaten:SilphCo_5F/rocket_m@9,21", + "trainer_at:SilphCo_5F/rocket_m@9,21=11,21" ] }, { diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.03.plan.json index 2e661587bc..25897b0b53 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-16.03.plan.json @@ -16,7 +16,8 @@ }, "start_flags": [ "card_key_held", - "trainer_beaten:SilphCo_5F/rocket_m@9,21" + "trainer_beaten:SilphCo_5F/rocket_m@9,21", + "trainer_at:SilphCo_5F/rocket_m@9,21=11,21" ], "events": [ { diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 43e6c5304d..9bfa1408d1 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2262,6 +2262,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMart_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMember.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMember.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMovedTrainers.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryMovedTrainers.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPacedRoute.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPacedRoute.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPaceSim.cpp From 880ab2f3b647086dbcedca2a33980ae5bf2899f4 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 00:11:05 -0800 Subject: [PATCH 5/5] FRLG AutoStory tests: name the flag set before keeping run_story_maps' reference GCC's -Wdangling-reference flagged binding the result to a call with a temporary set. --- .../Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp index af2c2c5b80..4ee0b188f1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp @@ -463,7 +463,8 @@ UnitTestResult test_corridor_rocket_stands_where_he_stopped(){ if (std::find(start.begin(), start.end(), flag) == start.end()){ return "16.03 does not start with " + flag + "."; } - const std::vector& maps = run_story_maps({flag}); + const std::set run_flags{flag}; + const std::vector& maps = run_story_maps(run_flags); if (!has_object_at(maps, "SilphCo_5F", 11, 21)){ return "The run's maps have nobody on SilphCo_5F (11, 21), where the Rocket stopped."; }