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
14 changes: 14 additions & 0 deletions .github/workflows/bdsp-map-resources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: BDSP map resources
on:
pull_request:
workflow_dispatch:
jobs:
integrity:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- name: Prepare and check the approved BDSP resource pin
uses: dayfine/PokemonSimulation/integrations/arduino-source/verify-map-packs@4de7742a764d27293b4525d6ad2a6fc8a95f3e3a
with:
layout: resources
operation: prepare
4 changes: 2 additions & 2 deletions .github/workflows/runtime-map-reader.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
- uses: actions/checkout@v7
with:
repository: dayfine/PokemonSimulation
ref: 906956e94fc912ae9adb0e44845da780f3bc7e4c
ref: 4de7742a764d27293b4525d6ad2a6fc8a95f3e3a
path: publisher
- uses: actions/setup-python@v6
with:
python-version: '3.12'
- run: python devtools/runtime-map-tests/prepare_parity.py --publisher publisher --publisher-revision 906956e94fc912ae9adb0e44845da780f3bc7e4c --out runtime-map-pairs.json
- run: python devtools/runtime-map-tests/prepare_parity.py --publisher publisher --publisher-revision 4de7742a764d27293b4525d6ad2a6fc8a95f3e3a --out runtime-map-pairs.json
- run: cmake -S devtools/runtime-map-tests -B build-runtime-maps -DCMAKE_BUILD_TYPE=Debug
- run: cmake --build build-runtime-maps --config Debug -j4
- run: ctest --test-dir build-runtime-maps -C Debug --output-on-failure
Expand Down
13 changes: 13 additions & 0 deletions SerialPrograms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ 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)
# Prepared data is validated by the shared publisher tooling. No source-map
# fallback or network download is performed during configuration.
set(POKESIM_TOOLS_SOURCE "$ENV{POKESIM_TOOLS_SOURCE}" CACHE PATH "Pinned PokemonSimulation tools checkout")
set(POKESIM_SOURCE_CACHE "$ENV{POKESIM_SOURCE_CACHE}" CACHE PATH "Approved publishing Git cache")
if(NOT POKESIM_TOOLS_SOURCE OR NOT POKESIM_SOURCE_CACHE)
message(FATAL_ERROR "Prepare devtools/maps/map-resources.lock.json and set POKESIM_TOOLS_SOURCE/POKESIM_SOURCE_CACHE; see devtools/maps/RESOURCE_MAPS.md.")
endif()
include("${POKESIM_TOOLS_SOURCE}/integrations/arduino-source/resource-maps.cmake")
get_filename_component(POKESIM_CONSUMER "${CMAKE_CURRENT_SOURCE_DIR}/.." ABSOLUTE)
pa_pokesim_map_directory(BDSP_MAP_DIRECTORY bdsp "${POKESIM_CONSUMER}" "${POKESIM_SOURCE_CACHE}")
file(RELATIVE_PATH BDSP_MAP_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}" "${BDSP_MAP_DIRECTORY}")
pa_embed_text_files(SerialProgramsLib "${BDSP_MAP_RESOURCES}"
PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h PokemonAutomation::NintendoSwitch::PokemonBDSP bdsp_map_json)


# Create binary target
Expand Down
6 changes: 6 additions & 0 deletions SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "Inference/PokemonBDSP_DialogDetector.h"
#include "Inference/PokemonBDSP_MenuDetector.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h"
#include "Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors_Tests.h"
#include "Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.h"
#include "PokemonBDSP_Tests.h"

namespace PokemonAutomation{
Expand All @@ -18,6 +21,9 @@ namespace PokemonBDSP{
void add_tests(UnitTestDatabase& database){
add_tests_DialogDetector(database);
add_tests_MenuDetector(database);
add_tests_AutoStory_Maps(database);
add_tests_AutoStory_Indoors(database);
add_tests_AutoStory_WildBattlePolicy(database);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* BDSP AutoStory - Coord events
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <map>
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/Json/JsonArray.h"
#include "Common/Cpp/Json/JsonObject.h"
#include "Common/Cpp/Json/JsonValue.h"
#include "PokemonBDSP_AutoStory_Maps.h"
#include "PokemonBDSP_AutoStory_CoordEvents.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonBDSP{

using Pokemon::TilePos;


namespace{

int read_int(const JsonObject& object, const std::string& key, const std::string& where){
const JsonValue* value = object.get_value(key);
if (value == nullptr || !value->is_integer()){
throw ParseException(where + key + " is missing or not an integer.");

Check warning on line 28 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}
return (int)value->to_integer_default();
}

std::string read_string(const JsonObject& object, const std::string& key, const std::string& where){
const JsonValue* value = object.get_value(key);
const std::string* text = value == nullptr ? nullptr : value->to_string();
if (text == nullptr){
throw ParseException(where + key + " is missing or not a string.");

Check warning on line 37 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}
return *text;
}

CoordEvent read_event(const JsonObject& object, const std::string& where){

Check warning on line 42 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::read_event( const JsonObject & object , const std :: string & where): function_lines=32 (advisory above 25, limit 50)
CoordEvent event{
.name = read_string(object, "name", where),
.tiles = {},
.var = read_string(object, "var", where),
.value = read_int(object, "value", where),
.source = read_string(object, "source", where),
};
int x = 0;
int y = 0;
if (const JsonArray* coord = object.get_array("coord")){
if (coord->size() < 2 || !(*coord)[0].is_integer() || !(*coord)[1].is_integer()){
throw ParseException(where + "coord must have at least 2 integer elements.");

Check warning on line 54 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}
x = (int)(*coord)[0].to_integer_default();
y = (int)(*coord)[1].to_integer_default();
}else{
x = read_int(object, "x", where);
y = read_int(object, "y", where);
}
const int width = read_int(object, "width", where);
const int length = read_int(object, "length", where);
if (width < 1 || length < 1){
throw ParseException(where + "width and length must be at least 1.");

Check warning on line 65 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}
for (int row = y; row < y + length; row++){
for (int col = x; col < x + width; col++){
event.tiles.push_back({col, row});
}
}
return event;
}

std::vector<CoordEvent> read_events(const std::string& name, const std::string& json){
std::vector<CoordEvent> events;
const JsonValue root = parse_json(json);
const JsonObject* object = root.to_object();
const JsonValue* list = object == nullptr ? nullptr : object->get_value("coord_events");
if (list == nullptr){
return events;
}
const JsonArray* array = list->to_array();
if (array == nullptr){
throw ParseException(name + ".json: coord_events is not an array.");

Check warning on line 85 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}
for (size_t c = 0; c < array->size(); c++){
const std::string where = std::format("{}.json: coord_events[{}].", name, c);
const JsonObject* item = (*array)[c].to_object();
if (item == nullptr){
throw ParseException(where + " is not an object.");

Check warning on line 91 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}
events.push_back(read_event(*item, where));
}
return events;
}

} // namespace


const std::vector<CoordEvent>& story_coord_events(std::string_view map){
static const std::map<std::string, std::vector<CoordEvent>, std::less<>> by_map = []{
std::map<std::string, std::vector<CoordEvent>, std::less<>> out;
for (const auto& [name, json] : bdsp_map_json()){
out.emplace(name, read_events(name, json));
}
return out;
}();
if (auto iter = by_map.find(map); iter != by_map.end()){
return iter->second;
}
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("No BDSP story map named \"{}\".", map));
}



} // namespace PokemonBDSP
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* BDSP AutoStory - Coord events
*
* From: https://github.com/PokemonAutomation/
*
* The scene triggers of the story maps: stepping onto one of an event's
* tiles while its story variable holds `value` starts its script. They are
* the decomp's coord events (zone_event), read from a map's
* "coord_events", which the shared map schema leaves to the game:
*
* "coord_events": [{"name": "catch_tutorial", "x": 21, "y": 25, "width": 1, "length": 5,
* "var": "VAR_ROUTE_202_STATE", "value": 0, "source": "..."}, ...]
*
* A trigger seen on the console that the decomp does not have names the run
* in `source`.
*
*/

#ifndef PokemonAutomation_PokemonBDSP_AutoStory_CoordEvents_H
#define PokemonAutomation_PokemonBDSP_AutoStory_CoordEvents_H

#include <string>
#include <string_view>
#include <vector>
#include "Pokemon/Navigation/Pokemon_Grid.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonBDSP{


// Coordinate-triggered event parsed from BDSP map data.
struct CoordEvent{
std::string name;
// The map's own tiles: `width` columns from x, `length` rows from y.
std::vector<Pokemon::TilePos> tiles;
std::string var;
int value = 0;
std::string source;
};

// The coord events of the map `map` (a JSON map's name), none if it lists
// none. Throws InternalProgramError for an unknown map, and ParseException
// for an event that does not parse.
const std::vector<CoordEvent>& story_coord_events(std::string_view map);



} // namespace PokemonBDSP
} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* BDSP AutoStory - Maps
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <map>
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/Json/JsonValue.h"
#include "PokemonBDSP_AutoStory_Maps.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonBDSP{

using Pokemon::MapData;
using Pokemon::TilePos;


namespace{

const std::map<std::string, MapData, std::less<>>& story_maps_by_name(){
static const std::map<std::string, MapData, std::less<>> maps = []{
std::map<std::string, MapData, std::less<>> parsed;
for (const auto& [name, json] : bdsp_map_json()){
MapData map = Pokemon::parse_map(parse_json(json), name + ".json");

Check warning on line 27 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
if (map.name != name){
throw ParseException(std::format("{}.json: name is \"{}\".", name, map.name));
}
parsed.emplace(name, std::move(map));
}
return parsed;
}();
return maps;
}

} // namespace


const MapData& story_map(std::string_view name){
const auto& maps = story_maps_by_name();
if (auto iter = maps.find(name); iter != maps.end()){
return iter->second;
}
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("No BDSP story map named \"{}\".", name));
}

TilePos map_point(std::string_view map, std::string_view point){
const MapData& data = story_map(map);
if (auto iter = data.points.find(std::string(point)); iter != data.points.end()){
return iter->second;
}
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("{} has no point \"{}\".", map, point));
}

std::vector<TilePos> map_region(std::string_view map, std::string_view region){
const MapData& data = story_map(map);
std::vector<TilePos> tiles;
while (true){
const auto iter = data.points.find(std::format("{}.{}", region, tiles.size() + 1));
if (iter == data.points.end()){
break;
}
tiles.push_back(iter->second);
}
if (tiles.empty()){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("{} has no region \"{}\".", map, region));
}
return tiles;
}



} // namespace PokemonBDSP
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* BDSP AutoStory - Maps
*
* From: https://github.com/PokemonAutomation/
*
* The maps the story walks, as JSON files in the shared map schema
* (Pokemon_MapData.h): PokemonBDSP/Programs/AutoStory/Maps, written by
* devtools/maps/bdsp_maps.py from the Diamond decompilation and built into
* the program by CMake. Each is named as its file ("Route202").
*
* A map's named tiles are its points. A region, a list of tiles, is the
* points "<region>.1", "<region>.2", ... in order: the schema has
* single-tile points only.
*
* TODO: move the maps to PokemonAutomation/Packages (Resources/PokemonBDSP/Maps) when ready
*
*/

#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Maps_H
#define PokemonAutomation_PokemonBDSP_AutoStory_Maps_H

#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "Pokemon/Navigation/Pokemon_Grid.h"
#include "Pokemon/Navigation/Pokemon_MapData.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonBDSP{


// (name, JSON text) of each map in PokemonBDSP/Programs/AutoStory/Maps/,
// built into the program by pa_embed_text_files() (cmake/EmbedTextFiles.cmake).
const std::vector<std::pair<std::string, std::string>>& bdsp_map_json();

// The map `name`, read from bdsp_map_json() on first use, with its patches
// applied. Throws InternalProgramError for a name it does not have, and
// ParseException if a map does not parse or is not named as its file.
// The name is a string_view so that a temporary string argument is not
// mistaken (by GCC's -Wdangling-reference) for what the result refers to.
const Pokemon::MapData& story_map(std::string_view name);

// The point `point` of the map `map`. Throws InternalProgramError for an
// unknown map or point.
Pokemon::TilePos map_point(std::string_view map, std::string_view point);

// The tiles of the region `region` of the map `map`, in order. Throws
// InternalProgramError for an unknown map, or a region with no tiles.
std::vector<Pokemon::TilePos> map_region(std::string_view map, std::string_view region);



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