Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapSet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* Map Set
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <algorithm>
#include "Common/Cpp/Exceptions.h"
#include "Pokemon_MapSet.h"

namespace PokemonAutomation{
namespace Pokemon{


void MapSet::add(std::string name, const TileGrid& grid, const TilePos& offset){

Check warning on line 15 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapSet.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::MapSet::add( std :: string name , const TileGrid & grid , const TilePos & offset): function_lines=43 (advisory above 25, limit 50)

Check failure on line 15 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapSet.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

PokemonAutomation::Pokemon::MapSet::add( std :: string name , const TileGrid & grid , const TilePos & offset): nesting=5, limit=4, allowed 4
for (const Placed& placed : m_maps){
if (placed.name == name){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Map placed twice: " + name);
}
// Map images show a border of trees past their edges, so two maps
// may share blocked tiles, but nothing else.
for (int y = 0; y < grid.height(); y++){
for (int x = 0; x < grid.width(); x++){
const TilePos other{offset.x + x - placed.offset.x, offset.y + y - placed.offset.y};
if (!placed.grid.in_bounds(other)){
continue;
}
if (grid.at({x, y}) != TileType::BLOCKED || placed.grid.at(other) != TileType::BLOCKED){
throw InternalProgramError(
nullptr, PA_CURRENT_FUNCTION,
"Map " + name + " overlaps " + placed.name + " at its tile " + to_string(TilePos{x, y}) + "."
);
}
}
}
}
m_maps.push_back(Placed{std::move(name), grid, offset});

// Rebuild the joined grid.
TilePos low = m_maps[0].offset;
TilePos high = low;
for (const Placed& placed : m_maps){
low.x = std::min(low.x, placed.offset.x);
low.y = std::min(low.y, placed.offset.y);
high.x = std::max(high.x, placed.offset.x + placed.grid.width());
high.y = std::max(high.y, placed.offset.y + placed.grid.height());
}
m_origin = low;
std::vector<std::string> rows(high.y - low.y, std::string(high.x - low.x, tile_char(TileType::UNKNOWN)));
for (const Placed& placed : m_maps){
const std::vector<std::string> map_rows = placed.grid.rows();
for (size_t y = 0; y < map_rows.size(); y++){
rows[placed.offset.y - low.y + y].replace(placed.offset.x - low.x, map_rows[y].size(), map_rows[y]);
}
}
m_world.emplace(rows);
}

const TileGrid& MapSet::world() const{
if (!m_world){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "No maps placed.");
}
return *m_world;
}

TilePos MapSet::to_world(const std::string& map, const TilePos& local) const{
for (const Placed& placed : m_maps){
if (placed.name == map){
return {
placed.offset.x - m_origin.x + local.x,
placed.offset.y - m_origin.y + local.y,
};
}
}
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown map: " + map);
}

std::optional<MapTile> MapSet::to_local(const TilePos& world) const{
for (const Placed& placed : m_maps){
const TilePos local{
world.x + m_origin.x - placed.offset.x,
world.y + m_origin.y - placed.offset.y,
};
if (placed.grid.in_bounds(local)){
return MapTile{placed.name, local};
}
}
return std::nullopt;
}

std::string MapSet::describe(const TilePos& world) const{
const std::optional<MapTile> local = to_local(world);
if (!local){
return "world " + to_string(world);
}
return local->map + " " + to_string(local->pos);
}



}
}
63 changes: 63 additions & 0 deletions SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapSet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Map Set
*
* From: https://github.com/PokemonAutomation/
*
* Several tile grids placed side by side on one world grid, so one plan
* can cross from a route into a town.
*/

#ifndef PokemonAutomation_Pokemon_MapSet_H
#define PokemonAutomation_Pokemon_MapSet_H

#include <optional>
#include <string>
#include <vector>
#include "Pokemon_TileGrid.h"

namespace PokemonAutomation{
namespace Pokemon{


struct MapTile{
std::string map;
TilePos pos;
};


class MapSet{
public:
// Places `grid` with its tile (0,0) at `offset`. Offsets are relative to
// each other and may be negative. Maps may overlap only where both are
// blocked; the map placed first holds those tiles. Throws
// InternalProgramError if the name is taken or the maps conflict.
void add(std::string name, const TileGrid& grid, const TilePos& offset);

// The maps joined on one grid. Tiles no map covers are UNKNOWN. World
// tile (0,0) is the top-left corner of all the maps together.
const TileGrid& world() const;

// Throws InternalProgramError for an unknown map name.
TilePos to_world(const std::string& map, const TilePos& local) const;

// The map that holds a world tile, or nothing between maps.
std::optional<MapTile> to_local(const TilePos& world) const;

// "Route 201 (45,3)", or the world tile if no map holds it.
std::string describe(const TilePos& world) const;

private:
struct Placed{
std::string name;
TileGrid grid;
TilePos offset;
};
std::vector<Placed> m_maps;
TilePos m_origin; // offset of world tile (0,0)
std::optional<TileGrid> m_world;
};



}
}
#endif
Loading
Loading