Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h"
#include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.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"
Expand All @@ -60,7 +61,7 @@



UnitTestDatabase make_UNIT_TESTS_ALL(){

Check warning on line 64 in SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::ComputerPrograms::make_UNIT_TESTS_ALL(): function_lines=45 (advisory above 25, limit 50)
UnitTestDatabase ret;

add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal());
Expand All @@ -84,6 +85,7 @@
Pokemon::add_tests_LegWalker(ret);
Pokemon::add_tests_WallRecovery(ret);
Pokemon::add_tests_MapData(ret);
Pokemon::add_tests_ConnectedMaps(ret);
Pokemon::add_tests_MoveMechanics(ret);
Pokemon::add_tests_RuntimeMap(ret);
Pokemon::add_tests_RouteLog(ret);
Expand Down
153 changes: 153 additions & 0 deletions SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/* Connected Maps
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <algorithm>
#include <format>
#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<TileKind>& 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<size_t> 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<MapPlace> ConnectedMaps::warp_arrival(const MapWarp& warp) const{
if (!warp.to){
return std::nullopt;
}
const std::optional<size_t> 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<MapPlace> 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<size_t> destination = find(connection.to_map);
if (!destination){
continue;
}
const Grid<TileKind>& 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<MapPlace> 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<MapConnection> 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<MapData>& 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<MapConnection> 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
99 changes: 99 additions & 0 deletions SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h
Original file line number Diff line number Diff line change
@@ -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 <cstddef>
#include <optional>
#include <string_view>
#include <vector>
#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{

Check warning on line 42 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::ConnectedMaps: public_methods=6 (advisory above 5, limit 7)
public:
explicit ConnectedMaps(const std::vector<MapData>& maps)
: m_maps(&maps)
{}
// A temporary list would be gone by the first call.
explicit ConnectedMaps(std::vector<MapData>&& 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<size_t> 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<MapPlace> 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<MapPlace> 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<MapPlace> neighbour(const MapPlace& from, Direction direction) const;

private:
const std::vector<MapData>* 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<MapData>& maps);



} // namespace Pokemon
} // namespace PokemonAutomation


// A hash of MapPlace, for planners that keep places in unordered sets.
template <>
struct std::hash<PokemonAutomation::Pokemon::MapPlace>{
size_t operator()(const PokemonAutomation::Pokemon::MapPlace& place) const{
return std::hash<PokemonAutomation::Pokemon::TilePos>()(place.tile) * 31 + place.map;
}
};

#endif
Loading
Loading