Skip to content
Draft
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
15 changes: 15 additions & 0 deletions .github/workflows/cpp-ci-serial-programs-base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ jobs:
ref: 'master'
path: 'Arduino-Source/Packages'

- name: Install Python for pinned map preparation
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Prepare approved FRLG map resources
id: map-resources
uses: dayfine/PokemonSimulation/integrations/arduino-source/verify-map-packs@906956e94fc912ae9adb0e44845da780f3bc7e4c
with:
layout: resources
operation: prepare
consumer-directory: Arduino-Source

- name: Install Qt
uses: jurplel/install-qt-action@v4
with:
Expand Down Expand Up @@ -204,6 +217,8 @@ jobs:
# every file missed, compiling took 10-40% longer than before on Linux and macOS, and
# about twice as long on Windows.
CCACHE_DEPEND: 1
POKESIM_TOOLS_SOURCE: ${{ steps.map-resources.outputs.tools-source }}
POKESIM_SOURCE_CACHE: ${{ steps.map-resources.outputs.source-cache }}
run: |
cd Arduino-Source/SerialPrograms
mkdir bin
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@ Packages/*
# Local agent/linter environments.
.venv-code-health/
__pycache__/

# External PokemonSimulation tools/data cache; prepared maps live in ignored Packages.
.pokesim-map-source/
4 changes: 4 additions & 0 deletions SerialPrograms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ add_library(SerialProgramsLib STATIC ${CORE_LIBRARY_SOURCES} ${LIBRARY_SOURCES})
include(cmake/EmbedTextFiles.cmake)
pa_embed_text_files(SerialProgramsLib Source/Pokemon/Navigation/TestMaps
Pokemon/Navigation/Pokemon_NavigationTestMaps.h PokemonAutomation::Pokemon navigation_test_map_json)
include(cmake/PokesimMapResources.cmake)
pa_frlg_map_resources(FRLG_MAP_RESOURCES)
pa_embed_text_files(SerialProgramsLib "${FRLG_MAP_RESOURCES}"
VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.h PokemonAutomation::NintendoSwitch::PokemonFRLG overworld_map_json)


# Create binary target
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
/* Overworld Map
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "Common/Cpp/Exceptions.h"
#include "PokemonFRLG_OverworldMap.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonFRLG{


bool in_bounds(const MapData& map, const TilePos& point){
return map.tiles.contains(point);
}
TileKind tile_at(const MapData& map, const TilePos& point){
return in_bounds(map, point) ? map.tiles.at(point) : TileKind::WALL;
}
uint8_t elevation_at(const MapData& map, const TilePos& point){
if (!in_bounds(map, point)){
return ELEVATION_IGNORED;
}
return map.elevation ? map.elevation->at(point) : ELEVATION_GROUND;
}

bool is_wall(const MapData& map, const TilePos& point){
const TileKind tile = tile_at(map, point);
return tile == TileKind::WALL || tile == TileKind::DOOR || tile == TileKind::SIGN || is_ledge(map, point);
}
bool is_ledge(const MapData& map, const TilePos& point){
switch (tile_at(map, point)){
case TileKind::LEDGE_NORTH:
case TileKind::LEDGE_SOUTH:
case TileKind::LEDGE_WEST:
case TileKind::LEDGE_EAST:
return true;
default:
return false;
}
}
bool is_water(const MapData& map, const TilePos& point){
return tile_at(map, point) == TileKind::WATER;
}
bool is_tall_grass(const MapData& map, const TilePos& point){
return tile_at(map, point) == TileKind::TALL_GRASS;
}

namespace{

// The tile closed on its `side` edge.
TileKind closed_side(Direction side){
switch (side){
case Direction::NORTH:
return TileKind::CLOSED_NORTH;
case Direction::SOUTH:
return TileKind::CLOSED_SOUTH;
case Direction::EAST:
return TileKind::CLOSED_EAST;
case Direction::WEST:
return TileKind::CLOSED_WEST;
}
return TileKind::WALL;
}
Direction opposite(Direction direction){
switch (direction){
case Direction::NORTH:
return Direction::SOUTH;
case Direction::SOUTH:
return Direction::NORTH;
case Direction::EAST:
return Direction::WEST;
case Direction::WEST:
return Direction::EAST;
}
return direction;
}

// How the warp on this tile is entered, if there is one.
std::optional<WarpEntry> warp_entry(const MapData& map, const TilePos& point){
const MapWarp* warp = warp_at(map, point);
if (warp == nullptr){
return std::nullopt;
}
return warp->enter;
}

} // namespace

bool blocks_entering(const MapData& map, const TilePos& point, Direction direction){
// Moving south enters by the north edge.
return tile_at(map, point) == closed_side(opposite(direction));
}
bool blocks_leaving(const MapData& map, const TilePos& point, Direction direction){
return tile_at(map, point) == closed_side(direction);
}

bool warps_on_step(const MapData& map, const TilePos& point){
const std::optional<WarpEntry> entry = warp_entry(map, point);
return entry == WarpEntry::STEP_ON || entry == WarpEntry::CAVE_ENTRANCE;
}
bool is_door(const MapData& map, const TilePos& point){
return tile_at(map, point) == TileKind::DOOR;
}
std::optional<Direction> exit_mat(const MapData& map, const TilePos& point){
const std::optional<WarpEntry> entry = warp_entry(map, point);
if (!entry){
return std::nullopt;
}
switch (*entry){
case WarpEntry::WALK_NORTH:
return Direction::NORTH;
case WarpEntry::WALK_SOUTH:
return Direction::SOUTH;
case WarpEntry::WALK_WEST:
return Direction::WEST;
case WarpEntry::WALK_EAST:
return Direction::EAST;
default:
return std::nullopt;
}
}
bool walks_out_on_arrival(const MapData& map, const TilePos& point){
return is_door(map, point) || warp_entry(map, point) == WarpEntry::CAVE_ENTRANCE;
}

void hide_objects(MapData& map, const std::set<std::string>& flags){

Check warning on line 128 in SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMap.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: hide_objects: output parameter 'MapData& map' comes before an input; put inputs (by value, const&) first and non-const references last (Google "Function Parameter Ordering")
std::vector<MapObject> shown;
for (const MapObject& object : map.objects){
if (!object.hide_flag || flags.count(*object.hide_flag) == 0){
shown.push_back(object);
}
}
map.objects = std::move(shown);
}

namespace{

// The tiles `object` may stand on: its wander box, or its own tile.
Pokemon::TileBox standing_box(const MapObject& object){
return object.wander.value_or(
Pokemon::TileBox{.x0 = object.x, .y0 = object.y, .x1 = object.x, .y1 = object.y}
);
}

// Whether `trainer`, standing on `from`, sees `tile` any way it looks.
bool sees_from(const MapData& map, const Pokemon::MapTrainer& trainer, const TilePos& from, const TilePos& tile){
for (Direction look : trainer.looks){
TilePos seen = from;
for (int c = 0; c < trainer.sight && !is_wall(map, seen.next(look)); c++){
seen = seen.next(look);
if (seen == tile){
return true;
}
}
}
return false;
}

} // namespace

const MapObject* object_at(const MapData& map, const TilePos& point){
for (const MapObject& object : map.objects){
const Pokemon::TileBox box = standing_box(object);
if (box.x0 <= point.x && point.x <= box.x1 && box.y0 <= point.y && point.y <= box.y1){
return &object;
}
}
return nullptr;
}

bool trainer_may_see(const MapData& map, const MapObject& object, const TilePos& tile){
if (!object.trainer){
return false;
}
const Pokemon::TileBox box = standing_box(object);
for (int y = box.y0; y <= box.y1; y++){
for (int x = box.x0; x <= box.x1; x++){
if (sees_from(map, *object.trainer, TilePos{x, y}, tile)){
return true;
}
}
}
return false;
}



StepKind classify_step(const MapData& map, const PlayerState& state, Direction direction){
return classify_step_onto(map, state, direction, map, state.position.next(direction));
}

StepKind classify_step_onto(

Check warning on line 194 in SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMap.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::classify_step_onto( const MapData & from , const PlayerState & state , Direction direction , const MapData & to , const TilePos & destination): function_lines=31 (advisory above 25, limit 50)
const MapData& from, const PlayerState& state, Direction direction,
const MapData& to, const TilePos& destination
){
if (is_wall(to, destination)
|| blocks_leaving(from, state.position, direction)
|| blocks_entering(to, destination, direction)
){
return StepKind::BLOCKED;
}

const uint8_t level = elevation_at(to, destination);
const bool mismatch = state.elevation != ELEVATION_ANY
&& level != ELEVATION_ANY && level != ELEVATION_IGNORED
&& level != state.elevation;
if (mismatch){
// CanStopSurfing() and IsPlayerFacingSurfableFishableWater().
if (state.surfing && level == ELEVATION_GROUND && object_at(to, destination) == nullptr){
return StepKind::STOP_SURFING;
}
if (!state.surfing && state.elevation == ELEVATION_GROUND && is_water(to, destination)){
return StepKind::START_SURF;
}
return StepKind::BLOCKED;
}

if (object_at(to, destination) != nullptr){
return StepKind::BLOCKED;
}
return StepKind::WALK;
}

PlayerState after_step(const MapData& map, const PlayerState& state, Direction direction, StepKind kind){
return after_step_onto(map, state, map, state.position.next(direction), kind);
}

PlayerState after_step_onto(
const MapData& from, const PlayerState& state,
const MapData& to, const TilePos& destination, StepKind kind
){
PlayerState next = state;
next.position = destination;
const uint8_t level = elevation_at(to, destination);
if (level != ELEVATION_IGNORED && elevation_at(from, state.position) != ELEVATION_IGNORED){
next.elevation = level;
}
if (kind == StepKind::START_SURF){
next.surfing = true;
}else if (kind == StepKind::STOP_SURFING){
next.surfing = false;
}
return next;
}



} // namespace PokemonFRLG
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* Overworld Map
*
* From: https://github.com/PokemonAutomation/
*
* FRLG's walking rules on a map (a Pokemon::MapData): per tile collision,
* elevation and the behaviors that matter for walking (water, one-way
* edges), plus its warps and the objects standing on it. The map data is
* generated from the pret/pokefirered decompilation, and classify_step()
* follows the game's own collision checks (GetCollisionAtCoords() in
* src/event_object_movement.c), so a route planned on these maps is one the
* game allows.
*
*/

#ifndef PokemonAutomation_PokemonFRLG_OverworldMap_H
#define PokemonAutomation_PokemonFRLG_OverworldMap_H

#include <cstdint>
#include <optional>
#include <set>
#include <string>
#include "Pokemon/Navigation/Pokemon_Grid.h"
#include "Pokemon/Navigation/Pokemon_MapData.h"
#include "PokemonFRLG_OverworldObjects.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonFRLG{



// What decides where the player's next step can go.
struct PlayerState{
TilePos position;
uint8_t elevation = ELEVATION_GROUND;
bool surfing = false;
};

// Classification of movement result when taking a step.
enum class StepKind{
BLOCKED,
WALK, // moves one tile (on land, or on water while surfing)
STOP_SURFING, // moves from water onto land and stops surfing
START_SURF, // blocked, but facing water the player can Surf onto
};

// What pressing `direction` once does to the player in `state`.
StepKind classify_step(const MapData& map, const PlayerState& state, Direction direction);

// The same for a step that crosses into another map: from `state` on `from`
// onto `destination` on `to`.
StepKind classify_step_onto(
const MapData& from, const PlayerState& state, Direction direction,
const MapData& to, const TilePos& destination
);

// The player after a step of kind WALK, STOP_SURFING or START_SURF (the
// Surf action moves the player onto the water).
PlayerState after_step(const MapData& map, const PlayerState& state, Direction direction, StepKind kind);
// The player state resulting from stepping across map boundaries.
PlayerState after_step_onto(
const MapData& from, const PlayerState& state,
const MapData& to, const TilePos& destination, StepKind kind
);



} // namespace PokemonFRLG
} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Loading
Loading