diff --git a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp index 97e85e6e14..1903e9b9a0 100644 --- a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp @@ -8,10 +8,12 @@ #include "Inference/PokemonBDSP_DialogDetector.h" #include "Inference/PokemonBDSP_MenuDetector.h" #include "Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.h" +#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_FrozenTrainer_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_RoutePlanner_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_Segment00_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.h" #include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryActions_Tests.h" @@ -39,12 +41,14 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStory_RoutePlanner(database); add_tests_AutoStory_RouteBench(database); add_tests_AutoStory_StoryMaps(database); + add_tests_AutoStory_Segment00(database); add_tests_AutoStory_StoryData(database); add_tests_AutoStory_StoryTable(database); add_tests_AutoStory_Walk(database); add_tests_AutoStory_WildBattlePolicy(database); add_tests_AutoStory_PlannedRoute(database); add_tests_AutoStory_SimulatedRoute(database); + add_tests_AutoStory_FrozenTrainer(database); add_tests_AutoStory_WallBattleSim(database); add_tests_AutoStory_StoryActions(database); add_tests_StarterRng(database); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Segment00Lines.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Segment00Lines.cpp new file mode 100644 index 0000000000..4c8b844243 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Segment00Lines.cpp @@ -0,0 +1,146 @@ +/* BDSP AutoStory - Segment 00's walked lines + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP_AutoStory_StoryMaps.h" +#include "PokemonBDSP_AutoStory_Segment00Lines.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ +namespace Segment00Lines{ + +using Pokemon::TilePos; + + + + +// Offsets are the decomp's world coordinates of each map's tile (0, 0): +// the Sinnoh matrix's cells are 32 tiles square, the Lakefront is cell +// (column 2, row 26), Route 201 cells (3..4, 26), Twinleaf (3, 27) and +// Sandgem (5, 26). The Route201 map leaves off the cell's seven wall rows +// at the top, so it starts 7 rows lower; story_map("SandgemTown") is its whole +// cell. +const StoryWorld& segment00_maps(){ + static const StoryWorld maps = []{ + StoryWorld world; + world.add(MAP_TWINLEAF_TOWN, story_map("TwinleafTown"), {96, 864}); + world.add(MAP_ROUTE_201, story_map("Route201"), {96, 839}); + world.add(MAP_VERITY_LAKEFRONT, story_map("VerityLakefront"), {64, 832}); + world.add(MAP_SANDGEM_TOWN, story_map("SandgemTown"), {160, 832}); + return world; + }(); + return maps; +} + + +// From the Diamond decomp's coord events (zone_event narc_0389 Twinleaf, +// 0327 Route 201, 0319 Verity Lakefront, 0396 Sandgem; Lake Verity's +// narc_0298 has none: its scene is the map's own script, run on arrival). +// Each is a rectangle in world tiles with the story variable and value it +// fires at; Platinum's readable events files name the variables and carry +// the same rectangles for Twinleaf and the Lakefront. Listed as local +// tiles of our grids. Not here, having no grid: the player's house's door +// tile (6, 10), where Mom's warning fires on the way out (narc_0392, value +// 1). +const std::vector& scene_triggers(){ + static const std::vector triggers{ + {"Barry's door", MAP_TWINLEAF_TOWN, {9, 12}, 1, 1, + "before Barry has burst out of his house (var 0x40e6 = 0)"}, + {"the guitarist stops us leaving", MAP_TWINLEAF_TOWN, {12, 3}, 8, 1, + "between Barry's door and his talk upstairs (var 0x4070 = 1; 2 after it)"}, + {"Barry's tall-grass warning", MAP_ROUTE_201, {14, 18}, 6, 1, + "the first time north of Twinleaf (var 0x4086 = 0)"}, + {"Route 201's row 19 after the warning", MAP_ROUTE_201, {14, 19}, 6, 1, + "after the warning and before the lake (var 0x4086 = 1); its script is Diamond's binary, not read"}, + {"Rowan and Dawn on the way back", MAP_ROUTE_201, {5, 14}, 1, 3, + "after the lake (var 0x4086 = 2)"}, + {"Barry at the lake's entrance", MAP_VERITY_LAKEFRONT, {16, 12}, 2, 1, + "the first visit (var 0x4082 = 0): he hears Rowan and the two walk into Lake Verity; the same tiles fire again at 3, later in the story"}, + {"Dawn at Sandgem's entrance", MAP_SANDGEM_TOWN, {4, 11}, 1, 5, + "the first time in from Route 201 (var 0x4071 = 0): she leads us to Rowan's lab"}, + }; + return triggers; +} + +bool trigger_covers(const SceneTrigger& trigger, const MapTile& tile){ + return tile.map == trigger.map && + tile.pos.x >= trigger.corner.x && tile.pos.x < trigger.corner.x + trigger.width && + tile.pos.y >= trigger.corner.y && tile.pos.y < trigger.corner.y + trigger.height; +} + + + +// Corners are chosen on the maps' features (a row clear of the houses, +// the exit path, a map's last tile, the tile a scene fires on); where a +// straight run is longer than the planner's 12-tile open leg (the row +// through the Lakefront, the row along Route 201) it is split at the +// twelfth tile. The lower level of Route 201 (rows 13 to 16) is the way +// both to and from the lake: the upper level's ledges only drop onto it. +// Row 16 is used westward and row 14 eastward: on the way out Rowan and +// the counterpart are not yet on (8, 14..15) and Barry is gone from (16, +// 16), but 16 keeps the line one row off the Breeder's range; on the way +// back 14 reaches Rowan's column with the fewest corners. +// +// What a new-game run must confirm before the segment switches to these: +// where each scene leaves the player (Barry's warning and Rowan's scene +// are assumed to leave us on the trigger tile; the lake's scene to warp +// us back onto (17, 12), the right-hand entrance tile, facing south), and +// that the stretches through tall grass are walked with the wild-battle +// handling the replay's walk_through_grass() has, which walk_waypoints() +// does not. +namespace{ +// Corners on each map, and a line from them: parentheses keep the table +// flat enough for the code-health linter's brace limit. +MapTile twinleaf(const TilePos& pos){ return {MAP_TWINLEAF_TOWN, pos}; } +MapTile route201(const TilePos& pos){ return {MAP_ROUTE_201, pos}; } +MapTile lakefront(const TilePos& pos){ return {MAP_VERITY_LAKEFRONT, pos}; } +MapTile sandgem(const TilePos& pos){ return {MAP_SANDGEM_TOWN, pos}; } +// The same, at a named point of the map. +MapTile twinleaf_point(std::string_view point){ return twinleaf(map_point("TwinleafTown", point)); } +MapTile route201_point(std::string_view point){ return route201(map_point("Route201", point)); } +MapTile lakefront_point(std::string_view point){ return lakefront(map_point("VerityLakefront", point)); } +MapTile sandgem_point(std::string_view point){ return sandgem(map_point("SandgemTown", point)); } +WalkedLine walked( + std::string checkpoint, std::string name, std::vector corners, + std::string ends_in, std::vector live_triggers +){ + return {std::move(checkpoint), std::move(name), std::move(corners), std::move(ends_in), std::move(live_triggers)}; +} + +} // namespace + +const std::vector& lines(){ + static const std::vector lines{ + walked("00.01", "the house's doorstep to Barry's door", { + twinleaf_point("house_doorstep"), twinleaf({13, 22}), twinleaf({13, 12}), twinleaf_point("barry_doorstep"), + }, "Barry's door", {"Barry's door"}), + walked("00.01", "Barry's doorstep to Route 201", { + twinleaf_point("barry_doorstep"), twinleaf({15, 12}), twinleaf_point("twinleaf_north_exit"), route201_point("barry_warning"), + }, "Barry's tall-grass warning", {"Barry's tall-grass warning"}), + walked("00.01", "Route 201's west half to the lake's entrance", { + route201_point("barry_warning"), route201({14, 18}), route201({14, 16}), route201({0, 16}), + lakefront({20, 23}), lakefront({20, 21}), lakefront({17, 21}), lakefront_point("lake_entrance"), + }, "Barry at the lake's entrance", {"Barry at the lake's entrance", "Route 201's row 19 after the warning"}), + walked("00.03", "the lake's entrance back to Rowan", { + lakefront_point("lake_entrance"), lakefront({17, 21}), lakefront({29, 21}), route201_point("rowan_stop"), + }, "Rowan and Dawn on the way back", {"Rowan and Dawn on the way back"}), + // Row 13, a row above the Breeder, who wanders (26..28, 14..16): no + // route runs along a wander zone (plan_robust_route()). + walked("00.03", "Rowan to Dawn at Sandgem's entrance", { + route201_point("rowan_stop"), route201({17, 14}), route201({17, 13}), route201({29, 13}), route201({41, 13}), + route201({41, 7}), route201({42, 7}), route201({42, 5}), route201({52, 5}), + route201_point("route_201_east_end"), sandgem_point("dawn_stop"), + }, "Dawn at Sandgem's entrance", {"Dawn at Sandgem's entrance"}), + }; + return lines; +} + +} // namespace Segment00Lines +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Segment00Lines.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Segment00Lines.h new file mode 100644 index 0000000000..a814de841c --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Segment00Lines.h @@ -0,0 +1,77 @@ +/* BDSP AutoStory - Segment 00's walked lines + * + * From: https://github.com/PokemonAutomation/ + * + * Checkpoints 00.01 to 00.03 (the house, Twinleaf, Route 201, the Verity + * Lakefront, the lake, and back to Sandgem) replay recorded stick holds + * today. These are the same walks as lines on the maps: corner to corner, + * each stretch one straight leg, ending on the tile that starts the next + * scene. The scenes' trigger rectangles come from the Diamond decomp's + * events files, so a test can show each line steps into its scene on its + * last tile and not before. Nothing runs them on the console yet (task + * #22); the segment keeps its replays until a new-game run has checked + * the lines. + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Segment00Lines_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Segment00Lines_H + +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP_AutoStory_StoryWorld.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ +namespace Segment00Lines{ + + +// Map names in segment00_maps(); Route 201 and Sandgem Town go by +// StoryMaps' MAP_ROUTE_201 and MAP_SANDGEM_TOWN. +inline const std::string MAP_TWINLEAF_TOWN = "Twinleaf Town"; +// Verity Lakefront overworld map name. +inline const std::string MAP_VERITY_LAKEFRONT = "Verity Lakefront"; + +// Twinleaf Town, Route 201, the Verity Lakefront and Sandgem Town joined on +// one grid at their world offsets from the decomp. Lake Verity is not in +// it: its own matrix, and the scene walks us in and the game warps us out, +// so no line is walked there. +const StoryWorld& segment00_maps(); + +// A rectangle of tiles that starts a scene when the player steps in while +// the story is in `when`. From the decomp's coord events, as local tiles. +struct SceneTrigger{ + std::string name; + std::string map; + Pokemon::TilePos corner; // north-west tile + int width; + int height; + std::string when; // the story state it fires in +}; +// Returns all scene trigger zones for Segment 00. +const std::vector& scene_triggers(); +// Returns whether given map tile falls within the scene trigger zone. +bool trigger_covers(const SceneTrigger& trigger, const MapTile& tile); + +// A line walked corner to corner: every stretch between two corners is one +// straight open leg (whole tiles, as the D-pad walks them). +struct WalkedLine{ + std::string checkpoint; + std::string name; + std::vector corners; // the first is where the line starts + std::string ends_in; // the scene the last corner steps into, "" for none + std::vector live_triggers; // the scenes armed while this line is walked +}; +// Returns all pre-defined walked route lines for Segment 00. +const std::vector& lines(); + +// The route records start and end on named points of the maps (Twinleaf +// Town's house_doorstep, Route 201's barry_warning, ...), all on the lines. + + +} // namespace Segment00Lines +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Segment00_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Segment00_Tests.cpp new file mode 100644 index 0000000000..d417b27ea2 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Segment00_Tests.cpp @@ -0,0 +1,252 @@ +/* BDSP AutoStory Segment 00 Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Data/PokemonBDSP_AutoStory_Maps.h" +#include "Data/PokemonBDSP_AutoStory_RobustRoute.h" +#include "Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "Data/PokemonBDSP_AutoStory_Segment00Lines.h" +#include "Data/PokemonBDSP_AutoStory_StoryMaps.h" +#include "Data/PokemonBDSP_AutoStory_PlanCosts.h" +#include "PokemonBDSP_AutoStory_Segment00_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +using Pokemon::TilePos; +using Segment00Lines::SceneTrigger; +using Segment00Lines::WalkedLine; + +std::string describe(const MapTile& tile){ + return tile.map + " " + Pokemon::to_string(tile.pos); +} + +const SceneTrigger* find_trigger(const std::string& name){ + for (const SceneTrigger& trigger : Segment00Lines::scene_triggers()){ + if (trigger.name == name){ + return &trigger; + } + } + return nullptr; +} + +// Plans one stretch of a line with the waypoint costs and returns the leg, +// or an error. The stretch must be the one straight leg a human took: to a +// wall or open, as the grid has it, ending exactly on the corner. +struct Stretch{ + TileLeg leg; + std::string error; +}; +Stretch plan_stretch(const StoryWorld& maps, const TilePos& from, const TilePos& to, const std::string& name){ + const auto plan = plan_robust_route(maps, std::vector{from}, + [&](const TilePos& pos){ return pos == to; }, waypoint_plan_costs()); + if (!plan){ + return {{}, name + ": no plan."}; + } + if (plan->size() != 1){ + std::string text = name + ": " + std::to_string(plan->size()) + " legs, expected the one straight leg:"; + for (const PlannedLeg& leg : *plan){ + text += " " + to_string(leg.leg); + } + return {{}, text}; + } + const PlannedLeg& leg = (*plan)[0]; + if (leg.after.size() != 1 || !(leg.after[0] == to)){ + return {{}, name + ": the leg " + to_string(leg.leg) + " does not end exactly on the corner."}; + } + return {leg.leg, ""}; +} + +// Every tile a line stands on, its start first, as local tiles; logs each +// stretch's leg. Empty with `error` set when a stretch does not plan. +std::vector line_tiles(Logger& logger, const StoryWorld& maps, const WalkedLine& line, std::string& error){ + const std::string name = line.checkpoint + " " + line.name; + std::vector tiles{line.corners[0]}; + TilePos at = maps.to_world(line.corners[0].map, line.corners[0].pos); + std::string text = name + ": from " + describe(line.corners[0]); + for (size_t i = 1; i < line.corners.size(); i++){ + const MapTile& corner = line.corners[i]; + const TilePos goal = maps.to_world(corner.map, corner.pos); + const Stretch stretch = plan_stretch(maps, at, goal, name + " stretch " + std::to_string(i) + " to " + describe(corner)); + if (!stretch.error.empty()){ + error = stretch.error; + return {}; + } + text += "\n " + to_string(stretch.leg) + " to " + describe(corner); + for (size_t k = 0; k < stretch.leg.tiles; k++){ + at = at.next(stretch.leg.direction); + const std::optional local = maps.to_local(at); + if (!local){ + error = name + ": the line leaves the maps at " + Pokemon::to_string(at) + "."; + return {}; + } + tiles.push_back(*local); + } + } + logger.log(text); + return tiles; +} + +// The last tile steps into the line's scene; no earlier tile is in any +// scene armed while the line is walked (an inert one is only logged). +std::string check_triggers(Logger& logger, const WalkedLine& line, const std::vector& tiles){ + const std::string name = line.checkpoint + " " + line.name; + for (const std::string& live : line.live_triggers){ + if (find_trigger(live) == nullptr){ + return name + ": no scene trigger named \"" + live + "\"."; + } + } + const auto armed = [&](const std::string& trigger){ + return std::ranges::find(line.live_triggers, trigger) != line.live_triggers.end(); + }; + if (!line.ends_in.empty() && !armed(line.ends_in)){ + return name + ": the scene it ends in, \"" + line.ends_in + "\", is not among its live triggers."; + } + for (size_t i = 0; i < tiles.size(); i++){ + for (const SceneTrigger& trigger : Segment00Lines::scene_triggers()){ + if (!Segment00Lines::trigger_covers(trigger, tiles[i])){ + continue; + } + const bool live = armed(trigger.name); + const bool last = i + 1 == tiles.size(); + logger.log(name + ": tile " + std::to_string(i + 1) + " of " + std::to_string(tiles.size()) + ", " + describe(tiles[i]) + + ", is in \"" + trigger.name + "\" (" + (live ? "armed" : "inert: " + trigger.when) + ")."); + if (live && !(last && trigger.name == line.ends_in)){ + return name + ": steps into the armed scene \"" + trigger.name + "\" at " + describe(tiles[i]) + ", before its end."; + } + } + } + if (!line.ends_in.empty() && !Segment00Lines::trigger_covers(*find_trigger(line.ends_in), tiles.back())){ + return name + ": its last tile " + describe(tiles.back()) + " is not in \"" + line.ends_in + "\"."; + } + return ""; +} + +// The points the route records start and end on, each of which must lie +// on one of the lines. +struct NamedTile{ + std::string map; // in segment00_maps() + std::string json_map; + std::string point; +}; +std::vector named_tiles(){ + const std::string& twinleaf = Segment00Lines::MAP_TWINLEAF_TOWN; + const std::string& lakefront = Segment00Lines::MAP_VERITY_LAKEFRONT; + return { + {.map = twinleaf, .json_map = "TwinleafTown", .point = "house_doorstep"}, + {.map = twinleaf, .json_map = "TwinleafTown", .point = "barry_doorstep"}, + {.map = twinleaf, .json_map = "TwinleafTown", .point = "twinleaf_north_exit"}, + {.map = MAP_ROUTE_201, .json_map = "Route201", .point = "route_201_arrival"}, + {.map = MAP_ROUTE_201, .json_map = "Route201", .point = "barry_warning"}, + {.map = lakefront, .json_map = "VerityLakefront", .point = "lakefront_arrival"}, + {.map = lakefront, .json_map = "VerityLakefront", .point = "below_lake_entrance"}, + {.map = lakefront, .json_map = "VerityLakefront", .point = "lake_entrance"}, + {.map = MAP_ROUTE_201, .json_map = "Route201", .point = "route_201_return"}, + {.map = MAP_ROUTE_201, .json_map = "Route201", .point = "rowan_stop"}, + {.map = MAP_ROUTE_201, .json_map = "Route201", .point = "route_201_east_end"}, + {.map = MAP_SANDGEM_TOWN, .json_map = "SandgemTown", .point = "sandgem_arrival"}, + {.map = MAP_SANDGEM_TOWN, .json_map = "SandgemTown", .point = "dawn_stop"}, + }; +} + +// The lines 00.01 to 00.03 would walk (Data/PokemonBDSP_AutoStory_Segment00Lines): +// every stretch plans as one straight leg on the decomp maps, and each +// line enters its scene's trigger rectangle on its last tile only. The +// named tiles the route records start from all lie on the lines. +UnitTestResult test_segment00_lines(Logger& logger){ + const StoryWorld& maps = Segment00Lines::segment00_maps(); + for (const SceneTrigger& trigger : Segment00Lines::scene_triggers()){ + if (trigger.width <= 0 || trigger.height <= 0){ + return "Scene trigger \"" + trigger.name + "\" has no tiles."; + } + maps.to_world(trigger.map, trigger.corner); // throws for a map not in the set + } + std::vector walked; + for (const WalkedLine& line : Segment00Lines::lines()){ + if (line.corners.size() < 2){ + return line.checkpoint + " " + line.name + ": a line needs two corners."; + } + std::string error; + const std::vector tiles = line_tiles(logger, maps, line, error); + if (!error.empty()){ + return error; + } + error = check_triggers(logger, line, tiles); + if (!error.empty()){ + return error; + } + walked.insert(walked.end(), tiles.begin(), tiles.end()); + } + for (const NamedTile& tile : named_tiles()){ + const MapTile named{tile.map, map_point(tile.json_map, tile.point)}; + const bool on_a_line = std::ranges::any_of(walked, [&](const MapTile& t){ + return t.map == named.map && t.pos == named.pos; + }); + if (!on_a_line){ + return std::string(tile.point) + " " + describe(named) + " is on none of the lines."; + } + } + return true; +} + + +struct Segment00Input{ + std::string name; + std::function body; +}; + +struct Segment00Expectation{ +}; + +struct Segment00Case{ + Segment00Input input; + Segment00Expectation expectation; +}; + +class Test_Segment00 : public UnitTest{ +public: + explicit Test_Segment00(Segment00Case test_case) + : UnitTest("PokemonBDSP::AutoStory - Segment 00: " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + Segment00Case m_case; +}; + +} // namespace + + +const Segment00Case SEGMENT00_CASES[] = { + { + .input = {"the lines into each scene", test_segment00_lines}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_Segment00(UnitTestDatabase& database){ + for (const auto& item : SEGMENT00_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Segment00_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Segment00_Tests.h new file mode 100644 index 0000000000..dfd010c7f4 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Segment00_Tests.h @@ -0,0 +1,27 @@ +/* BDSP AutoStory Segment 00 Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Segment00_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Segment00_Tests_H + +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + + +// Offline checks of Segment 00's walked lines against the maps and the +// scenes' trigger rectangles. +void add_tests_AutoStory_Segment00(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_FrozenTrainer_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_FrozenTrainer_Tests.cpp new file mode 100644 index 0000000000..ca3aca03be --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_FrozenTrainer_Tests.cpp @@ -0,0 +1,191 @@ +/* BDSP AutoStory Grid Simulation - Frozen Trainer Tests + * + * From: https://github.com/PokemonAutomation/ + * + * A trainer the map lacks sees the player one tile short of a leg's wall: + * the "!" freezes the player, the hold reads as stopped, as at the wall, + * and the dialogue opens later. Lab-B 2026-11-12 17:45, 01.02 by_the_gate: + * after the Youngster's battle the walk east along Route 203's row 18 was + * frozen at (54, 18), the stop was taken for the wall at (55, 18), and + * every later leg ran one column off. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_FrozenTrainer_Tests.h" +#include "PokemonBDSP_AutoStory_GridSim.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ +using namespace detail; + + +namespace{ + +using Pokemon::Direction; +using Pokemon::TilePos; + + +// Row 16 of 18, with a block between the columns 1-2 and 11-12 above it: +// east along the bottom rows to the wall, then north up column 12. +StoryWorld make_room(){ + Pokemon::MapData map; + map.name = "Test"; + map.tiles = Pokemon::Grid{14, 18, {}}; + for (int y = 0; y < 18; y++){ + for (int x = 0; x < 14; x++){ + const bool wall = x == 0 || x == 13 || y == 0 || y == 17 || (y <= 12 && x >= 3 && x <= 10); + map.tiles.cells.push_back(*Pokemon::tile_from_glyph(wall ? '#' : '.')); + } + } + StoryWorld world; + world.add("Test", map, {0, 0}); + return world; +} + +// The hold toward a wall that a trainer's sight stops reads as stopped, as +// at a wall, when the trainer is `trainer`; `seen`: whether a look right +// after finds their dialogue open. If not, it is open on the next input. +class FrozenTrainerRouteIO : public GridSimRouteIO{ +public: + FrozenTrainerRouteIO(GridSim& sim, std::vector& notes, std::string trainer, bool seen) + : GridSimRouteIO(sim, notes) + , m_sim(sim) + , m_trainer(std::move(trainer)) + , m_seen(seen) + {} + + virtual WalkResult walk_to_wall(const RouteWallWalk& walk) override{ + m_frozen = false; + const bool open = m_sim.player().dialog; + WalkResult result = GridSimRouteIO::walk_to_wall(walk); + if (!open && result.stop == WalkStop::DIALOG && m_sim.player().trainer == m_trainer){ + result.stop = WalkStop::BLOCKED; + m_frozen = !m_seen; + } + return result; + } + virtual void press(const RouteInput& input) override{ + m_frozen = false; + GridSimRouteIO::press(input); + } + virtual RouteSight sight_within(const RouteWatch& watch) override{ + RouteSight sight = GridSimRouteIO::sight_within(watch); + sight.dialog = sight.dialog && !m_frozen; + return sight; + } + +private: + GridSim& m_sim; + std::string m_trainer; + bool m_seen; + bool m_frozen = false; +}; + +struct FrozenTrainerInput{ + std::string name; + // Whether a look right after the frozen stop finds the dialogue open. + bool seen; +}; + +struct FrozenTrainerExpectation{ + // Where the route must end, by the wall at x = 12. + TilePos end; +}; + +struct FrozenTrainerCase{ + FrozenTrainerInput input; + FrozenTrainerExpectation expectation; +}; + +// East along row 16 meets a known trainer at (6, 14), whose sight covers +// (6, 15) and (6, 16); after the battle the walk on to the wall is frozen +// at (11, 16), a tile short, by a trainer the map lacks at (11, 13). The +// route must still end on (12, 4), by the wall at (12, 16) and the wall at +// (12, 1). +UnitTestResult run_frozen_trainer(Logger& logger, const FrozenTrainerCase& item){ + const StoryWorld room = make_room(); + const TilePos start{1, 16}; + const TilePos target = item.expectation.end; + const TrainerSight known{.name = "known", .pos = {6, 14}, .facing = Direction::SOUTH, .range = 2}; + const TrainerSight hidden{.name = "hidden", .pos = {11, 13}, .facing = Direction::SOUTH, .range = 3}; + GridSim sim({.world = &room, .trainers = {known, hidden}}, start, Direction::NORTH, true); + std::vector notes; + FrozenTrainerRouteIO sim_io(sim, notes, "hidden", item.input.seen); + RouteTrainers trainers{{known}, [&sim](const std::string&){ sim.end_dialogue(); return true; }}; + std::vector believed; + std::string failure; + try{ + believed = walk_route(sim_io, room, {start}, trainers, { + .goal = [&](const TilePos& pos){ return pos == target; }, + .label = "by_the_gate", + .avoid = {}, + .costs = *story_route_costs("town"), + .speed = MoveSpeed::RUN, + }); + }catch (const GridSimFailure& e){ + failure = "failed: " + e.message; + } + for (const std::string& note : notes){ + logger.log(note); + } + const TilePos at = nearest_tile(sim.player().pos); + if (!failure.empty()){ + return failure; + } + if (at != target || believed != std::vector{target}){ + return std::format("ended on ({}, {}), believed {} tile(s), not on ({}, {}).", at.x, at.y, believed.size(), target.x, target.y); + } + return true; +} + +class Test_FrozenTrainer : public UnitTest{ +public: + explicit Test_FrozenTrainer(FrozenTrainerCase item) + : UnitTest("PokemonBDSP::AutoStory - Frozen trainer: " + item.input.name) + , m_case(std::move(item)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return run_frozen_trainer(logger, m_case); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + FrozenTrainerCase m_case; +}; + +const FrozenTrainerCase FROZEN_TRAINER_CASES[] = { + {.input = {.name = "01.02, a trainer the map lacks freezes us a tile short of the wall, found on the stop (lab-B, 2026-11-12 17:45)", .seen = true}, .expectation = {.end = {12, 4}}}, + {.input = {.name = "01.02, a trainer the map lacks freezes us a tile short of the wall, talks on the next leg (lab-B, 2026-11-12 17:45)", .seen = false}, .expectation = {.end = {12, 4}}}, +}; + +} // namespace + + +void add_tests_AutoStory_FrozenTrainer(UnitTestDatabase& database){ + for (const FrozenTrainerCase& item : FROZEN_TRAINER_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_FrozenTrainer_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_FrozenTrainer_Tests.h new file mode 100644 index 0000000000..16b96729aa --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_FrozenTrainer_Tests.h @@ -0,0 +1,26 @@ +/* BDSP AutoStory Grid Simulation - Frozen Trainer Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_FrozenTrainer_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_FrozenTrainer_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + + +// A trainer the map lacks freezes the player a tile short of a wall. +void add_tests_AutoStory_FrozenTrainer(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WanderWait_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WanderWait_Tests.cpp new file mode 100644 index 0000000000..7ec43356f2 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WanderWait_Tests.cpp @@ -0,0 +1,374 @@ +/* BDSP AutoStory Wander Wait Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The Mart clown wanders (54..56, 32..34) round (55, 33). 00.05b waits + * for him in his box's end columns, 56 facing west, then 54 + * facing east (wait_for_wanderer): these run that wait on the simulation + * with him starting, or staying, on each tile of his box, and pick the + * tile to wait on first. He never steps onto the player's tile. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "Pokemon/AutoStory/Pokemon_TilePose.h" +#include "PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h" +#include "PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_GridSim.h" +#include "PokemonBDSP_AutoStory_SimWanderWait.h" +#include "PokemonBDSP_AutoStory_WanderWait_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::Direction; +using Pokemon::TileBox; +using Pokemon::TilePos; + + +namespace{ + +// Jubilife alone, with the Mart clown and the Poketch Company's clown +// wandering their boxes instead of standing on their spawns. One map at +// (0, 0): world tiles are its own. +StoryWorld clown_world(){ + Pokemon::MapData map = story_map("JubilifeCity"); + for (Pokemon::MapObject& object : map.objects){ + if (object.name == "clown_mart"){ + object.wander = jubilife_mart_clown_box(); + } + if (object.name == "clown_poketch_company"){ + object.wander = jubilife_company_clown_box(); + } + } + StoryWorld world; + world.add("JubilifeCity", map, {0, 0}); + return world; +} + +// The step `name` of row `row`. +const Pokemon::StoryStep& story_step(const std::string& row, const std::string& name){ + for (const StoryCheckpoint& checkpoint : story_checkpoints()){ + if (checkpoint.id != row){ + continue; + } + for (const Pokemon::StoryStep& step : checkpoint.steps){ + if (step.name == name){ + return step; + } + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("No step {} in row {}.", name, row)); +} + +// Where a clown wandering `box` may start: each tile of it but `player`'s. +std::vector clown_starts(const TilePos& player, const TileBox& box = jubilife_mart_clown_box()){ + std::vector starts; + for (int y = box.y0; y <= box.y1; y++){ + for (int x = box.x0; x <= box.x1; x++){ + if (TilePos{x, y} != player){ + starts.push_back({x, y}); + } + } + } + return starts; +} + +// Whether the clown walks his box or stays where he starts. +enum class ClownMoves{ + WANDERS, + STAYS, +}; + +// Which clown a wait is for. +struct Clown{ + std::string name; + TileBox box; +}; +Clown mart_clown(){ + return {"clown_mart", jubilife_mart_clown_box()}; +} +Clown company_clown(){ + return {"clown_poketch_company", jubilife_company_clown_box()}; +} + +// The wait from `start` with `who` starting on `clown`, his walk seeded +// by `seed`. +WanderWaitResult run_clown_wait( + const StoryWorld& world, const Clown& who, const TilePos& start, const TilePos& clown, uint64_t seed, + const WanderWaitPlan& plan, ClownMoves moves +){ + SimWanderer wanderer{.name = who.name, .box = who.box, .pos = clown, .seed = seed}; + if (moves == ClownMoves::STAYS){ + wanderer.step_interval = Milliseconds(0); + } + GridSim sim({.world = &world, .wanderers = {wanderer}}, start, Direction::NORTH, false); + SimWanderWait body(sim); + const WanderWaitResult result = wait_beside_wanderer(body, start, plan); + if (nearest_tile(sim.player().pos) != result.at){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("the wait counted {}, the player is on {}.", + Pokemon::to_string(result.at), Pokemon::to_string(nearest_tile(sim.player().pos)))); + } + return result; +} +// The wait for the Mart clown. +WanderWaitResult run_wait(const StoryWorld& world, const TilePos& start, const TilePos& clown, uint64_t seed, const WanderWaitPlan& plan, ClownMoves moves){ + return run_clown_wait(world, mart_clown(), start, clown, seed, plan, moves); +} + +constexpr uint64_t SEEDS = 8; + +// On lab-B (2026-11-12 11:47) 00.05b's company_clown swept row 19 below +// the Poketch Company's clown, asking north, while he walked off it: "no +// coupon question from any tile below his range". 00.05b now waits for +// him in his row (14..16, 18): from where their routes leave us, +// (15, 19), the wait talks to him wherever he starts or stays, and ends +// against the company's sign on (14, 19). +UnitTestResult test_company_clown_reached_in_his_row(Logger& logger){ + const StoryWorld world = clown_world(); + const TilePos start{15, 19}; + std::string out; + size_t runs = 0; + for (const std::string row : {"00.05b"}){ + const WanderWaitPlan plan = wander_wait_plan(story_step(row, "company_clown")); + for (const TilePos& clown : clown_starts(start, jubilife_company_clown_box())){ + for (uint64_t seed = 0; seed <= SEEDS; seed++){ + runs++; + const ClownMoves moves = seed == 0 ? ClownMoves::STAYS : ClownMoves::WANDERS; + const WanderWaitResult result = run_clown_wait(world, company_clown(), start, clown, seed, plan, moves); + if (result.end != WanderWaitEnd::TALKED || result.at != plan.exit){ + out += std::format("\n {} with the clown on {}, seed {}: ended {} on {}.", row, + Pokemon::to_string(clown), seed, (int)result.end, Pokemon::to_string(result.at)); + } + } + } + } + logger.log(std::format("{} waits run.", runs)); + return out.empty() ? UnitTestResult(true) : UnitTestResult(std::format("The wait missed the company's clown:{}", out)); +} + +// 00.05b comes up column 56 from the wall (56, 36): the wait talks to the +// clown wherever in his box he starts, over seeds 1 to 8, and ends on +// (56, 31). +UnitTestResult test_mart_clown_reached_from_column_56(Logger& logger){ + const StoryWorld world = clown_world(); + std::string out; + size_t runs = 0; + for (const auto& [row, start] : {std::pair{"00.05b", {56, 36}}}){ + const WanderWaitPlan plan = wander_wait_plan(story_step(row, "mart_clown")); + for (const TilePos& clown : clown_starts(start)){ + for (uint64_t seed = 1; seed <= SEEDS; seed++){ + runs++; + const WanderWaitResult result = run_wait(world, start, clown, seed, plan, ClownMoves::WANDERS); + if (result.end != WanderWaitEnd::TALKED || result.at != plan.exit){ + out += std::format("\n {} with the clown on {}, seed {}: ended {} on {}.", row, + Pokemon::to_string(clown), seed, (int)result.end, Pokemon::to_string(result.at)); + } + } + } + } + logger.log(std::format("{} waits run.", runs)); + return out.empty() ? UnitTestResult(true) : UnitTestResult(std::format("The wait missed the clown:{}", out)); +} + +// On lab-B (2026-11-12 11:00) the clown stayed out of reach of column 56 +// for the whole wait, on his box's west column. From either start, the +// wait talks to him on whichever tile of his box he stays. +UnitTestResult test_mart_clown_reached_when_he_stays(Logger& logger){ + const StoryWorld world = clown_world(); + std::string out; + size_t runs = 0; + for (const auto& [row, start] : {std::pair{"00.05b", {56, 36}}}){ + const WanderWaitPlan plan = wander_wait_plan(story_step(row, "mart_clown")); + for (const TilePos& clown : clown_starts(start)){ + runs++; + const WanderWaitResult result = run_wait(world, start, clown, 1, plan, ClownMoves::STAYS); + if (result.end != WanderWaitEnd::TALKED || result.at != plan.exit){ + out += std::format("\n {} with the clown staying on {}: ended {} on {}.", row, + Pokemon::to_string(clown), (int)result.end, Pokemon::to_string(result.at)); + } + } + } + logger.log(std::format("{} waits run.", runs)); + return out.empty() ? UnitTestResult(true) : UnitTestResult(std::format("The wait missed the clown:{}", out)); +} + +// The simulation's wait, counting its asks. +class CountingWanderWait : public SimWanderWait{ +public: + using SimWanderWait::SimWanderWait; + virtual bool ask() override{ + asks++; + return SimWanderWait::ask(); + } + size_t asks = 0; +}; + +// On lab-B (2026-11-12 11:03) 00.05b ended "no talk from any stand tile; +// on (56, 32)" with the clown diagonal, on (55, 33): A faced nobody, a +// dozen times. With him staying there and the wait on (56, 32), it steps +// to his row and talks within a few asks. +UnitTestResult test_mart_clown_diagonal_reached(Logger& logger){ + const StoryWorld world = clown_world(); + std::string out; + for (const std::string row : {"00.05b"}){ + const WanderWaitPlan table_plan = wander_wait_plan(story_step(row, "mart_clown")); + WanderWaitPlan plan = table_plan; + // Its east side's walk, from (56, 32) on. + std::vector& stands = plan.sides.at(0).stands; + const auto at = std::find(stands.begin(), stands.end(), TilePos{56, 32}); + if (at == stands.end()){ + return row + ": no stand on (56, 32)."; + } + std::rotate(stands.begin(), at, stands.end()); + SimWanderer clown{.name = "clown_mart", .box = jubilife_mart_clown_box(), .pos = {55, 33}, .seed = 1}; + clown.step_interval = Milliseconds(0); + GridSim sim({.world = &world, .wanderers = {clown}}, {56, 32}, Direction::WEST, false); + CountingWanderWait body(sim); + const WanderWaitResult result = wait_beside_wanderer(body, {56, 32}, plan); + logger.log(std::format("{}: ended {} after {} asks, talked on {}.", row, (int)result.end, body.asks, Pokemon::to_string(result.talked))); + if (result.end != WanderWaitEnd::TALKED || result.talked != TilePos{56, 33} || body.asks > 3){ + out += std::format("\n {}: ended {} on {} after {} asks, talked on {}.", row, (int)result.end, + Pokemon::to_string(result.at), body.asks, Pokemon::to_string(result.talked)); + } + } + return out.empty() ? UnitTestResult(true) : UnitTestResult(std::format("The wait stayed off his row:{}", out)); +} + +// On lab-B (2026-11-12 12:48) 00.05c's wait (since dropped) for the +// Poketch Company's clown ended on (16, 18) with him idling on our row out of reach. From +// either end of his row alone, the wait steps toward him a tile at a time +// inside his row and talks to him idling at the other end. +UnitTestResult test_company_clown_idling_two_tiles_away(Logger& logger){ + const StoryWorld world = clown_world(); + std::string out; + for (const std::string row : {"00.05b"}){ + const WanderWaitPlan table_plan = wander_wait_plan(story_step(row, "company_clown")); + for (const WanderWaitSide& side : table_plan.sides){ + WanderWaitPlan plan = table_plan; + plan.sides = {side}; + plan.rounds = 1; + const TilePos end = side.stands.at(0); + const TilePos clown{end.x == 14 ? 16 : 14, end.y}; + const TilePos start = end.next(Direction::SOUTH); + const WanderWaitResult result = run_clown_wait(world, company_clown(), start, clown, 1, plan, ClownMoves::STAYS); + logger.log(std::format("{} from {}: ended {} on {}.", row, Pokemon::to_string(end), (int)result.end, Pokemon::to_string(result.at))); + if (result.end == WanderWaitEnd::NO_TALK){ + out += std::format("\n {}: from {} with him on {}, no talk.", row, Pokemon::to_string(end), Pokemon::to_string(clown)); + } + } + } + return out.empty() ? UnitTestResult(true) : UnitTestResult(std::format("The wait stayed at the end:{}", out)); +} + +// How many of the clown's starts and seeds one stand tile alone talks +// to him from, facing west, within the plan's asks. +size_t talks_from(const StoryWorld& world, const TilePos& stand, const WanderWaitPlan& table_plan){ + WanderWaitPlan plan = table_plan; + plan.sides = {{.stands = {stand}, .facing = Direction::WEST}}; + plan.asks = 12; + plan.passes = 1; + plan.rounds = 1; + size_t talks = 0; + for (const TilePos& clown : clown_starts(stand)){ + for (uint64_t seed = 1; seed <= SEEDS; seed++){ + talks += run_wait(world, stand, clown, seed, plan, ClownMoves::WANDERS).end == WanderWaitEnd::TALKED ? 1 : 0; + } + } + return talks; +} + +// The first stand tile is the one of (56, 32..34) the simulation finds +// the clown level with most often. +UnitTestResult test_mart_clown_first_stand(Logger& logger){ + const StoryWorld world = clown_world(); + const WanderWaitPlan plan = wander_wait_plan(story_step("00.05b", "mart_clown")); + size_t best = 0; + int best_y = 0; + for (int y = 32; y <= 34; y++){ + const size_t talks = talks_from(world, {56, y}, plan); + logger.log(std::format("From (56, {}): {} talks of {} starts and seeds.", y, talks, clown_starts({56, y}).size() * SEEDS)); + if (talks > best){ + best = talks; + best_y = y; + } + } + for (const std::string row : {"00.05b"}){ + const TilePos first = wander_wait_plan(story_step(row, "mart_clown")).sides.at(0).stands.at(0); + if (first != TilePos{56, best_y}){ + return std::format("{} waits first on {}; the simulation talks most from (56, {}).", row, Pokemon::to_string(first), best_y); + } + } + return true; +} + +struct WanderWaitInput{ + std::string name; + std::function body; +}; +struct WanderWaitExpectation{}; +struct WanderWaitCase{ + WanderWaitInput input; + WanderWaitExpectation expectation; +}; + +class Test_AutoStoryWanderWait : public UnitTest{ +public: + explicit Test_AutoStoryWanderWait(WanderWaitCase test_case) + : UnitTest(std::format("PokemonBDSP::AutoStory - {}", test_case.input.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.body(logger); + }catch (Exception& e){ + return std::format("{}: {}", e.name(), e.message()); + } + } + +private: + WanderWaitCase m_case; +}; + +const WanderWaitCase WANDER_WAIT_CASES[] = { + {.input = {"Wander wait: the Mart clown is reached from column 56 wherever he starts (00.05b)", + test_mart_clown_reached_from_column_56}, .expectation = {}}, + {.input = {"Wander wait: the Mart clown is reached on whichever tile he stays (lab-B 2026-11-12, column 54)", + test_mart_clown_reached_when_he_stays}, .expectation = {}}, + {.input = {"Wander wait: the Mart clown diagonal to (56, 32) is reached on his row within 3 asks (lab-B 2026-11-12 11:03)", + test_mart_clown_diagonal_reached}, .expectation = {}}, + {.input = {"Wander wait: the Poketch Company's clown is reached in his row, wandering or not (lab-B 2026-11-12 11:47)", + test_company_clown_reached_in_his_row}, .expectation = {}}, + {.input = {"Wander wait: the Poketch Company's clown idling at his row's other end is reached (lab-B 2026-11-12 12:48)", + test_company_clown_idling_two_tiles_away}, .expectation = {}}, + {.input = {"Wander wait: the Mart clown's first stand tile is where the simulation meets him most", + test_mart_clown_first_stand}, .expectation = {}}, +}; + +} // namespace + + +void add_tests_AutoStory_WanderWait(UnitTestDatabase& database){ + for (const auto& item : WANDER_WAIT_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WanderWait_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WanderWait_Tests.h new file mode 100644 index 0000000000..71f8d051f2 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WanderWait_Tests.h @@ -0,0 +1,25 @@ +/* BDSP AutoStory Wander Wait Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_WanderWait_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_WanderWait_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The wait for the Mart clown on column 56 (00.05b, 00.05c), on the +// simulation with him wandering his box. +void add_tests_AutoStory_WanderWait(UnitTestDatabase& database); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/leg_trace_00.04.golden.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/leg_trace_00.04.golden.json new file mode 100644 index 0000000000..8d6959e03f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/leg_trace_00.04.golden.json @@ -0,0 +1,40 @@ +{ + "about": "Leg records of BDSP AutoStory row 00.04 run on the grid simulation (Simulation/PokemonBDSP_AutoStory_LegTrace_Tests.cpp). Rewrite with PA_UPDATE_LEG_TRACE=1.", + "legs": [ + {"checkpoint":"00.04","chunks":3,"dir":"east","end":[["Sandgem Town",29,11],["Sandgem Town",29,12]],"end_kind":"wall","event":"onto_the_path","interrupts":[],"known":"wall","leg":1,"ms":1350,"planned_end":[["Sandgem Town",29,11],["Sandgem Town",29,12]],"tiles":3}, + {"checkpoint":"00.04","chunks":0,"dir":"north","end":[["Sandgem Town",29,11]],"end_kind":"wall","event":"onto_the_path","interrupts":[],"known":"wall","leg":2,"ms":1050,"planned_end":[["Sandgem Town",29,11]],"tiles":1}, + {"checkpoint":"00.04","chunks":1,"dir":"south","end":[["Sandgem Town",29,12]],"end_kind":"open","event":"onto_the_path","interrupts":[],"known":"dead_reckoned","leg":3,"moved":1,"ms":800,"planned_end":[["Sandgem Town",29,12]],"tiles":1}, + {"checkpoint":"00.04","chunks":1,"dir":"north","end":[["Route 201",57,4],["Route 201",58,4],["Route 201",59,4],["Route 201",60,4],["Route 201",61,4],["Route 201",62,4],["Route 201",63,4]],"end_kind":"tile","event":"to_the_signs_row","interrupts":[],"known":"counted","leg":1,"moved":1,"ms":800,"planned_end":[["Route 201",57,4],["Route 201",58,4],["Route 201",59,4],["Route 201",60,4],["Route 201",61,4],["Route 201",62,4],["Route 201",63,4]],"tiles":1}, + {"checkpoint":"00.04","chunks":4,"dir":"west","end":[["Route 201",56,4]],"end_kind":"wall","event":"to_ledge_column","interrupts":[],"known":"wall","leg":1,"ms":1450,"planned_end":[["Route 201",56,4]],"tiles":7}, + {"checkpoint":"00.04","chunks":1,"dir":"south","end":[["Route 201",56,5]],"end_kind":"open","event":"to_ledge_column","interrupts":[],"known":"dead_reckoned","leg":2,"moved":1,"ms":800,"planned_end":[["Route 201",56,5]],"tiles":1}, + {"checkpoint":"00.04","chunks":1,"dir":"west","end":[["Route 201",55,5]],"end_kind":"open","event":"to_ledge_column","interrupts":[],"known":"dead_reckoned","leg":3,"moved":1,"ms":800,"planned_end":[["Route 201",55,5]],"tiles":1}, + {"checkpoint":"00.04","chunks":8,"dir":"south","end":[["Route 201",55,13]],"end_kind":"tile","event":"over_the_ledge","interrupts":[],"known":"counted","leg":1,"moved":8,"ms":6400,"planned_end":[["Route 201",55,13]],"tiles":8}, + {"checkpoint":"00.04","chunks":16,"dir":"west","end":[["Route 201",39,14]],"end_kind":"open","event":"along_route_201_road","interrupts":[],"known":"dead_reckoned","leg":1,"moved":16,"ms":12800,"planned_end":[["Route 201",39,14]],"tiles":16}, + {"checkpoint":"00.04","chunks":3,"dir":"north","end":[["Route 201",39,11]],"end_kind":"open","event":"along_route_201_road","interrupts":[],"known":"dead_reckoned","leg":2,"moved":3,"ms":2400,"planned_end":[["Route 201",39,11]],"tiles":3}, + {"checkpoint":"00.04","chunks":24,"dir":"west","end":[["Route 201",15,11]],"end_kind":"open","event":"along_route_201_road","interrupts":[],"known":"dead_reckoned","leg":3,"moved":24,"ms":19200,"planned_end":[["Route 201",15,11]],"tiles":24}, + {"checkpoint":"00.04","chunks":2,"dir":"south","end":[["Route 201",15,14]],"end_kind":"open","event":"along_route_201_road","interrupts":[],"known":"dead_reckoned","leg":4,"moved":2,"ms":1600,"planned_end":[["Route 201",15,14]],"tiles":2}, + {"checkpoint":"00.04","chunks":37,"dir":"south","end":[["Twinleaf Town",15,26]],"end_kind":"wall","event":"into_twinleaf","interrupts":[],"known":"wall","leg":1,"ms":6600,"planned_end":[["Twinleaf Town",15,26]],"tiles":37}, + {"checkpoint":"00.04","chunks":4,"dir":"north","end":[["Twinleaf Town",15,22]],"end_kind":"open","event":"into_twinleaf","interrupts":[],"known":"dead_reckoned","leg":2,"moved":4,"ms":3200,"planned_end":[["Twinleaf Town",15,22]],"tiles":4}, + {"checkpoint":"00.04","chunks":5,"dir":"east","end":[["Twinleaf Town",20,22]],"end_kind":"open","event":"to_moms_doorstep","interrupts":[],"known":"dead_reckoned","leg":1,"moved":5,"ms":4000,"planned_end":[["Twinleaf Town",20,22]],"tiles":5}, + {"checkpoint":"00.04","chunks":1,"dir":"north","end":[["PlayerHouse1F",6,8]],"end_kind":"open","event":"beside_mom","interrupts":[],"known":"dead_reckoned","leg":1,"moved":1,"ms":800,"planned_end":[["PlayerHouse1F",6,8]],"tiles":1}, + {"checkpoint":"00.04","chunks":0,"dir":"north","end":[],"end_kind":"wall","event":"into_the_east_corner","interrupts":[],"known":"wall","leg":1,"ms":0,"planned_end":[],"tiles":4}, + {"checkpoint":"00.04","chunks":0,"dir":"east","end":[],"end_kind":"wall","event":"into_the_east_corner","interrupts":[],"known":"wall","leg":2,"ms":0,"planned_end":[],"tiles":6}, + {"checkpoint":"00.04","chunks":0,"dir":"south","end":[],"end_kind":"wall","event":"into_the_east_corner","interrupts":[],"known":"wall","leg":3,"ms":0,"planned_end":[],"tiles":4}, + {"checkpoint":"00.04","chunks":1,"dir":"west","end":[["PlayerHouse1F",10,8]],"end_kind":"open","event":"into_moms_call","interrupts":[],"known":"dead_reckoned","leg":1,"moved":1,"ms":800,"planned_end":[["PlayerHouse1F",10,8]],"tiles":1}, + {"checkpoint":"00.04","chunks":1,"dir":"south","end":[["PlayerHouse1F",10,9]],"end_kind":"open","event":"into_moms_call","interrupts":[],"known":"dead_reckoned","leg":2,"moved":1,"ms":800,"planned_end":[["PlayerHouse1F",10,9]],"tiles":1}, + {"checkpoint":"00.04","chunks":4,"dir":"west","end":[],"end_kind":"wall","event":"into_moms_call","interrupts":["scene"],"known":"dialogue","leg":3,"ms":650,"note":"scene","planned_end":[["PlayerHouse1F",6,9]],"tiles":4}, + {"checkpoint":"00.04","chunks":0,"dir":"west","end":[],"end_kind":"wall","event":"to_twinleafs_west_wall","interrupts":[],"known":"wall","leg":1,"ms":0,"planned_end":[],"tiles":16}, + {"checkpoint":"00.04","chunks":18,"dir":"north","end":[["Twinleaf Town",6,4]],"end_kind":"wall","event":"short_of_the_catch_tutorial","interrupts":[],"known":"wall","leg":1,"ms":3750,"planned_end":[["Twinleaf Town",6,4]],"tiles":19}, + {"checkpoint":"00.04","chunks":8,"dir":"east","end":[["Twinleaf Town",14,4]],"end_kind":"open","event":"short_of_the_catch_tutorial","interrupts":[],"known":"dead_reckoned","leg":2,"moved":8,"ms":6400,"planned_end":[["Twinleaf Town",14,4]],"tiles":8}, + {"checkpoint":"00.04","chunks":16,"dir":"north","end":[["Route 201",14,13]],"end_kind":"wall","event":"short_of_the_catch_tutorial","interrupts":[],"known":"wall","leg":3,"ms":3450,"planned_end":[["Route 201",14,13]],"tiles":16}, + {"checkpoint":"00.04","chunks":43,"dir":"east","end":[["Route 201",57,13]],"end_kind":"wall","event":"short_of_the_catch_tutorial","interrupts":[],"known":"wall","leg":4,"ms":5350,"planned_end":[["Route 201",57,13]],"tiles":43}, + {"checkpoint":"00.04","chunks":7,"dir":"west","end":[["Route 201",50,13]],"end_kind":"open","event":"short_of_the_catch_tutorial","interrupts":[],"known":"dead_reckoned","leg":5,"moved":7,"ms":5600,"planned_end":[["Route 201",50,13]],"tiles":7}, + {"checkpoint":"00.04","chunks":7,"dir":"north","end":[["Route 201",50,6]],"end_kind":"open","event":"short_of_the_catch_tutorial","interrupts":[],"known":"dead_reckoned","leg":6,"moved":7,"ms":5600,"planned_end":[["Route 201",50,6]],"tiles":7}, + {"checkpoint":"00.04","chunks":43,"dir":"east","end":[["Sandgem Town",29,13]],"end_kind":"wall","event":"short_of_the_catch_tutorial","interrupts":[],"known":"wall","leg":7,"ms":5350,"planned_end":[["Sandgem Town",29,13]],"tiles":43}, + {"checkpoint":"00.04","chunks":2,"dir":"north","end":[["Sandgem Town",29,11]],"end_kind":"wall","event":"short_of_the_catch_tutorial","interrupts":[],"known":"wall","leg":8,"ms":1350,"planned_end":[["Sandgem Town",29,11]],"tiles":2}, + {"checkpoint":"00.04","chunks":5,"dir":"west","end":[["Sandgem Town",24,11]],"end_kind":"open","event":"short_of_the_catch_tutorial","interrupts":[],"known":"dead_reckoned","leg":9,"moved":5,"ms":4000,"planned_end":[["Sandgem Town",24,11]],"tiles":5}, + {"checkpoint":"00.04","chunks":19,"dir":"north","end":[["Route 202",25,24]],"end_kind":"wall","event":"short_of_the_catch_tutorial","interrupts":[],"known":"wall","leg":10,"ms":3900,"planned_end":[["Route 202",25,24]],"tiles":19}, + {"checkpoint":"00.04","chunks":3,"dir":"west","end":[["Route 202",22,24]],"end_kind":"wall","event":"short_of_the_catch_tutorial","interrupts":[],"known":"wall","leg":11,"ms":1350,"planned_end":[["Route 202",22,24]],"tiles":3}, + {"checkpoint":"00.04","chunks":3,"dir":"south","end":[["Route 202",22,27]],"end_kind":"open","event":"short_of_the_catch_tutorial","interrupts":[],"known":"dead_reckoned","leg":12,"moved":3,"ms":2400,"planned_end":[["Route 202",22,27]],"tiles":3} + ] +} diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 011caa9890..9f325bca8c 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1839,6 +1839,8 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RouteRecords.cpp Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RouteRecords.h + Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Segment00Lines.cpp + Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Segment00Lines.h Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMapNames.h Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.cpp Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.h @@ -1878,6 +1880,8 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RoutePlanner_Tests.cpp Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RoutePlanner_Tests.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Segment00_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Segment00_Tests.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp @@ -1912,6 +1916,8 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.h + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_FrozenTrainer_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_FrozenTrainer_Tests.h Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_LegTrace_Tests.cpp Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_LegTrace_Tests.h Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_OldStorySteps.cpp @@ -1927,6 +1933,8 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.h + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WanderWait_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WanderWait_Tests.h Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_CountedWallLeg.h Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.cpp Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h