diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 9b3c367c41..75af9451cc 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -33,6 +33,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" @@ -82,6 +83,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..1bd697a03e --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp @@ -0,0 +1,153 @@ +/* Connected Maps + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Pokemon_ConnectedMaps.h" + +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 + + +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){ + 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{ + 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; + const TilePos point = edge_entry(from.tile, side, connection, to); + 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..5fef2d16a6 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h @@ -0,0 +1,99 @@ +/* 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; + // Map, then tile: an order for sets and maps of places. + auto 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) + {} + // 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(); + } + // 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; + + // 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 + + +// 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{ + 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..ceafa9304d --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp @@ -0,0 +1,270 @@ +/* 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/Exceptions.h" +#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 std::format("{} {}", world.map(place->map).name, to_string(place->tile)); +} + +// 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; + 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; + PlaceExpectation 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 = {.place = NamedPlace{"Town", {2, 1}}}, + }, + { + .input = {.name = "north onto the route, shifted by its offset", .from = {"Town", {0, 0}}, .direction = Direction::NORTH}, + .expectation = {.place = NamedPlace{"Route", {1, 2}}}, + }, + { + .input = {.name = "south off the route into the town", .from = {"Route", {1, 2}}, .direction = Direction::SOUTH}, + .expectation = {.place = NamedPlace{"Town", {0, 0}}}, + }, + { + .input = {.name = "east into the lake", .from = {"Town", {3, 2}}, .direction = Direction::EAST}, + .expectation = {.place = NamedPlace{"Lake", {0, 2}}}, + }, + { + .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}, + }, + { + .input = {.name = "past the joined map's span", .from = {"Route", {0, 2}}, .direction = Direction::SOUTH}, + .expectation = {.place = std::nullopt}, + }, + { + .input = {.name = "a map off the world grid joins nothing", .from = {"House", {1, 0}}, .direction = Direction::NORTH}, + .expectation = {.place = std::nullopt}, + }, +}; + + +struct WarpInput{ + std::string_view name; + MapWarp warp; +}; +struct WarpCase{ + WarpInput input; + PlaceExpectation 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 = {.place = NamedPlace{"House", {0, 1}}}, + }, + { + .input = {.name = "a warp to a map that isn't loaded", .warp = {.to = WarpDestination{"Cave", "door"}}}, + .expectation = {.place = std::nullopt}, + }, + { + .input = {.name = "a warp to a point the map lacks", .warp = {.to = WarpDestination{"House", "stairs"}}}, + .expectation = {.place = std::nullopt}, + }, + { + .input = {.name = "a warp that leads nowhere", .warp = {}}, + .expectation = {.place = std::nullopt}, + }, +}; + + +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 +// 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}}; + bool rejected = false; + try{ + world.neighbour(stale, Direction::NORTH); + }catch (const InternalProgramError&){ + 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 = "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}}, +}; + + +} // 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); + } + for (const StalePlaceCase& scenario : STALE_PLACE_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/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp index d6f7a67b80..a3cb3e6495 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(); } @@ -271,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 cf46b6f0c5..b7c57f01db 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,7 @@ class StoryWorld{ // The map that holds a world tile, or nothing between maps. std::optional to_local(const Pokemon::TilePos& world) const; + 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); @@ -102,6 +108,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() @@ -109,6 +116,13 @@ 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); +// 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. // 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 99cb510515..37e501f309 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp @@ -3,18 +3,23 @@ * 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. * */ #include #include #include +#include #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 +224,127 @@ const StoryWorldCase STORY_WORLD_CASES[] = { }, }; +namespace{ + +// 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 = connected_maps(world); + for (size_t index = 0; index < connected.size(); index++){ + 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)); + } + } + } + return std::nullopt; +} + +struct ConnectedWorldInput{ + 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 = 0; +}; +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 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; + } + 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(); + } + if (connections != m_case.expectation.connections){ + return std::format("{} connections, expected {}.", connections, m_case.expectation.connections); + } + return true; + } + +private: + ConnectedWorldCase m_case; +}; + +const ConnectedWorldCase CONNECTED_WORLD_CASES[] = { + { + .input = {.name = "Route 201, Sandgem and Route 202", .world = story_maps}, + .expectation = {.connections = 4}, + }, + { + .input = {.name = "Twinleaf to Route 202", .plan_from = "TwinleafTown", .plan_to = "Route202"}, + .expectation = {.connections = 8}, + }, + { + .input = {.name = "Route 203 into Jubilife", .plan_from = "Route203", .plan_to = "JubilifeCity"}, + .expectation = {.connections = 2}, + }, + { + .input = {.name = "Jubilife up Route 204's south half", .plan_from = "JubilifeCity", .plan_to = "Route204South"}, + .expectation = {.connections = 2}, + }, + { + .input = {.name = "Route 204's north half into Floaroma", .plan_from = "Route204North", .plan_to = "FloaromaTown"}, + .expectation = {.connections = 2}, + }, + { + .input = {.name = "Jubilife alone", .world = 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); + } } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 2b78fa0d2f..3c73bdeefa 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1465,6 +1465,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_Direction.cpp Source/Pokemon/Navigation/Pokemon_Direction.h Source/Pokemon/Navigation/Pokemon_Grid.cpp