diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp index 67361691c5..a159b93efe 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp @@ -11,6 +11,7 @@ #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/Navigation/Pokemon_ConnectedMaps.h" #include "Pokemon/Navigation/Pokemon_LegPlanner.h" #include "PokemonFRLG_OverworldMaps.h" #include "PokemonFRLG_OverworldObjects.h" @@ -50,13 +51,12 @@ int route_cost(const RouteCosts& costs, RouteCost kind){ namespace{ +using Pokemon::ConnectedMaps; +using Pokemon::MapPlace; + int find_map(const std::vector& maps, const std::string& name){ - for (size_t c = 0; c < maps.size(); c++){ - if (maps[c].name == name){ - return (int)c; - } - } - return -1; + const std::optional index = ConnectedMaps(maps).find(name); + return index ? (int)*index : -1; } // The player on one of the loaded maps. @@ -92,19 +92,16 @@ struct Transition{ // The player taking `warp`, or nothing if it leads off the loaded maps. std::optional take_warp(const std::vector& maps, const MapWarp* warp){ - if (warp == nullptr || !warp->to){ + if (warp == nullptr){ return std::nullopt; } - const int destination = find_map(maps, warp->to->map); - if (destination < 0){ + const std::optional arrival = ConnectedMaps(maps).warp_arrival(*warp); + if (!arrival){ return std::nullopt; } + const int destination = (int)arrival->map; const MapData& map = maps[destination]; - const auto arrival = map.points.find(warp->to->point); - if (arrival == map.points.end()){ - return std::nullopt; - } - TilePos position = arrival->second; + TilePos position = arrival->tile; Transition transition; if (walks_out_on_arrival(map, position)){ position = position.next(Direction::SOUTH); @@ -119,43 +116,21 @@ std::optional take_warp(const std::vector& maps, const MapW // Walking off the map's edge onto the map connected there, if it is loaded. std::optional cross_edge(const std::vector& maps, const Located& player, Direction direction){ const MapData& map = maps[player.map]; - for (const MapConnection& connection : map.connections){ - if (connection.side != direction){ - continue; - } - const int destination = find_map(maps, connection.to_map); - if (destination < 0){ - return std::nullopt; - } - const MapData& to = maps[destination]; - const TilePos here = player.state.position; - TilePos point; - switch (direction){ - case Direction::NORTH: - point = {here.x - connection.offset, to.tiles.height - 1}; - break; - case Direction::SOUTH: - point = {here.x - connection.offset, 0}; - break; - case Direction::WEST: - point = {to.tiles.width - 1, here.y - connection.offset}; - break; - case Direction::EAST: - point = {0, here.y - connection.offset}; - break; - } - const StepKind kind = classify_step_onto(map, player.state, direction, to, point); - if (kind == StepKind::BLOCKED){ - return std::nullopt; - } - Transition transition; - transition.next.map = destination; - transition.next.state = after_step_onto(map, player.state, to, point, kind); - transition.action = kind == StepKind::START_SURF ? RouteAction::SURF : RouteAction::WALK; - transition.dismounted = kind == StepKind::STOP_SURFING; - return transition; + const std::optional across = ConnectedMaps(maps).across_edge(MapPlace{(size_t)player.map, player.state.position}, direction); + if (!across){ + return std::nullopt; } - return std::nullopt; + const MapData& to = maps[across->map]; + const StepKind kind = classify_step_onto(map, player.state, direction, to, across->tile); + if (kind == StepKind::BLOCKED){ + return std::nullopt; + } + Transition transition; + transition.next.map = (int)across->map; + transition.next.state = after_step_onto(map, player.state, to, across->tile, kind); + transition.action = kind == StepKind::START_SURF ? RouteAction::SURF : RouteAction::WALK; + transition.dismounted = kind == StepKind::STOP_SURFING; + return transition; } // Pressing `direction` once (field_control_avatar.c): an exit mat or a door diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp index 880e331e4b..001c266311 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp @@ -9,6 +9,7 @@ #include #include "Common/Cpp/Exceptions.h" #include "CommonFramework/Exceptions/OperationFailedException.h" +#include "Pokemon/Navigation/Pokemon_ConnectedMaps.h" #include "Pokemon/Navigation/Pokemon_LegWalker.h" #include "PokemonFRLG_OverworldLateBattle.h" #include "PokemonFRLG_OverworldReanchor.h" @@ -102,12 +103,8 @@ constexpr int BLACK_BELOW_ROWS = 5; constexpr int SMALL_ROOM_ROWS = 20; int find_map(const std::vector& maps, const std::string& name){ - for (size_t c = 0; c < maps.size(); c++){ - if (maps[c].name == name){ - return (int)c; - } - } - return -1; + const std::optional index = Pokemon::ConnectedMaps(maps).find(name); + return index ? (int)*index : -1; } // The way a ledge on `tile` hops the player, if it is one. @@ -250,19 +247,13 @@ std::optional OverworldSim::take_warp(int map_index, const T if (m_lift_floor && m_script.lift_floors.contains(m_maps[map_index].name)){ return lift_warp(map_index); } - if (!warp->to){ - return std::nullopt; - } - const int destination = find_map(m_maps, warp->to->map); - if (destination < 0){ + const std::optional arrival = Pokemon::ConnectedMaps(m_maps).warp_arrival(*warp); + if (!arrival){ return std::nullopt; } + const int destination = (int)arrival->map; const MapData& to = m_maps[destination]; - const auto arrival = to.points.find(warp->to->point); - if (arrival == to.points.end()){ - return std::nullopt; - } - TilePos position = arrival->second; + TilePos position = arrival->tile; const bool walked_out = walks_out_on_arrival(to, position); if (walked_out){ position = position.next(Direction::SOUTH); @@ -271,27 +262,16 @@ std::optional OverworldSim::take_warp(int map_index, const T } std::optional OverworldSim::cross_edge(Direction direction) const{ - for (const MapConnection& connection : map().connections){ - const int destination = connection.side == direction ? find_map(m_maps, connection.to_map) : -1; - if (destination < 0){ - continue; - } - const MapData& to = m_maps[destination]; - const TilePos here = m_player.position; - TilePos point{here.x - connection.offset, here.y - connection.offset}; - switch (direction){ - case Direction::NORTH: point.y = to.tiles.height - 1; break; - case Direction::SOUTH: point.y = 0; break; - case Direction::WEST: point.x = to.tiles.width - 1; break; - case Direction::EAST: point.x = 0; break; - } - const StepKind kind = classify_step_onto(map(), m_player, direction, to, point); - if (kind != StepKind::WALK){ - return std::nullopt; - } - return Step{destination, after_step_onto(map(), m_player, to, point, kind), false, false, false}; + const std::optional across = Pokemon::ConnectedMaps(m_maps).across_edge({(size_t)m_map, m_player.position}, direction); + if (!across){ + return std::nullopt; } - return std::nullopt; + const MapData& to = m_maps[across->map]; + const StepKind kind = classify_step_onto(map(), m_player, direction, to, across->tile); + if (kind != StepKind::WALK){ + return std::nullopt; + } + return Step{(int)across->map, after_step_onto(map(), m_player, to, across->tile, kind), false, false, false}; } // Pressing `direction` once, as the planner's press() reads the game, and