From dbe09a798a95f87bed7eb89d6cd1bccc12341af2 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 11 Nov 2026 20:31:06 -0800 Subject: [PATCH 1/8] Pokemon Navigation: connected maps, one world type for BDSP and FRLG Maps joined by their warps and edges, with the player's place as a map and a tile of it. Where a warp or an edge leads is computed once here; whether a step is allowed stays per game. connect_by_world() joins maps placed on one world grid, for BDSP's maps, which have no connections. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../ComputerPrograms_UnitTestRunner.cpp | 2 + .../Navigation/Pokemon_ConnectedMaps.cpp | 134 ++++++++++++ .../Navigation/Pokemon_ConnectedMaps.h | 90 ++++++++ .../Pokemon_ConnectedMaps_Tests.cpp | 205 ++++++++++++++++++ .../Navigation/Pokemon_ConnectedMaps_Tests.h | 22 ++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 6 files changed, 457 insertions(+) create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.h diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 5eec7c80d7..8e3eb9c1ec 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -32,6 +32,7 @@ #include "NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.h" #include "NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h" +#include "Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.h" #include "Pokemon/Navigation/Pokemon_LegPlanner_Tests.h" #include "Pokemon/Navigation/Pokemon_LegWalker_Tests.h" #include "Pokemon/Navigation/Pokemon_MapData_Tests.h" @@ -78,6 +79,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ Pokemon::add_tests_LegWalker(ret); Pokemon::add_tests_WallRecovery(ret); Pokemon::add_tests_MapData(ret); + Pokemon::add_tests_ConnectedMaps(ret); Pokemon::add_tests_RuntimeMap(ret); Pokemon::add_tests_RouteLog(ret); Pokemon::add_tests_AutoStory(ret); diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp new file mode 100644 index 0000000000..7f802aac2d --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp @@ -0,0 +1,134 @@ +/* Connected Maps + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_ConnectedMaps.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +std::optional ConnectedMaps::find(std::string_view name) const{ + for (size_t index = 0; index < m_maps->size(); index++){ + if ((*m_maps)[index].name == name){ + return index; + } + } + return std::nullopt; +} + +std::optional ConnectedMaps::warp_arrival(const MapWarp& warp) const{ + if (!warp.to){ + return std::nullopt; + } + const std::optional destination = find(warp.to->map); + if (!destination){ + return std::nullopt; + } + const MapData& to = map(*destination); + const auto arrival = to.points.find(warp.to->point); + if (arrival == to.points.end()){ + return std::nullopt; + } + return MapPlace{*destination, arrival->second}; +} + +std::optional ConnectedMaps::across_edge(const MapPlace& from, Direction side) const{ + const TilePos here = from.tile; + for (const MapConnection& connection : map(from.map).connections){ + if (connection.side != side){ + continue; + } + const std::optional destination = find(connection.to_map); + if (!destination){ + continue; + } + const Grid& to = map(*destination).tiles; + TilePos point{here.x - connection.offset, here.y - connection.offset}; + switch (side){ + case Direction::NORTH: + point.y = to.height - 1; + break; + case Direction::SOUTH: + point.y = 0; + break; + case Direction::WEST: + point.x = to.width - 1; + break; + case Direction::EAST: + point.x = 0; + break; + } + if (to.contains(point)){ + return MapPlace{*destination, point}; + } + } + return std::nullopt; +} + +std::optional ConnectedMaps::neighbour(const MapPlace& from, Direction direction) const{ + const TilePos ahead = from.tile.next(direction); + if (map(from.map).tiles.contains(ahead)){ + return MapPlace{from.map, ahead}; + } + return across_edge(from, direction); +} + + +namespace{ + +// The side of `a` that `b` touches, and the offset of `b`'s tiles along it, +// if their rectangles on the world grid meet along a side. +std::optional touching(const MapData& a, const MapData& b){ + const TilePos pa = a.world->offset; + const TilePos pb = b.world->offset; + const int aw = a.tiles.width; + const int ah = a.tiles.height; + const int bw = b.tiles.width; + const int bh = b.tiles.height; + const bool columns_overlap = pa.x < pb.x + bw && pb.x < pa.x + aw; + const bool rows_overlap = pa.y < pb.y + bh && pb.y < pa.y + ah; + if (columns_overlap && pb.y + bh == pa.y){ + return MapConnection{Direction::NORTH, b.name, pb.x - pa.x}; + } + if (columns_overlap && pb.y == pa.y + ah){ + return MapConnection{Direction::SOUTH, b.name, pb.x - pa.x}; + } + if (rows_overlap && pb.x + bw == pa.x){ + return MapConnection{Direction::WEST, b.name, pb.y - pa.y}; + } + if (rows_overlap && pb.x == pa.x + aw){ + return MapConnection{Direction::EAST, b.name, pb.y - pa.y}; + } + return std::nullopt; +} + +} // namespace + +void connect_by_world(std::vector& maps){ + for (MapData& a : maps){ + for (const MapData& b : maps){ + if (&a == &b || !a.world || !b.world || a.world->name != b.world->name){ + continue; + } + const std::optional connection = touching(a, b); + if (!connection){ + continue; + } + const bool joined = std::ranges::any_of(a.connections, [&](const MapConnection& existing){ + return existing.to_map == b.name; + }); + if (!joined){ + a.connections.push_back(*connection); + } + } + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h new file mode 100644 index 0000000000..f23e25d06a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h @@ -0,0 +1,90 @@ +/* Connected Maps + * + * From: https://github.com/PokemonAutomation/ + * + * The one world the tile games (BDSP, FRLG) plan and walk on: maps joined by + * their warps and their edges (MapData::connections). The player's place in + * it is a map and a tile of that map, in the map's own coordinates. Where a + * warp or an edge leads is the same in both games; whether a step is + * allowed, and what the game does on arrival (FRLG walks the player out of + * a door), is each game's. + * + */ + +#ifndef PokemonAutomation_Pokemon_ConnectedMaps_H +#define PokemonAutomation_Pokemon_ConnectedMaps_H + +#include +#include +#include +#include +#include "Pokemon_Grid.h" +#include "Pokemon_MapData.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A tile of one of the connected maps: the map's index and the tile in that +// map's own coordinates. +struct MapPlace{ + size_t map = 0; + TilePos tile; + + bool operator==(const MapPlace& other) const = default; +}; + + +// A view of loaded maps as one connected world. It does not own the maps, +// which must outlive it and keep their order: MapPlace::map indexes them. +class ConnectedMaps{ +public: + explicit ConnectedMaps(const std::vector& maps) + : m_maps(&maps) + {} + + const MapData& map(size_t index) const{ + return (*m_maps)[index]; + } + // The loaded map named `name`, or nothing. + std::optional find(std::string_view name) const; + + // Where taking `warp` puts the player: its destination point. Nothing + // if it leads to a map that isn't loaded or names a point that map + // doesn't have. + std::optional warp_arrival(const MapWarp& warp) const; + + // The tile past `side` of `from`'s map that carries on `from`'s row or + // column: on the first loaded map connected on that side that has it. + // Nothing if no loaded map there has it. + std::optional across_edge(const MapPlace& from, Direction side) const; + + // The tile next to `from` in `direction`: on the same map, or across + // its edge. Nothing past an edge that leads nowhere loaded. + std::optional neighbour(const MapPlace& from, Direction direction) const; + +private: + const std::vector* m_maps; +}; + + +// Joins the maps that lie on the same world grid (MapData::world) where +// they meet: each map gets a connection on each side to every map whose +// rectangle touches that side, unless it has one to that map already. +// Maps without a world are left alone. +void connect_by_world(std::vector& maps); + + + +} // namespace Pokemon +} // namespace PokemonAutomation + + +template <> +struct std::hash{ + size_t operator()(const PokemonAutomation::Pokemon::MapPlace& place) const{ + return std::hash()(place.tile) * 31 + place.map; + } +}; + +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp new file mode 100644 index 0000000000..69ad4cbf65 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp @@ -0,0 +1,205 @@ +/* Connected Maps Tests + * + * From: https://github.com/PokemonAutomation/ + * + * A town with a route north of it and a lake east of it, joined by their + * places on one world grid (connect_by_world()), and a house the town's + * door warps into: + * + * world x: -1 0 1 2 3 4 5 + * y 0..2: [ Route (6 wide) ] + * y 3..5: [ Town (4) ][Lake] + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_ConnectedMaps.h" +#include "Pokemon_ConnectedMaps_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ +namespace{ + + +MapData open_map(std::string name, int width, int height){ + MapData map; + map.name = std::move(name); + map.tiles = Grid{width, height, std::vector((size_t)width * height, TileKind::OPEN)}; + return map; +} + +MapData on_world(MapData map, const TilePos& offset){ + map.world = MapWorld{"Test", offset}; + return map; +} + +const std::vector& test_maps(){ + static const std::vector maps = []{ + std::vector ret{ + on_world(open_map("Town", 4, 3), {0, 3}), + on_world(open_map("Route", 6, 3), {-1, 0}), + on_world(open_map("Lake", 2, 3), {4, 3}), + open_map("House", 2, 2), + }; + ret[3].points["door"] = {0, 1}; + connect_by_world(ret); + return ret; + }(); + return maps; +} + +// A tile named by its map. +struct NamedPlace{ + std::string_view map; + TilePos tile; +}; + +std::string describe(const ConnectedMaps& world, const std::optional& place){ + if (!place){ + return "nowhere"; + } + return world.map(place->map).name + " " + to_string(place->tile); +} + +// Why `actual` is not `expected`, if it is not. +std::optional mismatch(const ConnectedMaps& world, const std::optional& actual, const std::optional& expected){ + std::optional wanted; + if (expected){ + wanted = MapPlace{*world.find(expected->map), expected->tile}; + } + if (actual == wanted){ + return std::nullopt; + } + return std::format("Got {}, expected {}.", describe(world, actual), describe(world, wanted)); +} + + +struct StepInput{ + std::string_view name; + NamedPlace from; + Direction direction; +}; +struct StepCase{ + StepInput input; + std::optional expectation; +}; + +class Test_ConnectedMaps_Step : public UnitTest{ +public: + explicit Test_ConnectedMaps_Step(StepCase scenario) + : UnitTest(std::format("Pokemon::ConnectedMaps - {}", scenario.input.name)), m_scenario(std::move(scenario)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const ConnectedMaps world(test_maps()); + const StepInput& input = m_scenario.input; + const MapPlace from{*world.find(input.from.map), input.from.tile}; + if (std::optional error = mismatch(world, world.neighbour(from, input.direction), m_scenario.expectation)){ + return *error; + } + return true; + } +private: + StepCase m_scenario; +}; + +const StepCase STEP_CASES[] = { + { + .input = {.name = "a step on the same map", .from = {"Town", {1, 1}}, .direction = Direction::EAST}, + .expectation = NamedPlace{"Town", {2, 1}}, + }, + { + .input = {.name = "north onto the route, shifted by its offset", .from = {"Town", {0, 0}}, .direction = Direction::NORTH}, + .expectation = NamedPlace{"Route", {1, 2}}, + }, + { + .input = {.name = "south off the route into the town", .from = {"Route", {1, 2}}, .direction = Direction::SOUTH}, + .expectation = NamedPlace{"Town", {0, 0}}, + }, + { + .input = {.name = "east into the lake", .from = {"Town", {3, 2}}, .direction = Direction::EAST}, + .expectation = NamedPlace{"Lake", {0, 2}}, + }, + { + .input = {.name = "west into the town", .from = {"Lake", {0, 1}}, .direction = Direction::WEST}, + .expectation = NamedPlace{"Town", {3, 1}}, + }, + { + .input = {.name = "an edge no map joins", .from = {"Town", {0, 1}}, .direction = Direction::WEST}, + .expectation = std::nullopt, + }, + { + .input = {.name = "past the joined map's span", .from = {"Route", {0, 2}}, .direction = Direction::SOUTH}, + .expectation = std::nullopt, + }, + { + .input = {.name = "a map off the world grid joins nothing", .from = {"House", {1, 0}}, .direction = Direction::NORTH}, + .expectation = std::nullopt, + }, +}; + + +struct WarpInput{ + std::string_view name; + MapWarp warp; +}; +struct WarpCase{ + WarpInput input; + std::optional expectation; +}; + +class Test_ConnectedMaps_Warp : public UnitTest{ +public: + explicit Test_ConnectedMaps_Warp(WarpCase scenario) + : UnitTest(std::format("Pokemon::ConnectedMaps - {}", scenario.input.name)), m_scenario(std::move(scenario)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const ConnectedMaps world(test_maps()); + if (std::optional error = mismatch(world, world.warp_arrival(m_scenario.input.warp), m_scenario.expectation)){ + return *error; + } + return true; + } +private: + WarpCase m_scenario; +}; + +const WarpCase WARP_CASES[] = { + { + .input = {.name = "a warp to a loaded map's point", .warp = {.to = WarpDestination{"House", "door"}}}, + .expectation = NamedPlace{"House", {0, 1}}, + }, + { + .input = {.name = "a warp to a map that isn't loaded", .warp = {.to = WarpDestination{"Cave", "door"}}}, + .expectation = std::nullopt, + }, + { + .input = {.name = "a warp to a point the map lacks", .warp = {.to = WarpDestination{"House", "stairs"}}}, + .expectation = std::nullopt, + }, + { + .input = {.name = "a warp that leads nowhere", .warp = {}}, + .expectation = std::nullopt, + }, +}; + + +} // namespace + +void add_tests_ConnectedMaps(UnitTestDatabase& database){ + for (const StepCase& scenario : STEP_CASES){ + database.add(scenario); + } + for (const WarpCase& scenario : WARP_CASES){ + database.add(scenario); + } +} + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.h new file mode 100644 index 0000000000..a3d6a6c26b --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.h @@ -0,0 +1,22 @@ +/* Connected Maps Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_ConnectedMaps_Tests_H +#define PokemonAutomation_Pokemon_ConnectedMaps_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers the checks on stepping and warping between connected maps. +void add_tests_ConnectedMaps(UnitTestDatabase& database); + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index cd4fc4c225..a5f48d3bcb 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1459,6 +1459,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/Inference/Pokemon_TrainPokemonOCR.h Source/Pokemon/Inference/Pokemon_TypeReader.cpp Source/Pokemon/Inference/Pokemon_TypeReader.h + Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp + Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h + Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp + Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.h Source/Pokemon/Navigation/Pokemon_Grid.cpp Source/Pokemon/Navigation/Pokemon_Grid.h Source/Pokemon/Navigation/Pokemon_LegTypes.h From b64cd70dc406a9931b2afdeb2041db2746dd541e Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 11 Nov 2026 20:33:17 -0800 Subject: [PATCH 2/8] Pokemon Navigation: connected maps tests take an expectation struct; edge entry split out Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Navigation/Pokemon_ConnectedMaps.cpp | 41 +++++++++++-------- .../Navigation/Pokemon_ConnectedMaps.h | 1 + .../Pokemon_ConnectedMaps_Tests.cpp | 38 +++++++++-------- 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp index 7f802aac2d..73b0495f30 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp @@ -9,6 +9,30 @@ namespace PokemonAutomation{ namespace Pokemon{ +namespace{ + +// The tile of `to` that `connection` on `side` leads onto from `here`: +// the edge row or column, shifted by the connection's offset. +TilePos edge_entry(const TilePos& here, Direction side, const MapConnection& connection, const Grid& to){ + TilePos point{here.x - connection.offset, here.y - connection.offset}; + switch (side){ + case Direction::NORTH: + point.y = to.height - 1; + break; + case Direction::SOUTH: + point.y = 0; + break; + case Direction::WEST: + point.x = to.width - 1; + break; + case Direction::EAST: + point.x = 0; + break; + } + return point; +} + +} // namespace std::optional ConnectedMaps::find(std::string_view name) const{ @@ -37,7 +61,6 @@ std::optional ConnectedMaps::warp_arrival(const MapWarp& warp) const{ } std::optional ConnectedMaps::across_edge(const MapPlace& from, Direction side) const{ - const TilePos here = from.tile; for (const MapConnection& connection : map(from.map).connections){ if (connection.side != side){ continue; @@ -47,21 +70,7 @@ std::optional ConnectedMaps::across_edge(const MapPlace& from, Directi continue; } const Grid& to = map(*destination).tiles; - TilePos point{here.x - connection.offset, here.y - connection.offset}; - switch (side){ - case Direction::NORTH: - point.y = to.height - 1; - break; - case Direction::SOUTH: - point.y = 0; - break; - case Direction::WEST: - point.x = to.width - 1; - break; - case Direction::EAST: - point.x = 0; - break; - } + const TilePos point = edge_entry(from.tile, side, connection, to); if (to.contains(point)){ return MapPlace{*destination, point}; } diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h index f23e25d06a..905685db72 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h @@ -80,6 +80,7 @@ void connect_by_world(std::vector& maps); } // namespace PokemonAutomation +// A hash of MapPlace, for planners that keep places in unordered sets. template <> struct std::hash{ size_t operator()(const PokemonAutomation::Pokemon::MapPlace& place) const{ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp index 69ad4cbf65..d39c6d2a3e 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp @@ -64,11 +64,17 @@ std::string describe(const ConnectedMaps& world, const std::optional& if (!place){ return "nowhere"; } - return world.map(place->map).name + " " + to_string(place->tile); + return std::format("{} {}", world.map(place->map).name, to_string(place->tile)); } // Why `actual` is not `expected`, if it is not. -std::optional mismatch(const ConnectedMaps& world, const std::optional& actual, const std::optional& expected){ +// Where a step or a warp should put the player, or nothing for nowhere. +struct PlaceExpectation{ + std::optional place; +}; + +std::optional mismatch(const ConnectedMaps& world, const std::optional& actual, const PlaceExpectation& expectation){ + const std::optional& expected = expectation.place; std::optional wanted; if (expected){ wanted = MapPlace{*world.find(expected->map), expected->tile}; @@ -87,7 +93,7 @@ struct StepInput{ }; struct StepCase{ StepInput input; - std::optional expectation; + PlaceExpectation expectation; }; class Test_ConnectedMaps_Step : public UnitTest{ @@ -111,35 +117,35 @@ class Test_ConnectedMaps_Step : public UnitTest{ const StepCase STEP_CASES[] = { { .input = {.name = "a step on the same map", .from = {"Town", {1, 1}}, .direction = Direction::EAST}, - .expectation = NamedPlace{"Town", {2, 1}}, + .expectation = {.place = NamedPlace{"Town", {2, 1}}}, }, { .input = {.name = "north onto the route, shifted by its offset", .from = {"Town", {0, 0}}, .direction = Direction::NORTH}, - .expectation = NamedPlace{"Route", {1, 2}}, + .expectation = {.place = NamedPlace{"Route", {1, 2}}}, }, { .input = {.name = "south off the route into the town", .from = {"Route", {1, 2}}, .direction = Direction::SOUTH}, - .expectation = NamedPlace{"Town", {0, 0}}, + .expectation = {.place = NamedPlace{"Town", {0, 0}}}, }, { .input = {.name = "east into the lake", .from = {"Town", {3, 2}}, .direction = Direction::EAST}, - .expectation = NamedPlace{"Lake", {0, 2}}, + .expectation = {.place = NamedPlace{"Lake", {0, 2}}}, }, { .input = {.name = "west into the town", .from = {"Lake", {0, 1}}, .direction = Direction::WEST}, - .expectation = NamedPlace{"Town", {3, 1}}, + .expectation = {.place = NamedPlace{"Town", {3, 1}}}, }, { .input = {.name = "an edge no map joins", .from = {"Town", {0, 1}}, .direction = Direction::WEST}, - .expectation = std::nullopt, + .expectation = {.place = std::nullopt}, }, { .input = {.name = "past the joined map's span", .from = {"Route", {0, 2}}, .direction = Direction::SOUTH}, - .expectation = std::nullopt, + .expectation = {.place = std::nullopt}, }, { .input = {.name = "a map off the world grid joins nothing", .from = {"House", {1, 0}}, .direction = Direction::NORTH}, - .expectation = std::nullopt, + .expectation = {.place = std::nullopt}, }, }; @@ -150,7 +156,7 @@ struct WarpInput{ }; struct WarpCase{ WarpInput input; - std::optional expectation; + PlaceExpectation expectation; }; class Test_ConnectedMaps_Warp : public UnitTest{ @@ -172,19 +178,19 @@ class Test_ConnectedMaps_Warp : public UnitTest{ const WarpCase WARP_CASES[] = { { .input = {.name = "a warp to a loaded map's point", .warp = {.to = WarpDestination{"House", "door"}}}, - .expectation = NamedPlace{"House", {0, 1}}, + .expectation = {.place = NamedPlace{"House", {0, 1}}}, }, { .input = {.name = "a warp to a map that isn't loaded", .warp = {.to = WarpDestination{"Cave", "door"}}}, - .expectation = std::nullopt, + .expectation = {.place = std::nullopt}, }, { .input = {.name = "a warp to a point the map lacks", .warp = {.to = WarpDestination{"House", "stairs"}}}, - .expectation = std::nullopt, + .expectation = {.place = std::nullopt}, }, { .input = {.name = "a warp that leads nowhere", .warp = {}}, - .expectation = std::nullopt, + .expectation = {.place = std::nullopt}, }, }; From a0991bc871c9461542ebc1ef88163740b002f91b Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 11 Nov 2026 20:36:15 -0800 Subject: [PATCH 3/8] Pokemon Navigation: ConnectedMaps::size() Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h index 905685db72..8b8c62948f 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h @@ -43,6 +43,10 @@ class ConnectedMaps{ : m_maps(&maps) {} + // How many maps are loaded: MapPlace::map runs from 0 to size() - 1. + size_t size() const{ + return m_maps->size(); + } const MapData& map(size_t index) const{ return (*m_maps)[index]; } From 780b97bb0e2a7926dd0253badcf144636d420c54 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 11 Nov 2026 20:36:27 -0800 Subject: [PATCH 4/8] PokemonBDSP AutoStory: story worlds also as connected maps Each StoryWorld now keeps its maps as Pokemon::ConnectedMaps too, joined where they meet on its grid, so the planner can move to a map and a tile (seam 2's one world type). Nothing reads it yet; a test checks that on the story's worlds every step off a map's edge lands where the grid puts it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Data/PokemonBDSP_AutoStory_StoryWorld.cpp | 5 + .../Data/PokemonBDSP_AutoStory_StoryWorld.h | 13 ++ ...PokemonBDSP_AutoStory_StoryWorld_Tests.cpp | 121 +++++++++++++++++- 3 files changed, 138 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp index d6f7a67b80..40afe689da 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp @@ -70,6 +70,11 @@ void StoryWorld::add(std::string name, const Pokemon::MapData& map, const TilePo placed.warps.push_back({warp.x, warp.y}); } check_overlap(placed); + Pokemon::MapData connected = map; + connected.name = placed.name; + connected.world = Pokemon::MapWorld{"story", offset}; + m_connected.push_back(std::move(connected)); + Pokemon::connect_by_world(m_connected); m_maps.push_back(std::move(placed)); rebuild(); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h index cf46b6f0c5..b48bafaf10 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h @@ -11,6 +11,10 @@ * plan may not end there, but walking into one is not a wall, since it may * be an exit. * + * The same maps are also kept as connected maps (Pokemon::ConnectedMaps), + * joined where they meet on the world grid, for planning on a map and a + * tile instead of the grid. + * */ #ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryWorld_H @@ -19,6 +23,7 @@ #include #include #include +#include "Pokemon/Navigation/Pokemon_ConnectedMaps.h" #include "Pokemon/Navigation/Pokemon_Grid.h" #include "Pokemon/Navigation/Pokemon_MapData.h" @@ -71,6 +76,13 @@ class StoryWorld{ // The map that holds a world tile, or nothing between maps. std::optional to_local(const Pokemon::TilePos& world) const; + // The maps as added, under their names here, joined where they meet. + // Tiles blocked or taken off the maps here are not in them. The view + // lasts until the next add(). + Pokemon::ConnectedMaps connected() const{ + return Pokemon::ConnectedMaps(m_connected); + } + friend std::vector wandering_hazards(const StoryWorld& world); friend bool wandering_hazard(const StoryWorld& world, const Pokemon::TilePos& pos); friend bool recovery_hazard(const StoryWorld& world, const Pokemon::TilePos& pos); @@ -102,6 +114,7 @@ class StoryWorld{ Cell* cell(const Pokemon::TilePos& pos); std::vector m_maps; + std::vector m_connected; Pokemon::TilePos m_origin; // offset of world tile (0,0) Pokemon::Grid m_cells; std::vector m_blocked; // world tiles, kept across add() diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp index 99cb510515..0ae63109d2 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp @@ -3,7 +3,9 @@ * From: https://github.com/PokemonAutomation/ * * StoryWorld on small synthetic maps: what each tile does to a step, maps - * joined at offsets, and tiles blocked or taken off the maps. + * joined at offsets, and tiles blocked or taken off the maps. Also the + * story's own worlds: their connected maps step across every map edge + * where the world grid does. * */ @@ -13,8 +15,10 @@ #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Data/PokemonBDSP_AutoStory_StoryMaps.h" #include "Data/PokemonBDSP_AutoStory_StoryWorld.h" #include "Data/PokemonBDSP_AutoStory_WalkRules.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" #include "PokemonBDSP_AutoStory_StoryWorld_Tests.h" namespace PokemonAutomation{ @@ -219,10 +223,125 @@ const StoryWorldCase STORY_WORLD_CASES[] = { }, }; +namespace{ + +// The first step off a map's edge, from any of its edge tiles, that lands +// somewhere else on the connected maps than on the world grid, or nothing. +std::optional edge_mismatch(const StoryWorld& world){ + const Pokemon::ConnectedMaps connected = world.connected(); + for (size_t index = 0; index < connected.size(); index++){ + const MapData& map = connected.map(index); + for (int y = 0; y < map.tiles.height; y++){ + for (int x = 0; x < map.tiles.width; x++){ + for (Direction direction : Pokemon::ALL_DIRECTIONS){ + const TilePos from{x, y}; + if (map.tiles.contains(from.next(direction))){ + continue; + } + const std::optional grid = world.to_local(world.to_world(map.name, from).next(direction)); + const std::optional place = connected.neighbour({index, from}, direction); + const bool same = grid && place + ? grid->map == connected.map(place->map).name && grid->pos == place->tile + : !grid && !place; + if (!same){ + return std::format("{} {} {}: the grid and the connected maps disagree.", + map.name, Pokemon::to_string(from), Pokemon::direction_name(direction)); + } + } + } + } + } + return std::nullopt; +} + +struct ConnectedWorldInput{ + std::string name; + const StoryWorld& (*world)(); +}; +struct ConnectedWorldExpectation{ + // Connections over all the maps: two for each pair that meets. + size_t connections; +}; +struct ConnectedWorldCase{ + ConnectedWorldInput input; + ConnectedWorldExpectation expectation; +}; + +class Test_ConnectedStoryWorld : public UnitTest{ +public: + explicit Test_ConnectedStoryWorld(ConnectedWorldCase test_case) + : UnitTest(std::format("PokemonBDSP::AutoStory - Story world as connected maps: {}", test_case.input.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const StoryWorld& world = m_case.input.world(); + if (std::optional error = edge_mismatch(world)){ + return *error; + } + const Pokemon::ConnectedMaps connected = world.connected(); + size_t connections = 0; + for (size_t index = 0; index < connected.size(); index++){ + connections += connected.map(index).connections.size(); + } + if (connections != m_case.expectation.connections){ + return std::format("{} connections, expected {}.", connections, m_case.expectation.connections); + } + return true; + } + +private: + ConnectedWorldCase m_case; +}; + +const StoryWorld& south_plan_world(){ + return *story_plan_world("TwinleafTown", "Route202"); +} +const StoryWorld& route_203_plan_world(){ + return *story_plan_world("Route203", "JubilifeCity"); +} +const StoryWorld& route_204_south_plan_world(){ + return *story_plan_world("JubilifeCity", "Route204South"); +} +const StoryWorld& route_204_north_plan_world(){ + return *story_plan_world("Route204North", "FloaromaTown"); +} + +const ConnectedWorldCase CONNECTED_WORLD_CASES[] = { + { + .input = {"Route 201, Sandgem and Route 202", story_maps}, + .expectation = {.connections = 4}, + }, + { + .input = {"Twinleaf to Route 202", south_plan_world}, + .expectation = {.connections = 8}, + }, + { + .input = {"Route 203 into Jubilife", route_203_plan_world}, + .expectation = {.connections = 2}, + }, + { + .input = {"Jubilife up Route 204's south half", route_204_south_plan_world}, + .expectation = {.connections = 2}, + }, + { + .input = {"Route 204's north half into Floaroma", route_204_north_plan_world}, + .expectation = {.connections = 2}, + }, + { + .input = {"Jubilife alone", jubilife_maps}, + .expectation = {.connections = 0}, + }, +}; + +} // namespace + void add_tests_AutoStory_StoryWorld(UnitTestDatabase& database){ for (const auto& item : STORY_WORLD_CASES){ database.add(item); } + for (const ConnectedWorldCase& item : CONNECTED_WORLD_CASES){ + database.add(item); + } } From 40f15473e3367852aa5e775a59a94e2309de5d6e Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 11 Nov 2026 20:40:46 -0800 Subject: [PATCH 5/8] PokemonBDSP AutoStory: connected_maps() as a free function; flatter edge check Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Data/PokemonBDSP_AutoStory_StoryWorld.cpp | 4 ++ .../Data/PokemonBDSP_AutoStory_StoryWorld.h | 13 +++--- ...PokemonBDSP_AutoStory_StoryWorld_Tests.cpp | 44 ++++++++++--------- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp index 40afe689da..a3cb3e6495 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp @@ -276,6 +276,10 @@ std::optional StoryWorld::to_local(const TilePos& world) const{ +Pokemon::ConnectedMaps connected_maps(const StoryWorld& world){ + return Pokemon::ConnectedMaps(world.m_connected); +} + bool covered(const StoryWorld& world, const TilePos& pos){ return world.tile(pos).has_value(); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h index b48bafaf10..819915568b 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h @@ -76,13 +76,7 @@ class StoryWorld{ // The map that holds a world tile, or nothing between maps. std::optional to_local(const Pokemon::TilePos& world) const; - // The maps as added, under their names here, joined where they meet. - // Tiles blocked or taken off the maps here are not in them. The view - // lasts until the next add(). - Pokemon::ConnectedMaps connected() const{ - return Pokemon::ConnectedMaps(m_connected); - } - + friend Pokemon::ConnectedMaps connected_maps(const StoryWorld& world); friend std::vector wandering_hazards(const StoryWorld& world); friend bool wandering_hazard(const StoryWorld& world, const Pokemon::TilePos& pos); friend bool recovery_hazard(const StoryWorld& world, const Pokemon::TilePos& pos); @@ -122,6 +116,11 @@ class StoryWorld{ }; +// The world's maps as added, under their names in it, joined where they +// meet. Tiles blocked or taken off the maps are not in them. The view +// lasts until the world's next add(). +Pokemon::ConnectedMaps connected_maps(const StoryWorld& world); + // The rules of walking on a story world, from its tiles. // All tiles where someone wanders on the maps, in world coordinates. diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp index 0ae63109d2..647f2cbaa7 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp @@ -225,29 +225,33 @@ const StoryWorldCase STORY_WORLD_CASES[] = { namespace{ -// The first step off a map's edge, from any of its edge tiles, that lands +// Whether stepping `direction` from `from` lands on the same map and tile +// on the world grid as on the connected maps (or nowhere on both). +bool steps_agree(const StoryWorld& world, const Pokemon::MapPlace& from, Direction direction){ + const Pokemon::ConnectedMaps connected = connected_maps(world); + const std::string& name = connected.map(from.map).name; + const std::optional grid = world.to_local(world.to_world(name, from.tile).next(direction)); + const std::optional place = connected.neighbour(from, direction); + if (grid && place){ + return grid->map == connected.map(place->map).name && grid->pos == place->tile; + } + return !grid && !place; +} + +// The first step off a map's edge, from any of its tiles, that lands // somewhere else on the connected maps than on the world grid, or nothing. std::optional edge_mismatch(const StoryWorld& world){ - const Pokemon::ConnectedMaps connected = world.connected(); + const Pokemon::ConnectedMaps connected = connected_maps(world); for (size_t index = 0; index < connected.size(); index++){ - const MapData& map = connected.map(index); - for (int y = 0; y < map.tiles.height; y++){ - for (int x = 0; x < map.tiles.width; x++){ - for (Direction direction : Pokemon::ALL_DIRECTIONS){ - const TilePos from{x, y}; - if (map.tiles.contains(from.next(direction))){ - continue; - } - const std::optional grid = world.to_local(world.to_world(map.name, from).next(direction)); - const std::optional place = connected.neighbour({index, from}, direction); - const bool same = grid && place - ? grid->map == connected.map(place->map).name && grid->pos == place->tile - : !grid && !place; - if (!same){ - return std::format("{} {} {}: the grid and the connected maps disagree.", - map.name, Pokemon::to_string(from), Pokemon::direction_name(direction)); - } + const Pokemon::Grid& tiles = connected.map(index).tiles; + for (int cell = 0; cell < tiles.width * tiles.height; cell++){ + const TilePos from{cell % tiles.width, cell / tiles.width}; + for (Direction direction : Pokemon::ALL_DIRECTIONS){ + if (tiles.contains(from.next(direction)) || steps_agree(world, {index, from}, direction)){ + continue; } + return std::format("{} {} {}: the grid and the connected maps disagree.", + connected.map(index).name, Pokemon::to_string(from), Pokemon::direction_name(direction)); } } } @@ -278,7 +282,7 @@ class Test_ConnectedStoryWorld : public UnitTest{ if (std::optional error = edge_mismatch(world)){ return *error; } - const Pokemon::ConnectedMaps connected = world.connected(); + const Pokemon::ConnectedMaps connected = connected_maps(world); size_t connections = 0; for (size_t index = 0; index < connected.size(); index++){ connections += connected.map(index).connections.size(); From 371bd8d1d163f8e3b1298970f674310ed1bd7086 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 00:15:57 -0800 Subject: [PATCH 6/8] Pokemon Navigation: ConnectedMaps refuses temporaries, checks map indexes, orders MapPlace (#408 review) The rvalue constructor is deleted, map() throws InternalProgramError past the last map, and MapPlace gets a defaulted <=> over TilePos's. The test art matches the maps, the Lake to Route edge has two cases, and stale map indexes have their own. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Navigation/Pokemon_ConnectedMaps.cpp | 10 ++++ .../Navigation/Pokemon_ConnectedMaps.h | 10 +++- .../Pokemon_ConnectedMaps_Tests.cpp | 55 ++++++++++++++++++- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp index 73b0495f30..1bd697a03e 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp @@ -5,6 +5,8 @@ */ #include +#include +#include "Common/Cpp/Exceptions.h" #include "Pokemon_ConnectedMaps.h" namespace PokemonAutomation{ @@ -35,6 +37,14 @@ TilePos edge_entry(const TilePos& here, Direction side, const MapConnection& con } // namespace +const MapData& ConnectedMaps::map(size_t index) const{ + if (index >= m_maps->size()){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, + std::format("Map index {} is past the {} connected maps.", index, m_maps->size())); + } + return (*m_maps)[index]; +} + std::optional ConnectedMaps::find(std::string_view name) const{ for (size_t index = 0; index < m_maps->size(); index++){ if ((*m_maps)[index].name == name){ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h index 8b8c62948f..5fef2d16a6 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h @@ -32,6 +32,8 @@ struct MapPlace{ TilePos tile; bool operator==(const MapPlace& other) const = default; + // Map, then tile: an order for sets and maps of places. + auto operator<=>(const MapPlace& other) const = default; }; @@ -42,14 +44,16 @@ class ConnectedMaps{ explicit ConnectedMaps(const std::vector& maps) : m_maps(&maps) {} + // A temporary list would be gone by the first call. + explicit ConnectedMaps(std::vector&& maps) = delete; // How many maps are loaded: MapPlace::map runs from 0 to size() - 1. size_t size() const{ return m_maps->size(); } - const MapData& map(size_t index) const{ - return (*m_maps)[index]; - } + // The map at `index`; throws InternalProgramError past the last map, + // as from a MapPlace of another world. + const MapData& map(size_t index) const; // The loaded map named `name`, or nothing. std::optional find(std::string_view name) const; diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp index d39c6d2a3e..abcc99f641 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp @@ -7,8 +7,8 @@ * door warps into: * * world x: -1 0 1 2 3 4 5 - * y 0..2: [ Route (6 wide) ] - * y 3..5: [ Town (4) ][Lake] + * y 0..2: [ Route (6 wide) ] + * y 3..5: [ Town (4) ][Lake] * */ @@ -18,6 +18,7 @@ #include #include #include +#include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" #include "Pokemon_ConnectedMaps.h" #include "Pokemon_ConnectedMaps_Tests.h" @@ -67,12 +68,12 @@ std::string describe(const ConnectedMaps& world, const std::optional& return std::format("{} {}", world.map(place->map).name, to_string(place->tile)); } -// Why `actual` is not `expected`, if it is not. // Where a step or a warp should put the player, or nothing for nowhere. struct PlaceExpectation{ std::optional place; }; +// Why `actual` is not `expected`, if it is not. std::optional mismatch(const ConnectedMaps& world, const std::optional& actual, const PlaceExpectation& expectation){ const std::optional& expected = expectation.place; std::optional wanted; @@ -135,6 +136,14 @@ const StepCase STEP_CASES[] = { .input = {.name = "west into the town", .from = {"Lake", {0, 1}}, .direction = Direction::WEST}, .expectation = {.place = NamedPlace{"Town", {3, 1}}}, }, + { + .input = {.name = "north out of the lake onto the route's last column", .from = {"Lake", {0, 0}}, .direction = Direction::NORTH}, + .expectation = {.place = NamedPlace{"Route", {5, 2}}}, + }, + { + .input = {.name = "north out of the lake past the route's span", .from = {"Lake", {1, 0}}, .direction = Direction::NORTH}, + .expectation = {.place = std::nullopt}, + }, { .input = {.name = "an edge no map joins", .from = {"Town", {0, 1}}, .direction = Direction::WEST}, .expectation = {.place = std::nullopt}, @@ -195,6 +204,43 @@ const WarpCase WARP_CASES[] = { }; +struct StalePlaceInput{ + std::string_view name; + size_t map; +}; +struct StalePlaceCase{ + StalePlaceInput input; +}; + +// A place whose map index is past the loaded maps (from another world, or +// one that has since shrunk) is an InternalProgramError, not a read past +// the end. +class Test_ConnectedMaps_StalePlace : public UnitTest{ +public: + explicit Test_ConnectedMaps_StalePlace(StalePlaceCase scenario) + : UnitTest(std::format("Pokemon::ConnectedMaps - {}", scenario.input.name)), m_scenario(std::move(scenario)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const ConnectedMaps world(test_maps()); + const MapPlace stale{m_scenario.input.map, {0, 0}}; + try{ + const std::optional place = world.neighbour(stale, Direction::NORTH); + return std::format("Map {} of {} was read: got {}, expected an InternalProgramError.", + stale.map, world.size(), place ? to_string(place->tile) : "nowhere"); + }catch (const InternalProgramError&){ + return true; + } + } +private: + StalePlaceCase m_scenario; +}; + +const StalePlaceCase STALE_PLACE_CASES[] = { + {.input = {.name = "a map index one past the last is rejected", .map = 4}}, + {.input = {.name = "a map index far past the last is rejected", .map = 1000}}, +}; + + } // namespace void add_tests_ConnectedMaps(UnitTestDatabase& database){ @@ -204,6 +250,9 @@ void add_tests_ConnectedMaps(UnitTestDatabase& database){ for (const WarpCase& scenario : WARP_CASES){ database.add(scenario); } + for (const StalePlaceCase& scenario : STALE_PLACE_CASES){ + database.add(scenario); + } } From d76aa604da3ffd33ffb43cfc29270e8c1ba80daa Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 00:20:48 -0800 Subject: [PATCH 7/8] BDSP AutoStory: connected-world test checks for a missing plan world; connected_maps() refuses temporaries (#411 review) Each row names its plan world's two places, and a pair that is renamed or dropped fails with their names instead of dereferencing null. Test fields get defaults and designated names. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Data/PokemonBDSP_AutoStory_StoryWorld.h | 2 + ...PokemonBDSP_AutoStory_StoryWorld_Tests.cpp | 45 +++++++++---------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h index 819915568b..b7c57f01db 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h @@ -120,6 +120,8 @@ class StoryWorld{ // meet. Tiles blocked or taken off the maps are not in them. The view // lasts until the world's next add(). Pokemon::ConnectedMaps connected_maps(const StoryWorld& world); +// A temporary world would be gone before the view is used. +Pokemon::ConnectedMaps connected_maps(StoryWorld&& world) = delete; // The rules of walking on a story world, from its tiles. diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp index 647f2cbaa7..37e501f309 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" @@ -259,12 +260,16 @@ std::optional edge_mismatch(const StoryWorld& world){ } struct ConnectedWorldInput{ - std::string name; - const StoryWorld& (*world)(); + std::string_view name; + // The story's plan world between two places (story_plan_world()), or, + // if `world` is set, that world. + std::string_view plan_from = {}; + std::string_view plan_to = {}; + const StoryWorld& (*world)() = nullptr; }; struct ConnectedWorldExpectation{ // Connections over all the maps: two for each pair that meets. - size_t connections; + size_t connections = 0; }; struct ConnectedWorldCase{ ConnectedWorldInput input; @@ -278,7 +283,14 @@ class Test_ConnectedStoryWorld : public UnitTest{ , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const StoryWorld& world = m_case.input.world(); + const ConnectedWorldInput& input = m_case.input; + const StoryWorld* found = input.world != nullptr + ? &input.world() + : story_plan_world(std::string(input.plan_from), std::string(input.plan_to)); + if (found == nullptr){ + return std::format("No story plan world from {} to {}.", input.plan_from, input.plan_to); + } + const StoryWorld& world = *found; if (std::optional error = edge_mismatch(world)){ return *error; } @@ -297,42 +309,29 @@ class Test_ConnectedStoryWorld : public UnitTest{ ConnectedWorldCase m_case; }; -const StoryWorld& south_plan_world(){ - return *story_plan_world("TwinleafTown", "Route202"); -} -const StoryWorld& route_203_plan_world(){ - return *story_plan_world("Route203", "JubilifeCity"); -} -const StoryWorld& route_204_south_plan_world(){ - return *story_plan_world("JubilifeCity", "Route204South"); -} -const StoryWorld& route_204_north_plan_world(){ - return *story_plan_world("Route204North", "FloaromaTown"); -} - const ConnectedWorldCase CONNECTED_WORLD_CASES[] = { { - .input = {"Route 201, Sandgem and Route 202", story_maps}, + .input = {.name = "Route 201, Sandgem and Route 202", .world = story_maps}, .expectation = {.connections = 4}, }, { - .input = {"Twinleaf to Route 202", south_plan_world}, + .input = {.name = "Twinleaf to Route 202", .plan_from = "TwinleafTown", .plan_to = "Route202"}, .expectation = {.connections = 8}, }, { - .input = {"Route 203 into Jubilife", route_203_plan_world}, + .input = {.name = "Route 203 into Jubilife", .plan_from = "Route203", .plan_to = "JubilifeCity"}, .expectation = {.connections = 2}, }, { - .input = {"Jubilife up Route 204's south half", route_204_south_plan_world}, + .input = {.name = "Jubilife up Route 204's south half", .plan_from = "JubilifeCity", .plan_to = "Route204South"}, .expectation = {.connections = 2}, }, { - .input = {"Route 204's north half into Floaroma", route_204_north_plan_world}, + .input = {.name = "Route 204's north half into Floaroma", .plan_from = "Route204North", .plan_to = "FloaromaTown"}, .expectation = {.connections = 2}, }, { - .input = {"Jubilife alone", jubilife_maps}, + .input = {.name = "Jubilife alone", .world = jubilife_maps}, .expectation = {.connections = 0}, }, }; From 213ceb78aaee19d59cbe9d8a0359047bb966a799 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 00:25:48 -0800 Subject: [PATCH 8/8] Pokemon Navigation: stale-place cases carry their expectation (code health) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Pokemon_ConnectedMaps_Tests.cpp | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp index abcc99f641..ceafa9304d 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp @@ -208,8 +208,13 @@ struct StalePlaceInput{ std::string_view name; size_t map; }; +struct StalePlaceExpectation{ + // Whether reading the place throws InternalProgramError. + bool rejected = false; +}; struct StalePlaceCase{ StalePlaceInput input; + StalePlaceExpectation expectation; }; // A place whose map index is past the loaded maps (from another world, or @@ -223,21 +228,26 @@ class Test_ConnectedMaps_StalePlace : public UnitTest{ virtual UnitTestResult run(Logger&, CancellableScope&) const override{ const ConnectedMaps world(test_maps()); const MapPlace stale{m_scenario.input.map, {0, 0}}; + bool rejected = false; try{ - const std::optional place = world.neighbour(stale, Direction::NORTH); - return std::format("Map {} of {} was read: got {}, expected an InternalProgramError.", - stale.map, world.size(), place ? to_string(place->tile) : "nowhere"); + world.neighbour(stale, Direction::NORTH); }catch (const InternalProgramError&){ - return true; + rejected = true; + } + if (rejected != m_scenario.expectation.rejected){ + return std::format("Map {} of {} was {}, expected it {}.", stale.map, world.size(), + rejected ? "rejected" : "read", m_scenario.expectation.rejected ? "rejected" : "read"); } + return true; } private: StalePlaceCase m_scenario; }; const StalePlaceCase STALE_PLACE_CASES[] = { - {.input = {.name = "a map index one past the last is rejected", .map = 4}}, - {.input = {.name = "a map index far past the last is rejected", .map = 1000}}, + {.input = {.name = "the last map index is read", .map = 3}, .expectation = {.rejected = false}}, + {.input = {.name = "a map index one past the last is rejected", .map = 4}, .expectation = {.rejected = true}}, + {.input = {.name = "a map index far past the last is rejected", .map = 1000}, .expectation = {.rejected = true}}, };