diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Sandgem.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Sandgem.cpp index 3ed31c906c..877fcdae15 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Sandgem.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Sandgem.cpp @@ -222,7 +222,7 @@ void mart_lady(VideoStream& stream, ProControllerContext& context){ walk_until_stopped(stream, context, Direction::EAST, 40, "East along the path"); walk_until_stopped(stream, context, Direction::NORTH, 2, "North to the front row"); const StoryWorld& maps = story_maps(); - RouteTrainers trainers{story_trainer_sights(), [&](const std::string& name){ fight_route_trainer(stream, context, name); }}; + RouteTrainers trainers{story_trainer_sights(), [&](const std::string& name){ return fight_route_trainer(stream, context, name); }}; walk_to_route_202_save_spot(stream, context, maps, {maps.to_world(MAP_SANDGEM_TOWN, map_point("SandgemTown", "front_row_east_end"))}, trainers); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp index 84dda41bbf..e8eb07f10d 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp @@ -149,7 +149,7 @@ uint64_t mean_brightness(const ImageViewRGB32& screen){ // Fights a trainer whose dialogue has opened, with whatever starter we // have. Someone who only talks leaves no battle to fight. -void fight_route_trainer(VideoStream& stream, ProControllerContext& context, const std::string& name){ +bool fight_route_trainer(VideoStream& stream, ProControllerContext& context, const std::string& name){ pbf_mash_button(context, BUTTON_B, 5000ms); context.wait_for_all_requests(); BattleMenuWatcher battle_menu(BattleType::TRAINER, COLOR_YELLOW); @@ -158,13 +158,14 @@ void fight_route_trainer(VideoStream& stream, ProControllerContext& context, con stream.log("[Route] " + name + ": no battle followed the talk."); pbf_mash_button(context, BUTTON_B, 2000ms); context.wait_for_all_requests(); - return; + return false; } fight_trainer(stream, context, name); context.wait_for_all_requests(); // The next trainer may be a few tiles on: check HP now. With no // Potion in the Bag this only looks. use_potion_first_pokemon(stream, context, 0.8); + return true; } bool sign_text_matches(const std::string& text, const std::string& key){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.h index b8fae5f225..3851a44744 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.h @@ -52,8 +52,9 @@ bool talk_or_fight( // Fights a trainer whose dialogue has opened, with whatever starter we // have, then a Potion if HP is under half. Someone who only talks leaves -// no battle to fight. How the story's planned walks fight (RouteTrainerFight). -void fight_route_trainer(VideoStream& stream, ProControllerContext& context, const std::string& name); +// no battle to fight; returns whether a battle followed. How the story's +// planned walks fight (RouteTrainerFight). +bool fight_route_trainer(VideoStream& stream, ProControllerContext& context, const std::string& name); // A rough check that we stand in a Pokemon Center: its floor fills the lower // half of the screen with warm yellow, where the gym is grey and routes are diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.cpp index c0d247096b..ad44da5415 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.cpp @@ -72,6 +72,42 @@ class WallPolicy{ }; +// One D-pad tap, a whole tile for every possible position: safe when each +// next tile is open ground on the same map with no one on it. +bool safe_tap(const StoryWorld& world, const TilePos& from, Direction direction){ + const auto map = world.to_local(from); + const auto next = step(world, from, direction); + const auto local = next ? world.to_local(*next) : std::nullopt; + return map && next && *next == from.next(direction) && local && local->map == map->map && standable(world, *next) + && !recovery_hazard(world, *next); +} + +// WallPolicy, with a tap where a hold has no wall for every position: a +// tile with no safe wall any way (someone beside it, a door, the map's +// edge) moves on a tile to where one is. +class WallOrTapPolicy : public WallPolicy{ +public: + static std::optional> hold( + const StoryWorld& world, const std::vector& from, Direction direction + ){ + std::optional> wall = WallPolicy::hold(world, from, direction); + if (wall){ + return wall; + } + std::vector ends; + for (const auto& start : from){ + if (!safe_tap(world, start, direction)){ + return std::nullopt; + } + ends.push_back(start.next(direction)); + } + return Pokemon::WallRecoveryStep{ + .positions = std::move(ends), + .hold = {.direction = direction, .tiles = 1, .ends_at_wall = false, .held = false}, + }; + } +}; + } // namespace std::vector> wall_recovery_candidates( @@ -106,6 +142,21 @@ std::vector interrupted_leg_positions( } namespace{ +// The most holds, taps among them, plan_recovered_route() tries when walls +// alone bring several tiles to no one wall. +constexpr size_t MAX_TAP_RECOVERY_HOLDS = 4; + +std::vector> tap_recovery_candidates( + const StoryWorld& world, const std::vector& from +){ + for (const auto& tile : from){ + if (!standable(world, tile)){ + return {}; + } + } + return Pokemon::find_wall_anchors(from, world, {.max_holds = MAX_TAP_RECOVERY_HOLDS}); +} + std::vector recovery_legs( const StoryWorld& world, std::vector from, const std::vector& holds ){ @@ -113,7 +164,7 @@ std::vector recovery_legs( for (const auto& hold : holds){ std::vector after; for (const auto& at : from){ - const TilePos end = slide_to_wall(world, at, hold.direction).stop; + const TilePos end = hold.ends_at_wall ? slide_to_wall(world, at, hold.direction).stop : at.next(hold.direction); if (std::find(after.begin(), after.end(), end) == after.end()){ after.push_back(end); } @@ -142,19 +193,34 @@ std::optional> plan_recovered_route( // fewest held tiles alone chose "south 12 (to wall), north 12 (open)" // over "north 13 (to wall), south 5 (open)" for 01.10's line 5 (lab-B // 2026-11-08 18:15:47). Ties keep find_wall_anchors()' order. + // Walls alone first; only when they bring the tiles to no one wall, + // D-pad taps where a hold has no wall, and a fourth hold. On lab-B + // (2026-11-12 02:52, 00.05c) a talk on Jubilife's row 24 left (35, 24) + // and (36, 24): from (36, 24) every way ends at no wall (the TV + // station's clown north, a man south, the woman's tile west, the map's + // edge east), and the walk failed with "no plan". Two taps east bring + // both tiles under the TV station's wall. std::optional> best; - long long best_cost = 0; - for (const auto& anchor : wall_recovery_candidates(world, from)){ - const auto back = plan_robust_route(world, std::vector{anchor.at}, goal, costs); - if (!back){ - continue; + for (const bool taps : {false, true}){ + if (best){ + break; } - auto route = recovery_legs(world, from, anchor.holds); - route.insert(route.end(), back->begin(), back->end()); - const long long cost = robust_route_cost(world, from, route, costs); - if (!best || cost < best_cost){ - best = std::move(route); - best_cost = cost; + long long best_cost = 0; + const std::vector> anchors = taps + ? tap_recovery_candidates(world, from) + : wall_recovery_candidates(world, from); + for (const auto& anchor : anchors){ + const auto back = plan_robust_route(world, std::vector{anchor.at}, goal, costs); + if (!back){ + continue; + } + auto route = recovery_legs(world, from, anchor.holds); + route.insert(route.end(), back->begin(), back->end()); + const long long cost = robust_route_cost(world, from, route, costs); + if (!best || cost < best_cost){ + best = std::move(route); + best_cost = cost; + } } } return best; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp index 5590f1f3f1..256dabe73f 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp @@ -156,11 +156,19 @@ struct ScriptedWalk{ std::vector fights; }; -ScriptedWalk walk_script(const StoryWorld& maps, const TilePos& from, const TilePos& goal, std::vector sights, Script& script){ +// What follows each talk that is fought. +enum class AfterTalk{ + BATTLE, + NO_BATTLE, +}; + +ScriptedWalk walk_script(const StoryWorld& maps, const TilePos& from, const TilePos& goal, std::vector sights, Script& script, + AfterTalk after_talk = AfterTalk::BATTLE +){ ScriptedWalk out; RouteTrainers trainers{ .sights = std::move(sights), - .fight = [&out](const std::string& trainer){ out.fights.push_back(trainer); }, + .fight = [&out, after_talk](const std::string& trainer){ out.fights.push_back(trainer); return after_talk == AfterTalk::BATTLE; }, }; ScriptedRouteIO io(script); try{ @@ -207,6 +215,7 @@ UnitTestResult test_wall_leg_trainer_on_the_way(Logger&){ Script script{.steps = { walk_step("wall: T: east 4 (to wall)", WalkStop::LIMIT, 2050ms, 0), walk_step("wall: T: east 4 (to wall): at the wall?", WalkStop::DIALOG, 400ms, 0), + answer_step("dialog within 5000 ms", false), walk_step("wall: T: east 4 (to wall): on to the wall", WalkStop::BLOCKED, 1200ms, 0), answer_step("dialog within 500 ms", false), }}; @@ -229,6 +238,34 @@ UnitTestResult test_wall_leg_trainer_on_the_way(Logger&){ } +// lab-B 2026-11-12 06:12 (00.05c below_the_company_clown, the 6th failure +// there): the Wonder Station's woman stopped the check walk of "west 1 (to +// wall)", the fight saw no battle follow and pressed her talk to its end, +// so nothing showed when we looked again; the walk on to the wall then +// started her talk again and ran out of time. A talk the fight saw end +// with no battle plans again from where it stopped, never walks on. +UnitTestResult test_wall_leg_talk_no_battle(Logger&){ + Script script{.steps = { + walk_step("wall: T: east 4 (to wall)", WalkStop::LIMIT, 2050ms, 0), + walk_step("wall: T: east 4 (to wall): at the wall?", WalkStop::DIALOG, 400ms, 0), + answer_step("dialog within 5000 ms", false), + }}; + try{ + const ScriptedWalk walk = walk_script(make_world({"#.....#"}), {1, 0}, {5, 0}, {}, script, AfterTalk::NO_BATTLE); + if (!walk.failure.empty()){ + return "failed after a talk no battle followed: " + walk.failure; + } + if (walk.at != std::vector{{5, 0}}){ + return "did not plan again from the wall it stopped at, (5, 0)."; + } + }catch (const ScriptMismatch& e){ + return e.message; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + + // A leg to a wall that ran out of time: a check walk that also runs out // of time counts as at the wall only if holding on there changes the // screen less than walking (r127a ran along the wrong row). @@ -671,6 +708,10 @@ const PlannedRouteCase PLANNED_ROUTE_CASES[] = { .input = {"a trainer on a wall leg, then on to the wall", test_wall_leg_trainer_on_the_way}, .expectation = {}, }, + { + .input = {"a talk no battle followed on a wall leg plans again (lab-B 2026-11-12 06:12)", test_wall_leg_talk_no_battle}, + .expectation = {}, + }, { .input = {"a wall leg out of time needs the holding check", test_wall_leg_limit}, .expectation = {}, diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp index 3f8104aae0..4a393b3b21 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp @@ -92,7 +92,7 @@ class RecoveryStepIO : public StoryStepIO{ public: explicit RecoveryStepIO(RouteIO& route) : m_route(route){} virtual RouteIO& route() override{ return m_route; } - virtual void fight(const std::string&) override{ throw GridSimFailure{"unexpected trainer"}; } + virtual bool fight(const std::string&) override{ throw GridSimFailure{"unexpected trainer"}; } virtual void walk_until_stopped(Direction, size_t, const std::string&) override{ throw GridSimFailure{"unexpected blind hold"}; } virtual bool run_until_map(Direction, Milliseconds, const std::string&, const std::string&) override{ throw GridSimFailure{"unexpected banner walk"}; } virtual WarpSeen run_into_door(Direction, bool, Milliseconds = Milliseconds(12000), Milliseconds = Milliseconds(0), const std::string& = "") override{ throw GridSimFailure{"unexpected door"}; } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp index 123a171f38..e786c357c5 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp @@ -197,7 +197,7 @@ UnitTestResult test_blocked_from_several_tiles(Logger&){ const TilePos goal{52, 41}; std::vector notes; SimulatedRouteIO io(world, {53, 41}, {{54, 41}}, notes); - RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){}}; + RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){ return true; }}; try{ walk_route(io, world, from, trainers, { .goal = [goal](const TilePos& pos){ return pos == goal; }, @@ -245,7 +245,7 @@ UnitTestResult test_jubilife_center_held_overshoot(Logger&){ std::vector notes; SimulatedRouteIO io(world, from, {}, notes); io.overshoot_timed_holds(true); - RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){}}; + RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){ return true; }}; try{ walk_route(io, world, {from}, trainers, { .goal = [goal](const TilePos& pos){ return pos == goal; }, @@ -273,7 +273,7 @@ UnitTestResult test_jubilife_center_kids(Logger&){ const TilePos goal{52, 41}; std::vector notes; SimulatedRouteIO io(world, from, {{52, 46}, {54, 46}}, notes); - RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){}}; + RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){ return true; }}; try{ walk_route(io, world, {from}, trainers, { .goal = [goal](const TilePos& pos){ return pos == goal; }, @@ -358,7 +358,7 @@ std::string sweep_story_route(const StoryRoute& route, bool overshoot, std::vect SimulatedRouteIO io(*world, from, {}, notes); io.overshoot_timed_holds(overshoot); io.set_can_run(route.speed == MoveSpeed::RUN); - RouteTrainers trainers{.sights = story_trainers(*world), .fight = [](const std::string&){}}; + RouteTrainers trainers{.sights = story_trainers(*world), .fight = [](const std::string&){ return true; }}; try{ walk_route(io, *world, start, trainers, { .goal = [&](const TilePos& pos){ return std::find(targets.begin(), targets.end(), pos) != targets.end(); }, diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp index 1c09aa7543..cb22e7d4c3 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp @@ -113,8 +113,10 @@ std::vector make_bdsp_actions(){ } const Pokemon::StoryArgSpec opt_avoid{.key = "avoid", .type = Pokemon::StoryArgType::INTEGER_ROWS, .required = false, .fallback = std::nullopt, .why = "ground on the target's map to keep off: [x, y] tiles, [x0, y0, x1, y1] boxes"}; + const Pokemon::StoryArgSpec opt_greeter{.key = "greeter", .type = Pokemon::StoryArgType::INTEGER_ROWS, .required = false, + .fallback = std::nullopt, .why = "ground on the target's map where someone walks up and talks, then walks off: [x, y] tiles, [x0, y0, x1, y1] boxes"}; for (std::string_view cost : {"town", "overworld", "indoors", "caves", "robust", "line", "trigger"}){ - actions.push_back({.kind = Pokemon::StoryStepKind::ROUTE, .name = std::string(cost), .args = {route_target, fight_wild, opt_legs, opt_avoid}}); + actions.push_back({.kind = Pokemon::StoryStepKind::ROUTE, .name = std::string(cost), .args = {route_target, fight_wild, opt_legs, opt_avoid, opt_greeter}}); } actions.push_back({.kind = Pokemon::StoryStepKind::ROUTE, .name = "into_scene", .args = {route_target, fight_wild, legs_arg}}); add_legs_actions(actions, legs_arg, fight_wild); @@ -125,9 +127,7 @@ std::vector make_bdsp_actions(){ actions.push_back({.kind = Pokemon::StoryStepKind::BATTLE, .name = "starter", .args = {may_lose}}); actions.push_back({.kind = Pokemon::StoryStepKind::BATTLE, .name = "doubles", .args = {may_lose}}); actions.push_back({.kind = Pokemon::StoryStepKind::SHOP, .name = "sandgem_potions", .args = {items}}); - add_event_actions(actions); - return actions; } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp index 300ebdbf54..109cff58db 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp @@ -243,12 +243,12 @@ ConsoleStoryStepIO::ConsoleStoryStepIO(VideoStream& stream, ProControllerContext , m_route(stream, context, m_escape) {} -void ConsoleStoryStepIO::fight(const std::string& trainer){ +bool ConsoleStoryStepIO::fight(const std::string& trainer){ if (trainer.find("Twins") != std::string::npos){ talk_or_fight(m_stream, m_context, trainer, TalkExpect::BATTLE, BattleFormat::DOUBLES); - return; + return true; } - fight_route_trainer(m_stream, m_context, trainer); + return fight_route_trainer(m_stream, m_context, trainer); } void ConsoleStoryStepIO::walk_until_stopped(Direction direction, size_t tiles, const std::string& label){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h index 3db291d727..35d3d5feb9 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h @@ -63,7 +63,8 @@ class StoryStepIO{ virtual RouteIO& route() = 0; // Fights a trainer whose dialogue has opened (fight_route_trainer()): // Route 204's Twins as a pair (01.10), everyone else alone. - virtual void fight(const std::string& trainer) = 0; + // Returns whether a battle followed. + virtual bool fight(const std::string& trainer) = 0; // walk_until_stopped(): `direction` until something stops us, for at // most `tiles` tiles' time. Fails on a door or a dialogue. virtual void walk_until_stopped(Pokemon::Direction direction, size_t tiles, const std::string& label) = 0; @@ -100,7 +101,7 @@ class ConsoleStoryStepIO : public StoryStepIO{ virtual RouteIO& route() override{ return m_route; } - virtual void fight(const std::string& trainer) override; + virtual bool fight(const std::string& trainer) override; virtual void walk_until_stopped(Pokemon::Direction direction, size_t tiles, const std::string& label) override; virtual bool run_until_map(Pokemon::Direction direction, Milliseconds limit, const std::string& map, const std::string& label) override; virtual WarpSeen run_into_door(Pokemon::Direction direction, bool run, Milliseconds timeout = Milliseconds(12000), diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp index 5eb4d9dd17..a3d0425fa2 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp @@ -445,6 +445,16 @@ UnitTestResult test_jubilife_routes_cross_no_wander_zone(Logger& logger){ } // Where the walk from `x` on row 22 walks the plaza, or "". +// The Wonder Station's plaza on JubilifeCity, as measured on lab-B +// (2026-11-12): its round wall and the woman by it stop walks west on +// rows 21 to 24 from x 52 and on row 26 from x 43 (taps from (58, 26) +// moved 14 tiles clean to (44, 26), then stopped by her). Row 26 east of +// her is open: the way west goes south round her. +constexpr Pokemon::TileBox WONDER_STATION_PLAZA[] = { + {.x0 = 40, .y0 = 20, .x1 = 52, .y1 = 25}, + {.x0 = 40, .y0 = 26, .x1 = 43, .y1 = 27}, +}; + std::string company_clown_plaza_tiles(Logger& logger, TableRoute route, int x){ route.from = Pokemon::tile_pose("JubilifeCity", {{x, 22}}, Direction::WEST); const RouteStepPlan plan = plan_table_route(route); @@ -457,15 +467,17 @@ std::string company_clown_plaza_tiles(Logger& logger, TableRoute route, int x){ } const auto plaza = [&](const TilePos& pos){ const std::optional local = story_to_local(world, "JubilifeCity", pos); - return local && local->x >= 44 && local->x <= 52 && local->y >= 20 && local->y <= 24; + return local && std::any_of(std::begin(WONDER_STATION_PLAZA), std::end(WONDER_STATION_PLAZA), [&](const Pokemon::TileBox& box){ + return local->x >= box.x0 && local->x <= box.x1 && local->y >= box.y0 && local->y <= box.y1; + }); }; const auto [most, leg] = most_zone_tiles(world, plan.start, *plan.legs, plaza); - return most == 0 ? "" : std::format("the leg {} walks {} tile(s) of the plaza (44..52, 20..24).", leg, most); + return most == 0 ? "" : std::format("the leg {} walks {} tile(s) of the plaza.", leg, most); } // 00.05c's walk to the Poketch Company's clown from the street // (57..58, 22), where the checkpoint starts on a rerun from 01.01's start, -// keeps off the plaza (44..52, 20..24). BDSP put the Global Wonder Station +// keeps off the plaza (WONDER_STATION_PLAZA). BDSP put the Global Wonder Station // there, a round wall with people round it that pret's Diamond map lacks: // on lab-B (2026-11-12 01:22 and 01:55) the walk west from (57, 22) was // stopped at (50, 22), (51, 21) and (49, 23), and talked to the woman at @@ -485,6 +497,32 @@ UnitTestResult test_company_clown_from_the_street_keeps_off_the_plaza(Logger& lo return "00.05c below_the_company_clown: not in the table, or from nowhere known."; } +// 00.05c's replan on lab-B (2026-11-12 02:52): the walk west on row 24 +// talked to the woman it took for (34, 24), marked her tile off the map, +// and from (35, 24) and (36, 24), with (43, 26) marked as a wall, found no +// plan at all, with the route costs or the default ones. +UnitTestResult test_company_clown_after_the_woman_on_row_24(Logger& logger){ + for (TableRoute route : table_routes()){ + if (route.row != "00.05c" || route.step->name != "below_the_company_clown" || route.world == nullptr){ + continue; + } + StoryWorld world = *route.world; + world.block(story_to_world(world, "JubilifeCity", {43, 26})); + world.uncover(story_to_world(world, "JubilifeCity", {34, 24})); + route.world = &world; + route.from = Pokemon::tile_pose("JubilifeCity", {{35, 24}, {36, 24}}, Direction::WEST); + const RouteStepPlan plan = plan_table_route(route); + if (!plan.legs){ + return "00.05c below_the_company_clown: no plan from (35, 24), (36, 24)."; + } + for (const PlannedLeg& planned : *plan.legs){ + logger.log("00.05c below_the_company_clown from (35, 24), (36, 24): " + to_string(planned)); + } + return UnitTestResult(true); + } + return "00.05c below_the_company_clown: not in the table, or from nowhere known."; +} + // 01.08's walk below the Ravaged Path keeps off the row Route 204 South's // youngster_3 wanders, (12, 2) to (14, 2): on lab-B (2026-11-08) the plan // "north 35 (to wall) to (13, 2), west 2 (to wall) to (11, 2)" walked along @@ -765,6 +803,10 @@ const AutoStoryStoryTableCase AUTO_STORY_STORY_TABLE_CASES[] = { .input = {"00.05c: from the street (57..58, 22), the walk keeps off the Wonder Station's plaza (lab-B, 2026-11-12)", test_company_clown_from_the_street_keeps_off_the_plaza}, .expectation = {}, }, + { + .input = {"00.05c: a plan from (35, 24), (36, 24) past the woman on row 24 (lab-B, 2026-11-12 02:52)", test_company_clown_after_the_woman_on_row_24}, + .expectation = {}, + }, }; void add_tests_AutoStory_StoryTable(UnitTestDatabase& database){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp index 60f97497a9..c4c41cb0d0 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp @@ -152,15 +152,15 @@ TappedEnd tapped_end(const StoryWorld& world, const std::string& map, const Tile return tapped; } -std::vector story_route_avoid(const StoryWorld& world, const StoryStep& step){ - if (!step.args.contains("avoid")){ +std::vector story_route_tiles(const StoryWorld& world, const StoryStep& step, std::string_view key){ + if (!step.args.contains(std::string(key))){ return {}; } const std::string& map = story_arg(step.args, "target").map; std::vector tiles; - for (const std::vector& row : story_arg>>(step.args, "avoid")){ + for (const std::vector& row : story_arg>>(step.args, std::string(key))){ if (row.size() != 2 && row.size() != 4){ - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("{}: an avoid row is [x, y] or [x0, y0, x1, y1].", step.name)); + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("{}: a {} row is [x, y] or [x0, y0, x1, y1].", step.name, key)); } const size_t last = row.size() - 2; for (int64_t y = row[1]; y <= row[last + 1]; y++){ @@ -251,12 +251,12 @@ Pose StoryWalkSteps::walk_to(const Pose& from, const Pose& goal, const StoryStep std::vector& sights = m_trainers.try_emplace(maps, row_trainers(*maps, m_row)).first->second; RouteTrainers trainers{ sights, - [this](const std::string& trainer){ m_io.fight(trainer); } + [this](const std::string& trainer){ return m_io.fight(trainer); } }; const RouteWalkPlan plan{ [&](const TilePos& pos){ return std::find(targets.begin(), targets.end(), pos) != targets.end(); }, - label_of(step), story_route_avoid(*maps, step), *costs, m_can_run ? MoveSpeed::RUN : MoveSpeed::WALK, m_scene_next.contains(step.name), - step.action == ROUTE_ONTO_TRIGGER, + label_of(step), story_route_tiles(*maps, step, "avoid"), *costs, m_can_run ? MoveSpeed::RUN : MoveSpeed::WALK, m_scene_next.contains(step.name), + step.action == ROUTE_ONTO_TRIGGER, story_route_tiles(*maps, step, "greeter"), }; RouteEnd end = walk_route_to_warp(m_route, *maps, start, trainers, plan); sights = std::move(trainers.sights); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h index 844fc8908a..194c9db7cd 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h @@ -67,12 +67,13 @@ std::optional measured_ends(const Pokemon::Pose& from, const Poke // less those an earlier row fights, whom its save and every later one's // keep beaten. std::vector row_trainers(const StoryWorld& world, const std::string& row); -// The world tiles a ROUTE step's "avoid" rows name on its target's map, -// [x, y] a tile and [x0, y0, x1, y1] a box: ground the maps leave open that -// the walk keeps off (BDSP's Global Wonder Station in Jubilife's plaza, -// which pret's Diamond map lacks). None without the arg. Throws +// The world tiles a ROUTE step's `key` rows name on its target's map, +// [x, y] a tile and [x0, y0, x1, y1] a box. "avoid": ground the maps leave +// open that the walk keeps off (BDSP's Global Wonder Station in Jubilife's +// plaza, which pret's Diamond map lacks). "greeter": where someone walks up +// and talks (RouteWalkPlan::greeter). None without the arg. Throws // InternalProgramError for a row of another length. -std::vector story_route_avoid(const StoryWorld& world, const Pokemon::StoryStep& step); +std::vector story_route_tiles(const StoryWorld& world, const Pokemon::StoryStep& step, std::string_view key); // Executes walking and movement-related story steps via StoryStepIO. diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.cpp index 7a64bb465e..41760bffbf 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.cpp @@ -88,7 +88,7 @@ RouteStepPlan plan_table_route(const TableRoute& route){ // where it starts. StoryWorld world = *route.world; const std::vector from = world_tiles(world, *route.from); - for (const TilePos& pos : story_route_avoid(world, *route.step)){ + for (const TilePos& pos : story_route_tiles(world, *route.step, "avoid")){ if (std::find(from.begin(), from.end(), pos) == from.end()){ world.uncover(pos); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp index 0185a04e74..61c0871497 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp @@ -227,7 +227,7 @@ SimWalk walk_on_sim(const SimRoute& route, bool stick_turns){ GridSimRouteIO sim_io(sim, out.notes); StickTurnIO stick_io(sim_io); RouteIO& io = stick_turns ? static_cast(stick_io) : sim_io; - RouteTrainers trainers{story_trainers(*route.world), [&sim](const std::string&){ sim.end_dialogue(); }}; + RouteTrainers trainers{story_trainers(*route.world), [&sim](const std::string&){ sim.end_dialogue(); return true; }}; const std::vector& targets = route.targets; std::vector avoid = wandering_hazards(*route.world); std::erase_if(avoid, [&](const TilePos& pos){ @@ -425,8 +425,8 @@ class WalkStepsIO : public StoryStepIO{ virtual RouteIO& route() override{ return m_route; } - virtual void fight(const std::string& trainer) override{ - unsupported("a fight with " + trainer); + virtual bool fight(const std::string& trainer) override{ + return unsupported("a fight with " + trainer); } virtual void walk_until_stopped(Direction direction, size_t tiles, const std::string& label) override{ m_sim.tap(direction, ROUTE_TURN_TAP, false); @@ -437,8 +437,7 @@ class WalkStepsIO : public StoryStepIO{ } } virtual bool run_until_map(Direction, Milliseconds, const std::string& map, const std::string&) override{ - unsupported("a run onto " + map); - return false; + return unsupported("a run onto " + map); } virtual WarpSeen run_into_door(Direction, bool, Milliseconds, Milliseconds, const std::string&) override{ unsupported("a run into a door"); @@ -453,8 +452,9 @@ class WalkStepsIO : public StoryStepIO{ } private: - void unsupported(const std::string& what){ + bool unsupported(const std::string& what){ m_route.note(what + " is not simulated here.", RouteNote::FAILURE); + return false; } GridSim& m_sim; @@ -717,7 +717,7 @@ UnitTestResult test_wandering_npc_routes_multi_seed(Logger& logger){ GridSim sim({.world = world, .trainers = story_trainers(*world), .wanderers = {hiker}}, start, Direction::NORTH, true); std::vector notes; GridSimRouteIO sim_io(sim, notes); - RouteTrainers trainers{story_trainers(*world), [&sim](const std::string&){ sim.end_dialogue(); }}; + RouteTrainers trainers{story_trainers(*world), [&sim](const std::string&){ sim.end_dialogue(); return true; }}; const auto result = walk_route(sim_io, *world, {start}, trainers, { .goal = [&](const TilePos& pos){ return pos == target; }, .label = "01.03 above_the_mine", @@ -751,7 +751,7 @@ UnitTestResult test_wandering_npc_wait_clearance(Logger& logger){ GridSim sim({.world = world, .trainers = {}, .wanderers = {hiker}}, start, Direction::NORTH, true); std::vector notes; GridSimRouteIO sim_io(sim, notes); - RouteTrainers trainers{{}, [&sim](const std::string&){ sim.end_dialogue(); }}; + RouteTrainers trainers{{}, [&sim](const std::string&){ sim.end_dialogue(); return true; }}; const auto result = walk_route(sim_io, *world, {start}, trainers, { .goal = [&](const TilePos& pos){ return pos == target; }, .label = "walk_through_hiker", @@ -804,7 +804,7 @@ UnitTestResult test_wandering_npc_oreburgh_north_multi_seed(Logger& logger){ GridSim sim({.world = world, .trainers = story_trainers(*world), .wanderers = {hiker}}, start, Direction::NORTH, true); std::vector notes; GridSimRouteIO sim_io(sim, notes); - RouteTrainers trainers{story_trainers(*world), [&sim](const std::string&){ sim.end_dialogue(); }}; + RouteTrainers trainers{story_trainers(*world), [&sim](const std::string&){ sim.end_dialogue(); return true; }}; const auto result = walk_route(sim_io, *world, {start}, trainers, { .goal = [&](const TilePos& pos){ return pos == target; }, .label = "01.03 oreburgh_north", @@ -854,7 +854,7 @@ UnitTestResult test_wall_leg_check_at_wall_short_distance_safeguard(Logger& logg GridSim sim({.world = &world}, start, Direction::EAST, true); std::vector notes; StalledWallLegRouteIO sim_io(sim, notes); - RouteTrainers trainers{{}, [](const std::string&){}}; + RouteTrainers trainers{{}, [](const std::string&){ return true; }}; try{ walk_route(sim_io, world, {start}, trainers, { .goal = [&](const TilePos& pos){ return pos == target; }, @@ -871,6 +871,75 @@ UnitTestResult test_wall_leg_check_at_wall_short_distance_safeguard(Logger& logg return "caught unexpected failure: " + f.message; } } +// A hold into someone standing still reads as stopped `late` after a stop +// at a wall would: the screen moves on while we push against them. +class PushingRouteIO : public GridSimRouteIO{ +public: + PushingRouteIO(GridSim& sim, std::vector& notes, Milliseconds late) + : GridSimRouteIO(sim, notes) + , m_sim(sim) + , m_late(late) + {} + + virtual WalkResult walk_to_wall(const RouteWallWalk& walk) override{ + WalkResult result = GridSimRouteIO::walk_to_wall(walk); + const TilePos ahead = nearest_tile(m_sim.player().pos).next(walk.direction); + const std::vector& people = sim_wanderers(m_sim); + if (result.stop == WalkStop::BLOCKED && std::any_of(people.begin(), people.end(), [&](const SimWanderer& person){ + return person.pos == ahead; + })){ + result.walked += m_late; + } + return result; + } + +private: + GridSim& m_sim; + Milliseconds m_late; +}; + +// Someone the map lacks stands on Jubilife's row 26 at (54, 26), and the +// walk west to the wall from (58, 26) pushes against them, which reads as +// stopped 1000 ms late: on lab-B (2026-11-12 02:52) "west 17 (to wall)" +// stopped after 2400 ms, its time read as "about 13 tiles", and the stop's +// tiles round the count, (44..46, 26), went on as where we stood. Any tile +// of the leg fits; taps back to the wall behind find which. +UnitTestResult test_stranger_stops_a_wall_leg_early(Logger& logger){ + const StoryWorld* world = story_plan_world("JubilifeCity", "JubilifeCity"); + if (world == nullptr){ + return "JubilifeCity world not found."; + } + const TilePos start = story_to_world(*world, "JubilifeCity", {58, 26}); + const TilePos target = story_to_world(*world, "JubilifeCity", {41, 26}); + const TilePos stranger = story_to_world(*world, "JubilifeCity", {54, 26}); + const SimWanderer person{.name = "a stranger", .box = {.x0 = stranger.x, .y0 = stranger.y, .x1 = stranger.x, .y1 = stranger.y}, + .pos = stranger, .seed = 0, .step_interval = 0ms}; + GridSim sim({.world = world, .trainers = {}, .wanderers = {person}}, start, Direction::WEST, true); + std::vector notes; + PushingRouteIO sim_io(sim, notes, 1000ms); + RouteTrainers trainers{{}, [&sim](const std::string&){ sim.end_dialogue(); return true; }}; + std::vector believed; + try{ + believed = walk_route(sim_io, *world, {start}, trainers, { + .goal = [&](const TilePos& pos){ return pos == target; }, + .label = "west on row 26", + .avoid = {}, + .costs = *story_route_costs("town"), + .speed = MoveSpeed::RUN, + }); + }catch (const GridSimFailure& e){ + return "failed: " + e.message; + } + for (const std::string& note : notes){ + logger.log(note); + } + const TilePos at = nearest_tile(sim.player().pos); + if (believed != std::vector{target} || at != target){ + return std::format("ended on {}, believed {} tile(s), not on {}.", describe(*world, at), believed.size(), describe(*world, target)); + } + return true; +} + // Oreburgh Mine B1F: the entrance hall corridor is traversed to and back by // running south from (12, 1) to (12, 21), and north from (12, 21), where the // coal room's warp lands us, to (12, 1). @@ -973,6 +1042,10 @@ const AutoStoryGridSimCase AUTO_STORY_GRID_SIM_CASES[] = { .input = {"check_at_wall safeguard rejects legs stalled well short of expected distance", test_wall_leg_check_at_wall_short_distance_safeguard}, .expectation = {}, }, + { + .input = {"00.05c, someone the map lacks stops a wall leg early, the push read as many tiles (lab-B, 2026-11-12 02:52)", test_stranger_stops_a_wall_leg_early}, + .expectation = {}, + }, { .input = {"01.03, Oreburgh Mine B1F entrance hall corridor runs to and back", test_oreburgh_mine_hall_corridor}, .expectation = {}, diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h index b73e7a9884..4eecb78cfe 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h @@ -337,8 +337,9 @@ class SimStoryStepIO : public StoryStepIO{ } // A scene's dialogue fought as a trainer's is pressed through under the // walk, not its own step: noted (sim_fight()). - virtual void fight(const std::string&) override{ + virtual bool fight(const std::string&) override{ detail::sim_fight(m_sim); + return true; } virtual void walk_until_stopped(Pokemon::Direction direction, size_t tiles, const std::string& label) override; virtual bool run_until_map(Pokemon::Direction direction, Milliseconds limit, const std::string& map, const std::string&) override; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp index dc74a850ec..6ffc93aeec 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp @@ -7,6 +7,9 @@ * the wall with the battle counted. And a run into a scene (00.03's into * Sandgem), a wild battle on it or not: the scene stops the run wherever it * starts, and with no scene the run stops at its cap and the step fails. + * And 00.05c's greeter by the Wonder Station, who talks each time we come + * near: heard out each time, the walk still arrives. And a talk no battle + * follows that outlasts a press-through: heard out, the walk goes round. * */ @@ -93,7 +96,7 @@ WallBattleWalk walk_wall_battle(const std::string& leg, const SimWildBattle& bat GridSim sim({.world = &world}, start, Direction::EAST, true); WallBattleWalk walk; GridSimRouteIO sim_io(sim, walk.notes, {}, {{.label = leg, .direction = Direction::EAST, .battle = battle}}); - RouteTrainers trainers{{}, [](const std::string&){}}; + RouteTrainers trainers{{}, [](const std::string&){ return true; }}; try{ walk_route(sim_io, world, {start}, trainers, { .goal = [&](const TilePos& pos){ return pos == target; }, @@ -159,8 +162,9 @@ class RouteOnlyStepIO : public StoryStepIO{ virtual RouteIO& route() override{ return m_route; } - virtual void fight(const std::string& trainer) override{ + virtual bool fight(const std::string& trainer) override{ unsupported("a fight with " + trainer); + return false; } virtual void walk_until_stopped(Direction, size_t, const std::string& label) override{ unsupported(label + ": a walk until stopped"); @@ -338,10 +342,330 @@ const SceneRunCase SCENE_RUN_SIM_CASES[] = { {.input = {.name = "a wild battle and no scene: the step fails at the cap", .dawn = false, .battle = true}, .expectation = {.in_scene = false}}, }; +// The Wonder Station's greeter in Jubilife's plaza walks up and talks +// whenever we come near, then walks off: on lab-B (2026-11-12 03:57, 00.05c +// below_the_company_clown) she stopped one walk three times, at about +// (44, 28), (43, 30) and (39, 32), and the third failed it as "dialogue the +// map does not explain". Here a step onto the step's greeter ground starts +// her dialogue, and so does the next one after each talk, `talks` in all. +struct GreeterInput{ + std::string name; + size_t talks; +}; +struct GreeterExpectation{ + bool arrives; +}; +struct GreeterCase{ + GreeterInput input; + GreeterExpectation expectation; +}; + +// Where the walk past `talks` of the greeter's went wrong, or "". +std::string walk_past_the_greeter(Logger& logger, size_t most_talks){ + const StoryStep* step = nullptr; + for (const StoryCheckpoint& row : story_checkpoints()){ + for (const StoryStep& s : row.steps){ + step = row.id == "00.05c" && s.name == "below_the_company_clown" ? &s : step; + } + } + const StoryWorld* world = story_plan_world("JubilifeCity", "JubilifeCity"); + if (step == nullptr || world == nullptr){ + return "00.05c below_the_company_clown: not in the table, or no JubilifeCity world."; + } + const std::vector greeter = story_route_tiles(*world, *step, "greeter"); + const TilePos start = story_to_world(*world, "JubilifeCity", {57, 22}); + const TilePos target = story_to_world(*world, "JubilifeCity", {15, 19}); + GridSim sim({.world = world, .trainers = {}, .triggers = {}}, start, Direction::WEST, true); + size_t talks = 0; + const auto arm = [&](){ + sim_add_trigger(sim, {.name = std::format("the greeter {}", talks), .tiles = greeter, .spends = {}}); + }; + arm(); + std::vector notes; + GridSimRouteIO sim_io(sim, notes); + RouteTrainers trainers{{}, [&](const std::string&){ + sim.end_dialogue(); + talks++; + if (talks < most_talks){ + arm(); + } + return false; + }}; + std::vector believed; + try{ + believed = walk_route(sim_io, *world, {start}, trainers, {.goal = [&](const TilePos& pos){ return pos == target; }, + .label = "below the company clown", .avoid = story_route_tiles(*world, *step, "avoid"), .costs = *story_route_costs("town"), + .speed = MoveSpeed::RUN, .scene_next = false, .goal_is_trigger = false, .greeter = greeter}); + }catch (const GridSimFailure& e){ + return std::format("failed after {} talk(s): {}", talks, e.message); + } + for (const std::string& note : notes){ + logger.log(note); + } + const TilePos at = nearest_tile(sim.player().pos); + const bool arrived = believed == std::vector{target} && at == target && talks == most_talks; + return arrived ? "" : std::format("ended on {} after {} talk(s), not on {}.", describe(*world, at), talks, describe(*world, target)); +} + +class Test_GreeterSim : public UnitTest{ +public: + explicit Test_GreeterSim(GreeterCase test_case) + : UnitTest("PokemonBDSP::AutoStory - Grid sim: 00.05c, the Wonder Station's greeter " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + const std::string error = walk_past_the_greeter(logger, m_case.input.talks); + if (error.empty() != m_case.expectation.arrives){ + return error.empty() ? "arrived" : error; + } + return true; + } + +private: + GreeterCase m_case; +}; + +const GreeterCase GREETER_SIM_CASES[] = { + {.input = {.name = "talks once", .talks = 1}, .expectation = {.arrives = true}}, + {.input = {.name = "talks again on each leg near her (lab-B, 2026-11-12 03:57)", .talks = 3}, .expectation = {.arrives = true}}, +}; + +// A talk no battle followed, long enough to outlast a press-through, from +// someone who stands on the leg: on lab-B (2026-11-12 04:58, 00.05c +// below_the_company_clown, the 5th failure there) the Wonder Station's +// woman stopped "west 1 (to wall)" as "a trainer on the way", 2 s of B left +// her explanation on screen, and the walk on into her ran out of time. +// Here a corridor two rows high: her talk starts on (11, 1) and takes +// `presses` press-throughs to end; with `stands` she stays on (12, 1), +// which our map has open. +struct TalkerInput{ + std::string name; + size_t presses; + bool stands; +}; +struct TalkerExpectation{ + bool arrives; +}; +struct TalkerCase{ + TalkerInput input; + TalkerExpectation expectation; +}; + +// One press-through of the talk: the `needed`th ends it. +void press_through(GridSim& sim, size_t& presses, size_t needed){ + if (sim.player().dialog && ++presses >= needed){ + sim.end_dialogue(); + } +} + +// Where the walk past the talker went wrong, or "". +std::string walk_past_the_talker(Logger& logger, const TalkerInput& input){ + const std::vector open{ + "########################", + "#......................#", + "#......................#", + "########################", + }; + std::vector real = open; + if (input.stands){ + real[1][12] = '#'; + } + const StoryWorld map = make_world(open); + const StoryWorld world = make_world(real); + const TilePos start{1, 1}; + const TilePos target{22, 1}; + const SimTrigger talk{.name = "the woman", .tiles = {TilePos{11, 1}}}; + GridSim sim({.world = &world, .trainers = {}, .triggers = {talk}}, start, Direction::EAST, true); + std::vector notes; + GridSimRouteIO sim_io(sim, notes); + size_t presses = 0; + RouteTrainers trainers{{}, [&](const std::string&){ + press_through(sim, presses, input.presses); + return false; + }}; + std::vector believed; + try{ + believed = walk_route(sim_io, map, {start}, trainers, {.goal = [&](const TilePos& pos){ return pos == target; }, + .label = "past the talker", .avoid = {}, .costs = *story_route_costs("town"), .speed = MoveSpeed::RUN}); + }catch (const GridSimFailure& e){ + return std::format("failed after {} press-through(s): {}", presses, e.message); + } + for (const std::string& note : notes){ + logger.log(note); + } + const TilePos at = nearest_tile(sim.player().pos); + const bool arrived = believed == std::vector{target} && at == target; + return arrived ? "" : std::format("ended on {} believing {}, not on {}.", to_string(sim.player().pos), + believed.empty() ? std::string("nothing") : describe(map, believed[0]), describe(map, target)); +} + +class Test_TalkerSim : public UnitTest{ +public: + explicit Test_TalkerSim(TalkerCase test_case) + : UnitTest("PokemonBDSP::AutoStory - Grid sim: a talk no battle follows " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + const std::string error = walk_past_the_talker(logger, m_case.input); + if (error.empty() != m_case.expectation.arrives){ + return error.empty() ? "arrived" : error; + } + return true; + } + +private: + TalkerCase m_case; +}; + +const TalkerCase TALKER_SIM_CASES[] = { + {.input = {.name = "outlasts a press-through, then she walks off", .presses = 3, .stands = false}, .expectation = {.arrives = true}}, + {.input = {.name = "outlasts a press-through, and she stands on the leg (lab-B, 2026-11-12 04:58)", .presses = 3, .stands = true}, .expectation = {.arrives = true}}, + {.input = {.name = "ends in the fight, and she stands on the leg (lab-B, 2026-11-12 06:12)", .presses = 1, .stands = true}, .expectation = {.arrives = true}}, +}; + +// The plaza's greeter on an open leg, as lab-B saw her (2026-11-12 07:44, +// 00.05c below_the_company_clown, run 8): her talk stopped "south 5 (open)" +// from (46, 27) with no tap counted, so the stop could be any tile of the +// leg, (46, 27) to (46, 33); the walk planned from all seven, and her next +// talk, a tap later, spread it over two columns, from which no plan was +// found. Here her talk starts on each of `talks` tiles in turn (map tiles), +// and the console's count of taps before a talk is lost (UncountedTalkIO). +struct OpenLegTalkInput{ + std::string name; + std::vector talks; +}; +struct OpenLegTalkExpectation{ + bool arrives; +}; +struct OpenLegTalkCase{ + OpenLegTalkInput input; + OpenLegTalkExpectation expectation; +}; + +// Taps a talk stopped count none moved, as on lab-B (run 8). +class UncountedTalkIO : public RouteIO{ +public: + explicit UncountedTalkIO(RouteIO& io) + : m_io(io) + {} + virtual WalkResult walk_to_wall(const RouteWallWalk& walk) override{ + return m_io.walk_to_wall(walk); + } + virtual WalkResult walk_taps(Direction direction, size_t tiles, const std::string& label) override{ + WalkResult result = m_io.walk_taps(direction, tiles, label); + if (result.stop == WalkStop::DIALOG){ + result.moved_chunks = 0; + } + return result; + } + virtual void press(const RouteInput& input) override{ + m_io.press(input); + } + virtual bool dialog_within(Milliseconds wait, Color color) override{ + return m_io.dialog_within(wait, color); + } + virtual bool holding_against_wall(Direction direction, const std::string& label) override{ + return m_io.holding_against_wall(direction, label); + } + virtual void look(const std::function& at) override{ + m_io.look(at); + } + virtual void note(const std::string& text, RouteNote kind) override{ + m_io.note(text, kind); + } + +private: + RouteIO& m_io; +}; + +// Where the walk from (46, 27) past the greeter's talks went wrong, or "". +std::string walk_open_leg_talks(Logger& logger, const OpenLegTalkInput& input){ + const StoryStep* step = nullptr; + for (const StoryCheckpoint& row : story_checkpoints()){ + for (const StoryStep& s : row.steps){ + step = row.id == "00.05c" && s.name == "below_the_company_clown" ? &s : step; + } + } + const StoryWorld* world = story_plan_world("JubilifeCity", "JubilifeCity"); + if (step == nullptr || world == nullptr){ + return "00.05c below_the_company_clown: not in the table, or no JubilifeCity world."; + } + const TilePos start = story_to_world(*world, "JubilifeCity", {46, 27}); + const TilePos target = story_to_world(*world, "JubilifeCity", {15, 19}); + GridSim sim({.world = world, .trainers = {}, .triggers = {}}, start, Direction::SOUTH, true); + size_t talks = 0; + const auto arm = [&](){ + if (talks < input.talks.size()){ + sim_add_trigger(sim, {.name = std::format("the greeter {}", talks), + .tiles = {story_to_world(*world, "JubilifeCity", input.talks[talks])}, .spends = {}}); + } + }; + arm(); + std::vector notes; + GridSimRouteIO sim_io(sim, notes); + UncountedTalkIO io(sim_io); + RouteTrainers trainers{{}, [&](const std::string&){ + sim.end_dialogue(); + talks++; + arm(); + return false; + }}; + std::vector believed; + std::string error; + try{ + believed = walk_route(io, *world, {start}, trainers, {.goal = [&](const TilePos& pos){ return pos == target; }, + .label = "below the company clown", .avoid = story_route_tiles(*world, *step, "avoid"), .costs = *story_route_costs("town"), + .speed = MoveSpeed::RUN, .scene_next = false, .goal_is_trigger = false, .greeter = story_route_tiles(*world, *step, "greeter")}); + }catch (const GridSimFailure& e){ + error = std::format("failed after {} talk(s): {}", talks, e.message); + } + for (const std::string& note : notes){ + logger.log(note); + } + if (!error.empty()){ + return error; + } + const TilePos at = nearest_tile(sim.player().pos); + const bool arrived = believed == std::vector{target} && at == target && talks == input.talks.size(); + return arrived ? "" : std::format("ended on {} after {} talk(s), not on {}.", describe(*world, at), talks, describe(*world, target)); +} + +class Test_OpenLegTalkSim : public UnitTest{ +public: + explicit Test_OpenLegTalkSim(OpenLegTalkCase test_case) + : UnitTest("PokemonBDSP::AutoStory - Grid sim: 00.05c, the greeter's talk on an open leg " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + const std::string error = walk_open_leg_talks(logger, m_case.input); + if (error.empty() != m_case.expectation.arrives){ + return error.empty() ? "arrived" : error; + } + return true; + } + +private: + OpenLegTalkCase m_case; +}; + +const OpenLegTalkCase OPEN_LEG_TALK_SIM_CASES[] = { + {.input = {.name = "with no tap counted", .talks = {{46, 29}}}, .expectation = {.arrives = true}}, + {.input = {.name = "with no tap counted, then again a tap on (lab-B, 2026-11-12 07:44)", .talks = {{46, 29}, {45, 30}}}, .expectation = {.arrives = true}}, +}; + } // namespace void add_tests_AutoStory_WallBattleSim(UnitTestDatabase& database){ + for (const OpenLegTalkCase& item : OPEN_LEG_TALK_SIM_CASES){ + database.add(item); + } + for (const TalkerCase& item : TALKER_SIM_CASES){ + database.add(item); + } + for (const GreeterCase& item : GREETER_SIM_CASES){ + database.add(item); + } for (const WallBattleCase& item : WALL_BATTLE_SIM_CASES){ database.add(item); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05c.plan.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05c.plan.json index 5bf12d8a0b..3ae7db38bf 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05c.plan.json +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05c.plan.json @@ -12,13 +12,27 @@ "args": { "avoid": [ [ - 44, + 40, 20, 52, - 24 + 25 + ], + [ + 40, + 26, + 43, + 27 ] ], "fight_wild": false, + "greeter": [ + [ + 30, + 22, + 48, + 34 + ] + ], "target": { "facing": "north", "map": "JubilifeCity", @@ -57,6 +71,14 @@ "action": "town", "args": { "fight_wild": false, + "greeter": [ + [ + 30, + 22, + 48, + 34 + ] + ], "target": { "facing": "north", "map": "JubilifeCity", @@ -95,6 +117,14 @@ "action": "town", "args": { "fight_wild": false, + "greeter": [ + [ + 30, + 22, + 48, + 34 + ] + ], "target": { "facing": "south", "map": "JubilifeCity", @@ -151,6 +181,14 @@ "action": "town", "args": { "fight_wild": false, + "greeter": [ + [ + 30, + 22, + 48, + 34 + ] + ], "target": { "facing": "east", "map": "JubilifeCity", diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.cpp index 178553ff52..0eeca444cf 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.cpp @@ -6,6 +6,7 @@ #include #include +#include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/PrettyPrint.h" #include "Common/Cpp/Filesystem/Filesystem.h" @@ -131,6 +132,11 @@ Milliseconds wall_limit(const TileLeg& leg){ return ROUTE_START_DELAY + per_tile * leg.tiles * 5 / 4 + 1000ms; } +// A talk no battle followed is heard out until no dialogue shows this +// long, pressed through at most this often. +constexpr Milliseconds HEAR_OUT_QUIET = 5000ms; +constexpr size_t HEAR_OUT_PRESSES = 6; + // A wait for someone walking about to get out of the way. constexpr RouteInput WAIT_FOR_THE_WAY{.kind = RouteInputKind::WAIT, .direction = Direction::NORTH, .hold = 2500ms, .release = 0ms, .count = 1, .end = RouteInputEnd::DONE}; @@ -148,6 +154,14 @@ size_t tiles_moved(const WalkResult& walk, Milliseconds per_tile){ return moving.count() <= 0 ? (size_t)0 : (size_t)(moving / per_tile); } +// Tiles a walk on to the wall after a stop moved: none if no chunk moved. +// A push against someone still in the way reads as stopped late, and its +// time counted as tiles put a walk stopped 3 tiles in at its wall 17 tiles +// away (the sim of lab-B 2026-11-12 02:52). +size_t moved_on(const WalkResult& walk, Milliseconds per_tile){ + return walk.moved_chunks == 0 ? 0 : tiles_moved(walk, per_tile); +} + // Whether a walk to the wall never moved: no chunk changed and its time // counts no tile. It stopped where it began, so whoever stopped it stands // on the next tile, however short the leg. On lab-B (2026-11-08 16:50, @@ -256,18 +270,22 @@ class RouteWalker{ RouteEnd run(std::vector from); private: - std::vector plan_legs(bool first) const; + std::optional> plan_legs(bool first) const; + bool find_again(); LegNext walk_leg(const PlannedLeg& planned, size_t number, size_t count); std::vector arrive() const; // Legs to a wall. LegNext wall_leg(LegWalk& walk); - bool finish_interrupted_wall(LegWalk& walk, Milliseconds limit); + std::optional finish_interrupted_wall(LegWalk& walk, Milliseconds limit); size_t unstick_wall_leg(LegWalk& walk, size_t walked_tiles, size_t nearest); LegNext stopped_short_of_wall(LegWalk& walk, size_t walked_tiles); + LegNext stopped_near_count(LegWalk& walk, size_t walked_tiles, Blocker blocker); + LegNext stopped_well_short(LegWalk& walk, size_t walked_tiles, bool first_moved); + std::vector back_to_the_wall(const LegWalk& walk, const std::vector& stopped, Blocker blocker); LegNext held_up_on_the_way(const LegWalk& walk, size_t nearest); - void check_at_wall(LegWalk& walk, size_t walked_tiles = SIZE_MAX, size_t expected_tiles = SIZE_MAX); - void on_to_wall(LegWalk& walk); + LegNext check_at_wall(LegWalk& walk, size_t walked_tiles = SIZE_MAX, size_t expected_tiles = SIZE_MAX); + LegNext on_to_wall(LegWalk& walk); // Open legs, tapped. LegNext open_leg(LegWalk& walk); @@ -285,6 +303,11 @@ class RouteWalker{ LegNext known_trainer(LegWalk& walk, const std::vector>& paths, const std::vector& watching); Blocker talk_to_blocker(const std::string& leg_label); + void count_unexplained_talk(const std::string& leg_label, const std::vector& near); + bool heard_out_a_talk(const std::string& leg_label); + LegNext replan_after_talk(const LegWalk& walk, std::vector stopped); + std::vector talk_stop_tiles(const LegWalk& walk, size_t walked_tiles) const; + std::vector open_leg_talk_tiles(const LegWalk& walk) const; LegNext replan_from(const LegWalk& walk, std::vector stopped, const std::string& why); void mark_blocker(const LegWalk& walk, const std::vector& stopped, Blocker blocker); void mark_tiles_past(const LegWalk& walk, const std::vector& stopped); @@ -318,7 +341,14 @@ RouteEnd RouteWalker::run(std::vector from){ m_from = std::move(from); m_map_start = map_of(m_maps, m_from); for (size_t plans = 0; plans < 8; plans++){ - const std::vector legs = plan_legs(plans == 0); + const std::optional> planned = plan_legs(plans == 0); + if (!planned){ + if (!find_again()){ + fail(m_io, m_plan.label + ": no plan from " + describe(m_maps, m_from) + "."); + } + continue; + } + const std::vector& legs = *planned; m_io.note(m_plan.label + ": " + std::to_string(legs.size()) + " legs planned", RouteNote::STATUS); log_plan(m_io, m_maps, m_plan.label, m_from, legs); LegNext next = LegNext::NEXT_LEG; @@ -332,7 +362,7 @@ RouteEnd RouteWalker::run(std::vector from){ fail(m_io, m_plan.label + ": too many plans."); } -std::vector RouteWalker::plan_legs(bool first) const{ +std::optional> RouteWalker::plan_legs(bool first) const{ std::optional> legs = plan_walk({m_grid, m_from, m_plan.goal, m_plan.costs, m_plan.speed, {}, m_plan.goal_is_trigger}, first); if (!legs){ // From a few possible tiles the exact-open-leg costs found no @@ -349,10 +379,56 @@ std::vector RouteWalker::plan_legs(bool first) const{ legs = plan_recovered_route(m_maps, m_from, m_plan.goal, RobustPlanCosts()); } } - if (!legs){ - fail(m_io, m_plan.label + ": no plan from " + describe(m_maps, m_from) + "."); + return legs; +} + +// From tiles no plan brings to one wall (a talk on the plaza's open legs +// spread us over two columns, lab-B 2026-11-12 07:44, 00.05c run 8): hold +// toward a wall each of them stops at, the way whose walls tiles the time +// cannot tell apart share, and keep the walls whose distance the time +// walked fits. Returns whether there was such a way. +bool RouteWalker::find_again(){ + constexpr size_t SLACK = 2; + for (const Direction direction : {Direction::SOUTH, Direction::NORTH, Direction::EAST, Direction::WEST}){ + std::vector hits; + size_t farthest = 0; + for (const TilePos& pos : m_from){ + hits.push_back(slide_to_wall(m_grid, pos, direction)); + farthest = std::max(farthest, hits.back().tiles); + } + const bool apart = std::all_of(hits.begin(), hits.end(), [&](const WallHit& a){ + return std::all_of(hits.begin(), hits.end(), [&](const WallHit& b){ + return a.stop == b.stop || a.tiles > b.tiles + SLACK || b.tiles > a.tiles + SLACK; + }); + }); + if (!apart || farthest == 0){ + continue; + } + const TileLeg leg{.direction = direction, .tiles = farthest, .ends_at_wall = true, .held = false}; + const std::string label = m_plan.label + ": " + to_string(leg) + " to find us again"; + const WalkResult walk = m_io.walk_to_wall({.direction = direction, .limit = wall_limit(leg), .label = label, + .options = {.arrived = nullptr, .pace = DpadPace::RUN}}); + if (walk.stop != WalkStop::BLOCKED && walk.stop != WalkStop::LIMIT){ + fail(m_io, label + ": " + walk_stop_name(walk.stop) + " instead of a wall."); + } + const size_t walked = tiles_moved(walk, route_ms_per_tile(direction, m_plan.speed)); + std::vector found; + for (const WallHit& hit : hits){ + if (hit.tiles + SLACK >= walked && walked + SLACK >= hit.tiles){ + found.push_back(hit.stop); + } + } + if (found.empty()){ + for (const WallHit& hit : hits){ + found.push_back(hit.stop); + } + } + m_from = sorted_unique(std::move(found)); + m_facing = direction; + m_io.note(std::format("{}{}", ROUTE_LOG_TAG, route_found_line(m_plan.label, route_tiles(m_maps, m_from), 1)), RouteNote::WARNING); + return true; } - return std::move(*legs); + return false; } LegNext RouteWalker::walk_leg(const PlannedLeg& planned, size_t number, size_t count){ @@ -419,8 +495,8 @@ LegNext RouteWalker::wall_leg(LegWalk& walk){ const TileLeg& leg = walk.planned.leg; const Milliseconds limit = wall_limit(leg); walk.result = m_io.walk_to_wall({.direction = walk.planned.leg.direction, .limit = limit, .label = walk.label, .options = run_watching(walk)}); - if (finish_interrupted_wall(walk, limit)){ - return LegNext::NEXT_LEG; + if (const std::optional next = finish_interrupted_wall(walk, limit)){ + return *next; } // A wall well before the nearest one the map has: something the map // does not know is in the way. Work out where we stopped from the time @@ -440,7 +516,12 @@ LegNext RouteWalker::wall_leg(LegWalk& walk){ if (nearest != SIZE_MAX && walked_tiles >= nearest && held_up(walk.result, nearest, route_ms_per_tile(leg.direction, m_plan.speed))){ return held_up_on_the_way(walk, nearest); } + const bool well_short = walked_tiles + 2 < nearest && !never_moved(walk.result, walked_tiles); + const bool first_moved = walk.result.moved_chunks > 0; walked_tiles = unstick_wall_leg(walk, walked_tiles, nearest); + if (walk.result.stop == WalkStop::BLOCKED && walked_tiles < nearest && well_short){ + return stopped_well_short(walk, walked_tiles, first_moved); + } // Any stop short of the wall's distance: someone who stays put is // talked to, and the leg is planned again from where we are. Within the // time count's slack this may be the wall itself, where A does nothing @@ -459,14 +540,15 @@ LegNext RouteWalker::wall_leg(LegWalk& walk){ if (nearest <= EXACT_WALL_LEG_TILES && walked_tiles < nearest){ return stopped_short_of_wall(walk, walked_tiles); } - check_at_wall(walk, walked_tiles, nearest); + return check_at_wall(walk, walked_tiles, nearest); } return LegNext::NEXT_LEG; } -bool RouteWalker::finish_interrupted_wall(LegWalk& walk, Milliseconds limit){ +// Nothing without a wild battle on the walk; else how the leg goes on. +std::optional RouteWalker::finish_interrupted_wall(LegWalk& walk, Milliseconds limit){ if (walk.result.wild_battles == 0){ - return false; + return std::nullopt; } // A battle that may have begun before the hold changes nothing here: // from where the leg began, it ends at its wall however far it got. @@ -482,9 +564,9 @@ bool RouteWalker::finish_interrupted_wall(LegWalk& walk, Milliseconds limit){ fail(m_io, "recovery budget exhausted: " + walk.label); } if (walk.result.stop != WalkStop::BLOCKED){ - check_at_wall(walk); + return check_at_wall(walk); } - return true; + return LegNext::NEXT_LEG; } // A walk to the wall that stopped well short of it: tap through a corner, @@ -502,7 +584,7 @@ size_t RouteWalker::unstick_wall_leg(LegWalk& walk, size_t walked_tiles, size_t m_io.press({.kind = RouteInputKind::DPAD, .direction = walk.planned.leg.direction, .hold = 160ms, .release = 640ms, .count = 2, .end = RouteInputEnd::DONE}); walk.result = m_io.walk_to_wall({.direction = walk.planned.leg.direction, .limit = limit, .label = walk.label, .options = run_watching(walk)}); - walked_tiles += tiles_moved(walk.result, per_tile); + walked_tiles += moved_on(walk.result, per_tile); } // Someone walking about may stand in the way for a moment: wait and // walk to the wall again before calling it an obstacle. A walk that @@ -516,7 +598,7 @@ size_t RouteWalker::unstick_wall_leg(LegWalk& walk, size_t walked_tiles, size_t " tiles, short of the wall; waiting for the way to clear.", RouteNote::WARNING); m_io.press(WAIT_FOR_THE_WAY); walk.result = m_io.walk_to_wall({.direction = walk.planned.leg.direction, .limit = limit, .label = walk.label, .options = run_watching(walk)}); - walked_tiles += tiles_moved(walk.result, per_tile); + walked_tiles += moved_on(walk.result, per_tile); } return walked_tiles; } @@ -524,7 +606,10 @@ size_t RouteWalker::unstick_wall_leg(LegWalk& walk, size_t walked_tiles, size_t LegNext RouteWalker::stopped_short_of_wall(LegWalk& walk, size_t walked_tiles){ // Someone who stays put: talk to them. A trainer we walked up to from // the side starts the battle this way. - const Blocker blocker = talk_to_blocker(walk.label); + return stopped_near_count(walk, walked_tiles, talk_to_blocker(walk.label)); +} + +LegNext RouteWalker::stopped_near_count(LegWalk& walk, size_t walked_tiles, Blocker blocker){ // A walk that never moved is where it began (never_moved()); any other // stop may be a tile either side of the tiles its time counts. const size_t last = never_moved(walk.result, walked_tiles) ? 0 : walked_tiles + 1; @@ -546,7 +631,99 @@ LegNext RouteWalker::stopped_short_of_wall(LegWalk& walk, size_t walked_tiles){ return replan_from(walk, std::move(stopped), "stopped early after about " + std::to_string(walked_tiles) + " tiles"); } -void RouteWalker::check_at_wall(LegWalk& walk, size_t walked_tiles, size_t expected_tiles){ +// A walk to the wall that stopped well short of it and stayed stopped: +// its time no longer counts tiles. A push against someone reads as +// stopped only late, so the count is at most a tile under where we stand, +// and may be many over: on lab-B (2026-11-12 02:52, 00.05c) "west 17 (to +// wall)" from (58, 26) stopped after 2400 ms, read as "about 13 tiles", +// and the route went on from (44..46, 26) alone. Any tile of the leg up to +// a tile past the count fits; walk back to the wall behind us to find +// which, then plan again. +LegNext RouteWalker::stopped_well_short(LegWalk& walk, size_t walked_tiles, bool first_moved){ + const Blocker blocker = talk_to_blocker(walk.label); + std::vector stopped; + for (const TilePos& pos : m_from){ + if (!first_moved){ + stopped.push_back(pos); + } + const std::vector path = leg_path(m_grid, pos, walk.planned.leg.direction, walked_tiles + 1); + stopped.insert(stopped.end(), path.begin(), path.end()); + } + stopped = sorted_unique(std::move(stopped)); + if (stopped.empty()){ + fail(m_io, walk.label + ": stopped well short of the wall, and no tile fits."); + } + if (stopped.size() == 1){ + mark_blocker(walk, stopped, blocker); + return replan_from(walk, std::move(stopped), "stopped well short of the wall"); + } + std::vector back = back_to_the_wall(walk, stopped, blocker); + // No safe wall behind: the tiles round the count, as before; the whole + // leg across a map's edge left Route 204's walk with no plan (sim). + if (back.empty()){ + return stopped_near_count(walk, walked_tiles, blocker); + } + const LegNext next = replan_from(walk, std::move(back), "back at the wall behind the stop"); + m_facing = Direction(((int)walk.planned.leg.direction + 2) % 4); + return next; +} + +// From several tiles a stop may be on, D-pad taps back to the wall the leg +// came from: a tap moves exactly a tile, so the taps that moved tell which +// tile we stopped on, and whatever stopped us stands on the next tile on. +// Returns where the taps leave us (each stop tile the taps' moved tiles +// back, if they stopped short of every wall), or nothing, before any tap, +// when there is no safe wall behind (a warp or someone walking about on +// the way). +std::vector RouteWalker::back_to_the_wall(const LegWalk& walk, const std::vector& stopped, Blocker blocker){ + const Direction ahead = walk.planned.leg.direction; + const Direction back = Direction(((int)ahead + 2) % 4); + size_t most = 0; + for (const TilePos& pos : stopped){ + const WallHit hit = slide_to_wall(m_grid, pos, back); + const std::vector path = leg_path(m_grid, pos, back, hit.tiles); + if (!hit.anchored || path.size() != hit.tiles || std::any_of(path.begin(), path.end(), [&](const TilePos& tile){ + return recovery_hazard(m_grid, tile); + })){ + return {}; + } + most = std::max(most, hit.tiles); + } + if (most == 0){ + return {}; + } + m_io.note(std::format("[Route] {}: stopped on one of {} tiles; {} taps back to the wall to find which.", + walk.label, stopped.size(), most), RouteNote::WARNING); + turn(m_io, back); + const WalkResult taps = m_io.walk_taps(back, most, walk.label + ": back to the wall"); + if (taps.stop != WalkStop::BLOCKED && taps.stop != WalkStop::LIMIT){ + fail(m_io, walk.label + ": " + walk_stop_name(taps.stop) + " on the taps back to the wall."); + } + const size_t moved = std::min(most, taps.moved_chunks); + std::vector found; + std::vector now; + std::vector shifted; + for (const TilePos& pos : stopped){ + const WallHit hit = slide_to_wall(m_grid, pos, back); + if (hit.tiles == moved && taps.wild_battles == 0){ + found.push_back(pos); + now.push_back(hit.stop); + } + const std::vector path = leg_path(m_grid, pos, back, moved); + if (path.size() == moved){ + shifted.push_back(moved == 0 ? pos : path.back()); + } + } + if (now.empty() && shifted.empty()){ + fail(m_io, walk.label + ": the taps back to the wall fit no tile."); + } + if (found.size() == 1){ + mark_blocker(walk, found, blocker); + } + return sorted_unique(now.empty() ? std::move(shifted) : std::move(now)); +} + +LegNext RouteWalker::check_at_wall(LegWalk& walk, size_t walked_tiles, size_t expected_tiles){ if (expected_tiles != SIZE_MAX && expected_tiles > 0 && walked_tiles != SIZE_MAX){ if (walked_tiles < min_tiles_at_wall(expected_tiles)){ fail(m_io, walk.label + ": ran out of time well short of the wall (" + @@ -563,7 +740,15 @@ void RouteWalker::check_at_wall(LegWalk& walk, size_t walked_tiles, size_t expec // tiles from Platinum's, r242a), and a leg to a wall ends at its // wall however far it got. m_io.note(walk.label + ": a trainer stopped us on the way; battling.", RouteNote::STATUS); - m_trainers.fight("a trainer on the way"); + const bool battled = m_trainers.fight("a trainer on the way"); + if (heard_out_a_talk(walk.label) || !battled){ + // No battle, seen in the fight or after: someone who only talks and may stand in + // the way (00.05c's Wonder Station woman, lab-B 2026-11-12: at 04:58 the walk on + // ran into her; at 06:12 her talk had ended in the fight and the walk on restarted it). + std::vector stopped = talk_stop_tiles(walk, walked_tiles == SIZE_MAX ? SIZE_MAX : walked_tiles + check.moved_chunks); + count_unexplained_talk(walk.label, stopped); + return replan_after_talk(walk, std::move(stopped)); + } m_io.note("[Route] " + walk.label + ": on to the wall after the battle.", RouteNote::WARNING); walk.result = m_io.walk_to_wall({.direction = walk.planned.leg.direction, .limit = wall_limit(walk.planned.leg), .label = walk.label + ": on to the wall", .options = run_watching(walk)}); @@ -578,20 +763,26 @@ void RouteWalker::check_at_wall(LegWalk& walk, size_t walked_tiles, size_t expec fail(m_io, walk.label + ": ran out of time short of the wall, and walking on gets " + walk_stop_name(check.stop) + ": not where the plan has us."); } + return LegNext::NEXT_LEG; } // After a battle on a leg to a wall: a leg to a wall ends at its wall // however far it got, so walk on to it and carry on. -void RouteWalker::on_to_wall(LegWalk& walk){ +LegNext RouteWalker::on_to_wall(LegWalk& walk){ m_io.note("[Route] " + walk.label + ": on to the wall after the battle.", RouteNote::WARNING); const Milliseconds limit = wall_limit(walk.planned.leg); walk.result = m_io.walk_to_wall({.direction = walk.planned.leg.direction, .limit = limit, .label = walk.label + ": on to the wall", .options = run_watching(walk)}); - if (!finish_interrupted_wall(walk, limit) && walk.result.stop != WalkStop::BLOCKED){ - check_at_wall(walk); + std::optional next = finish_interrupted_wall(walk, limit); + if (!next && walk.result.stop != WalkStop::BLOCKED){ + next = check_at_wall(walk); + } + if (next && *next != LegNext::NEXT_LEG){ + return *next; } m_from = walk.planned.after; log_leg_done(m_io, m_maps, m_plan.label, walk.planned, 0); + return LegNext::NEXT_LEG; } @@ -806,16 +997,17 @@ LegNext RouteWalker::unknown_trainer(LegWalk& walk, const std::vector 2){ - fail(m_io, walk.label + ": dialogue the map does not explain stopped us 3 times."); - } + count_unexplained_talk(walk.label, anywhere); m_io.note(m_plan.label + ": a trainer the map lacks stopped us; fighting.", RouteNote::STATUS); - m_trainers.fight("unknown trainer"); + const bool battled = m_trainers.fight("unknown trainer"); + if (heard_out_a_talk(walk.label) || !battled){ + if (!walk.planned.leg.ends_at_wall && anywhere.size() > 1){ + anywhere = open_leg_talk_tiles(walk); + } + return replan_after_talk(walk, std::move(anywhere)); + } if (walk.planned.leg.ends_at_wall){ - on_to_wall(walk); - return LegNext::NEXT_LEG; + return on_to_wall(walk); } m_from = std::move(anywhere); return LegNext::REPLAN; @@ -847,8 +1039,7 @@ LegNext RouteWalker::known_trainer(LegWalk& walk, const std::vector& near){ + if (++m_unexplained_talks > (m_plan.greeter_near(near) ? 6u : 2u)){ + fail(m_io, std::format("{}: dialogue the map does not explain stopped us {} times.", leg_label, m_unexplained_talks)); + } +} + +// After the fight with whoever stopped us: a dialogue still showing, or +// back within 5 s, is a talk no battle followed (00.05c's Wonder Station +// woman, lab-B 2026-11-12 04:58: 2 s of B left her explanation on screen). +// Presses through it again while it shows, until the overworld is quiet +// 5 s. Returns whether there was such a talk. +bool RouteWalker::heard_out_a_talk(const std::string& leg_label){ + size_t presses = 0; + while (m_io.dialog_within(HEAR_OUT_QUIET, COLOR_YELLOW)){ + if (++presses > HEAR_OUT_PRESSES){ + fail(m_io, std::format("{}: a talk no battle followed still showed after {} press-throughs.", leg_label, HEAR_OUT_PRESSES)); + } + m_io.note(leg_label + ": no battle followed the talk; hearing it out.", RouteNote::STATUS); + m_trainers.fight("someone talking"); + } + return presses > 0; +} + +// Where a talk stopped a leg to a wall: between `walked_tiles` on, if +// counted (SIZE_MAX if not), and the wall. +std::vector RouteWalker::talk_stop_tiles(const LegWalk& walk, size_t walked_tiles) const{ + std::vector stopped; + for (const TilePos& pos : m_from){ + const std::vector path = leg_path(m_grid, pos, walk.planned.leg.direction, SIZE_MAX); + const size_t first = walked_tiles == SIZE_MAX ? 0 : std::min(walked_tiles, path.size()); + if (first == 0){ + stopped.push_back(pos); + } + stopped.insert(stopped.end(), path.begin() + (first == 0 ? 0 : first - 1), path.end()); + } + return stopped; +} + +// Where a talk no battle followed stopped a tapped leg whose taps were not +// counted: any tile from where it began to where it ends, none past it. A +// talk freezes us where we stand and the taps after it move nobody; unlike +// a trainer's, it has no walk over to wait out (after_leg()'s extra tile). +std::vector RouteWalker::open_leg_talk_tiles(const LegWalk& walk) const{ + std::vector stopped = m_from; + for (const TilePos& pos : m_from){ + const std::vector path = leg_path(m_grid, pos, walk.planned.leg.direction, walk.planned.leg.tiles); + stopped.insert(stopped.end(), path.begin(), path.end()); + } + return sorted_unique(std::move(stopped)); +} + +// After a talk no battle followed, from the tiles it may have stopped us +// on: whoever talked may stand on the tile ahead, open on our map, so it +// comes off the map (one tile only), and the walk plans again from there +// instead of walking on into them. +LegNext RouteWalker::replan_after_talk(const LegWalk& walk, std::vector stopped){ + stopped = sorted_unique(std::move(stopped)); + if (stopped.size() == 1 && slide_to_wall(m_grid, stopped[0], walk.planned.leg.direction).tiles > 0){ + mark_blocker(walk, stopped, Blocker::PERSON); + } + return replan_from(walk, std::move(stopped), "heard out a talk no battle followed"); +} + // What stopped us on one tile is on the next; a person's comes off our map: no wall (00.05c). void RouteWalker::mark_blocker(const LegWalk& walk, const std::vector& stopped, Blocker blocker){ if (stopped.size() != 1){ @@ -977,7 +1234,6 @@ RouteEnd walk_route_to_warp( } - namespace{ // Waits for the way at most this often in one counted leg: Oreburgh's diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h index 3e8f138641..3a62bcd0df 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h @@ -11,6 +11,7 @@ #ifndef PokemonAutomation_PokemonBDSP_AutoStory_PlannedRoute_H #define PokemonAutomation_PokemonBDSP_AutoStory_PlannedRoute_H +#include #include #include #include @@ -33,8 +34,9 @@ namespace PokemonBDSP{ // Called when a trainer's dialogue has opened, or when someone standing in // the way was talked to. Fights the battle if one starts, and returns to the -// overworld either way. -using RouteTrainerFight = std::function; +// overworld either way. Returns whether a battle followed: a talk with +// none is someone who only talks, and may stand in the way. +using RouteTrainerFight = std::function; // The trainers a route walk may meet, and how to fight them. A trainer who // is fought is taken off `sights`. @@ -61,6 +63,19 @@ struct RouteWalkPlan{ // `goal` is that scene's trigger (ROUTE_ONTO_TRIGGER): a walk stops on // it when the scene starts, as at a wall. bool goal_is_trigger = false; + // Ground where someone walks up and talks whenever we come near, then + // walks off (00.05c: the Wonder Station's greeter in Jubilife's plaza, + // lab-B 2026-11-12, again on every unsaved run). A dialogue no trainer + // explains while we may stand here is theirs: played out and walked on + // from, not counted against the walk as a scene the map lacks. + std::vector greeter = {}; + + // Whether any of `tiles` is the greeter's ground. + bool greeter_near(const std::vector& tiles) const{ + return std::ranges::any_of(tiles, [this](const Pokemon::TilePos& pos){ + return std::ranges::find(greeter, pos) != greeter.end(); + }); + } }; // Walks from any of the world tiles `from` of `maps` until every tile the