diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor.cpp index 6a76bb6c76..9801e7a4d7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor.cpp @@ -456,7 +456,8 @@ std::optional plan_reanchor( const std::vector& maps, const std::vector& candidates, const RouteStep& step, - const RouteCosts& costs + const RouteCosts& costs, + std::optional end_facing ){ if (candidates.empty() || step.action != RouteAction::WALK){ return std::nullopt; @@ -478,9 +479,9 @@ std::optional plan_reanchor( const Direction facing = at.holds.empty() ? step.direction : at.holds.back().direction; // The way back keeps clear of every trainer who may stand in it if // it can, and must walk the same whether they do or not. - std::optional> back = plan_overworld_route(clear_of_trainers, at.at, RouteGoal{step.end, std::nullopt}, costs, facing); + std::optional> back = plan_overworld_route(clear_of_trainers, at.at, RouteGoal{step.end, end_facing}, costs, facing); if (!back && !trainers.empty()){ - back = plan_overworld_route(maps, at.at, RouteGoal{step.end, std::nullopt}, costs, facing); + back = plan_overworld_route(maps, at.at, RouteGoal{step.end, end_facing}, costs, facing); } if (!back || (!trainers.empty() && !walks_same_everywhere(worlds, positions, at.at, *back))){ continue; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor.h index bcc65bffb7..e32947523a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor.h @@ -92,12 +92,16 @@ struct Reanchor{ // the player, the one that walks the fewest tiles, holds and way back // together, with tiles of open legs weighted over held ones. Nothing if // no holds bring them all to one tile, or no way back walks the same from -// there. +// there. With `end_facing`, the way back ends facing that way: the route's +// last leg must leave the player facing as planned, or a step after it only +// turns them (16.03's rival_trigger, a tap west after a way back that ended +// "north 1 (to wall)" beside the trigger; console B, 2026-11-15 00:28). std::optional plan_reanchor( const std::vector& maps, const std::vector& candidates, const RouteStep& step, - const RouteCosts& costs = default_route_costs() + const RouteCosts& costs = default_route_costs(), + std::optional end_facing = std::nullopt ); // Holds to walls that bring the player to one tile from every one of diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp index 72aef14d38..7de8a16c30 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp @@ -777,7 +777,7 @@ class RouteRun{ if (candidates.size() < 2 || (!last && is_wall_hold(m_plan[m_next]) && hold_finds(*maps(), candidates, m_plan[m_next]))){ return; } - const std::optional anchor = plan_reanchor(*maps(), candidates, step, m_planning->costs); + const std::optional anchor = plan_reanchor(*maps(), candidates, step, m_planning->costs, last ? std::optional(step.facing_after) : std::nullopt); if (water_chain && (!anchor || anchor->holds.empty())){ m_calls.fail(std::format( "walk_overworld_route(): {} on the water may have ended short with nothing seen, and {} after it is open too; " @@ -871,7 +871,7 @@ class RouteRun{ } const std::optional anchor = candidates.empty() ? std::nullopt - : plan_reanchor(*maps(), candidates, step, m_planning->costs); + : plan_reanchor(*maps(), candidates, step, m_planning->costs, m_next + 1 == m_plan.size() ? std::optional(step.facing_after) : std::nullopt); if (!anchor){ m_calls.fail( "walk_overworld_route(): stopped part-way on " + to_string(step) 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 4ee0b188f1..732ea35c9a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp @@ -295,6 +295,46 @@ UnitTestResult test_rival_row(){ return true; } +// 16.03 to_rival_trigger from 7F's pad (5, 4): "west 2 (open)" to (3, 4) +// ends where a hold can't tell a tile short from none, so the walker finds +// the player at walls and walks back, its last leg "north 1 (to wall)". The +// route must still end facing west, as planned: the rival_trigger step's +// tap west then only turned the player, who never stepped onto the +// trigger (2, 4) (pokefirered data/maps/SilphCo_7F/map.json, coord_events +// (2, 4) and (2, 5), SilphCo_7F_EventScript_RivalTriggerTop) and the +// rival's battle never came (console B, 2026-11-15 00:28, 16.03 run 5). +UnitTestResult test_rival_trigger_walk_ends_facing_west(){ + const std::vector maps = maps_after_16_02(); + const MapPosition pad{"SilphCo_7F", {5, 4}}; + const MapPosition beside_trigger{"SilphCo_7F", {3, 4}}; + const std::optional> route = plan_overworld_route( + maps, pad, RouteGoal{beside_trigger, Direction::WEST}, story_route_costs(), Direction::WEST + ); + if (!route || route->empty()){ + return "No route from 7F's pad to (3, 4)."; + } + OverworldSim sim(maps, pad, Direction::WEST); + OverworldSimWalker walker(sim); + const RoutePlanning planning{.maps = &maps, .costs = story_route_costs(), .start = pad, .label = "to_rival_trigger"}; + if (const std::string failure = walker.run(*route, planning); !failure.empty()){ + return std::format("The walk failed: {}", failure); + } + const std::vector& log = walker.log(); + const bool found_at_walls = std::any_of(log.begin(), log.end(), [](const std::string& line){ + return line.find("finding the player at walls") != std::string::npos; + }); + if (!found_at_walls){ + return "Expected the walker to find the player at walls after \"west 2 (open)\", as on the console."; + } + if (!(sim.state().position == beside_trigger) || sim.state().facing != Direction::WEST){ + return std::format( + "Expected {} facing west, got {} facing {}.", + to_string(beside_trigger), to_string(sim.state().position), direction_name(sim.state().facing) + ); + } + return true; +} + // 16.03b's checkpoint: Gift RNG's Lapras, on 7F beside the employee. UnitTestResult test_lapras_handoff(){ const StoryCheckpoint& gift = checkpoint("16.03b"); @@ -487,6 +527,10 @@ const AutoStorySilphCase AUTO_STORY_SILPH_CASES[] = { .input = {.name = "16.03 3F's Card Key door, the pad to 7F and the rival; 16.03b to the employee", .body = test_rival_row}, .expectation = {.description = "opens 3F's door_2, ends by the employee on 7F"}, }, + { + .input = {.name = "16.03 the walk to the rival's trigger, found again at walls", .body = test_rival_trigger_walk_ends_facing_west}, + .expectation = {.description = "after the holds and the way back, on (3, 4) facing west, the trigger (2, 4) a tap ahead"}, + }, { .input = {.name = "16.03b/16.04 the Lapras by Gift RNG", .body = test_lapras_handoff}, .expectation = {.description = "16.03b hands off to Gift RNG for a Lapras; 16.04 is the live save"},