diff --git a/.github/workflows/bdsp-map-resources.yml b/.github/workflows/bdsp-map-resources.yml new file mode 100644 index 0000000000..644eecd648 --- /dev/null +++ b/.github/workflows/bdsp-map-resources.yml @@ -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 diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index efb3c80a04..c21b13ec85 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -88,6 +88,14 @@ jobs: ref: 'master' path: 'Arduino-Source/Packages' + - name: Prepare pinned BDSP map resources + id: map-resources + uses: dayfine/PokemonSimulation/integrations/arduino-source/verify-map-packs@4de7742a764d27293b4525d6ad2a6fc8a95f3e3a + with: + layout: resources + operation: prepare + consumer-directory: Arduino-Source + - name: Install Qt uses: jurplel/install-qt-action@v4 with: @@ -204,6 +212,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 diff --git a/.github/workflows/runtime-map-reader.yml b/.github/workflows/runtime-map-reader.yml index 1fd59940a7..027383c60b 100644 --- a/.github/workflows/runtime-map-reader.yml +++ b/.github/workflows/runtime-map-reader.yml @@ -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 diff --git a/.gitignore b/.gitignore index ef6f409ae9..ab64316491 100644 --- a/.gitignore +++ b/.gitignore @@ -77,7 +77,11 @@ build_*/ .cache/* Packages/* - -# Local agent/linter environments. -.venv-code-health/ -__pycache__/ + +# Local agent/linter environments. +.venv-code-health/ +__pycache__/ + +# Prepared, pinned PokemonSimulation maps are external build inputs. +Packages/Resources/Pokemon/Maps/ +.pokesim-map-source/ diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index 62d006eb4c..5c800bb009 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -230,6 +230,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 diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 15aaea005b..211ed49ce7 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -43,6 +43,7 @@ #include "Pokemon/Navigation/Pokemon_RouteLog_Tests.h" #include "Pokemon/Navigation/Pokemon_RuntimeMap_Tests.h" #include "Pokemon/Navigation/Pokemon_WallRecovery_Tests.h" +#include "PokemonBDSP/PokemonBDSP_Tests.h" #include "VideoGames/PokemonHome/PokemonHome_Tests.h" #include "VideoGames/PokemonSwSh/PokemonSwSh_Tests.h" #include "VideoGames/PokemonLA/PokemonLA_Tests.h" @@ -94,6 +95,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ NintendoSwitch::add_tests_FailedToConnectDetector(ret); NintendoSwitch::add_tests_UpdatePopupDetector(ret); NintendoSwitch::Typewriter::add_tests_TypewriterTextMapping(ret); + NintendoSwitch::PokemonBDSP::add_tests(ret); NintendoSwitch::PokemonBDSP::add_tests_DialogDetector(ret); NintendoSwitch::PokemonBDSP::add_tests_MenuDetector(ret); NintendoSwitch::PokemonFRLG::add_tests(ret); diff --git a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp index b8ff5e26a5..36f4de8f6d 100644 --- a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp @@ -5,6 +5,7 @@ */ #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h" #include "PokemonBDSP_Tests.h" namespace PokemonAutomation{ @@ -14,6 +15,7 @@ namespace PokemonBDSP{ void add_tests(UnitTestDatabase& database){ + add_tests_AutoStory_Maps(database); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.cpp new file mode 100644 index 0000000000..1992a6cb4d --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.cpp @@ -0,0 +1,77 @@ +/* BDSP AutoStory - Maps + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#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>& story_maps_by_name(){ + static const std::map> maps = []{ + std::map> parsed; + for (const auto& [name, json] : bdsp_map_json()){ + MapData map = Pokemon::parse_map(parse_json(json), name + ".json"); + 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 map_region(std::string_view map, std::string_view region){ + const MapData& data = story_map(map); + std::vector 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 diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h new file mode 100644 index 0000000000..988fb7b52f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h @@ -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 ".1", ".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 +#include +#include +#include +#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>& 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 map_region(std::string_view map, std::string_view region); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.cpp new file mode 100644 index 0000000000..01174e5b99 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.cpp @@ -0,0 +1,420 @@ +/* BDSP AutoStory Maps Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The built-in story maps (Data/PokemonBDSP_AutoStory_Maps.h): every map + * loads, its points and objects stand where they can, and the lookups + * find what the story code asks for. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP_AutoStory_Maps_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +using Pokemon::MapData; +using Pokemon::MapObject; +using Pokemon::TileKind; +using Pokemon::TilePos; + +bool is_ground(TileKind tile){ + return tile == TileKind::OPEN || tile == TileKind::TALL_GRASS; +} + +// The person or item that stands still on `pos`, if any. +const MapObject* standing_object(const MapData& map, const TilePos& pos){ + for (const MapObject& object : map.objects){ + if (!object.wander && object.x == pos.x && object.y == pos.y){ + return &object; + } + } + return nullptr; +} + +// "rocks" for "rocks.3", the point's own name otherwise. +std::string region_of(const std::string& point){ + const size_t dot = point.rfind('.'); + if (dot == std::string::npos || dot + 1 == point.size()){ + return point; + } + const bool numbered = std::all_of(point.begin() + dot + 1, point.end(), [](char c){ return '0' <= c && c <= '9'; }); + return numbered ? point.substr(0, dot) : point; +} + + +// Every built-in map parses through the shared loader, named as its file. +UnitTestResult test_story_maps_load(Logger& logger){ + if (bdsp_map_json().empty()){ + return std::string("No built-in maps."); + } + for (const auto& [name, json] : bdsp_map_json()){ + try{ + const MapData& map = story_map(name); + if (map.name != name){ + return std::format("{}.json: story_map() returned \"{}\".", name, map.name); + } + logger.log(std::format( + "{}: {} x {}, {} points, {} objects, {} patches", + name, map.tiles.width, map.tiles.height, map.points.size(), map.objects.size(), map.patches.size() + )); + }catch (Exception& e){ + return std::format("{}: {}: {}", name, e.name(), e.message()); + } + } + return true; +} + + +// What a named point must be on. Points not listed are ground the player +// stands on, with nobody standing there. +enum class PointOn{ + DOOR, + SIGN, + PERSON, // someone stands still there + WALL, // a rock until smashed +}; +struct PointCase{ + std::string map; + std::string point; // a point, or a region for all its tiles + PointOn on; +}; + +const std::vector& point_cases(){ + static const std::vector cases{ + {.map = "Route201", .point = "border_sign", .on = PointOn::SIGN}, + {.map = "Route202", .point = "trainer_tristan", .on = PointOn::PERSON}, + {.map = "Route202", .point = "trainer_natalie", .on = PointOn::PERSON}, + {.map = "Route202", .point = "trainer_logan", .on = PointOn::PERSON}, + {.map = "Route204North", .point = "trainer_twin_left", .on = PointOn::PERSON}, + {.map = "Route204North", .point = "trainer_twin_right", .on = PointOn::PERSON}, + {.map = "Route204North", .point = "trainer_taylor", .on = PointOn::PERSON}, + {.map = "Route204North", .point = "trainer_brandon", .on = PointOn::PERSON}, + {.map = "Route204South", .point = "trainer_samantha", .on = PointOn::PERSON}, + {.map = "Route204South", .point = "trainer_sarah", .on = PointOn::PERSON}, + {.map = "Route204South", .point = "trainer_tyler", .on = PointOn::PERSON}, + {.map = "SandgemTown", .point = "lab_door", .on = PointOn::DOOR}, + {.map = "SandgemTown", .point = "pokemon_center_door", .on = PointOn::DOOR}, + {.map = "SandgemTown", .point = "pokemon_center_sign", .on = PointOn::SIGN}, + {.map = "SandgemTown", .point = "poke_mart_door", .on = PointOn::DOOR}, + {.map = "JubilifeCity", .point = "trainers_school_door", .on = PointOn::DOOR}, + {.map = "JubilifeCity", .point = "pokemon_center_door", .on = PointOn::DOOR}, + {.map = "JubilifeCity", .point = "tv_station_door", .on = PointOn::DOOR}, + {.map = "JubilifeCity", .point = "poketch_president", .on = PointOn::PERSON}, + {.map = "JubilifeCity", .point = "clown_mart", .on = PointOn::PERSON}, + {.map = "JubilifeCity", .point = "clown_tv_station", .on = PointOn::PERSON}, + {.map = "JubilifeCity", .point = "clown_poketch_company", .on = PointOn::PERSON}, + {.map = "OreburghCity", .point = "pokemon_center_door", .on = PointOn::DOOR}, + {.map = "OreburghCity", .point = "gym_door", .on = PointOn::DOOR}, + {.map = "OreburghCity", .point = "gate_mouth", .on = PointOn::DOOR}, + {.map = "OreburghGym", .point = "roark", .on = PointOn::PERSON}, + {.map = "OreburghGym", .point = "trainer_first_tier", .on = PointOn::PERSON}, + {.map = "OreburghGym", .point = "trainer_second_tier", .on = PointOn::PERSON}, + {.map = "OreburghMine", .point = "roark", .on = PointOn::PERSON}, + {.map = "RavagedPath", .point = "rocks", .on = PointOn::WALL}, + {.map = "FloaromaTown", .point = "old_woman", .on = PointOn::PERSON}, + {.map = "FloaromaTown", .point = "old_man", .on = PointOn::PERSON}, + {.map = "FloaromaTown", .point = "pokemon_center_door", .on = PointOn::DOOR}, + {.map = "TwinleafTown", .point = "player_house_door", .on = PointOn::DOOR}, + {.map = "TwinleafTown", .point = "rival_house_door", .on = PointOn::DOOR}, + {.map = "PlayerHouse1F", .point = "mom", .on = PointOn::PERSON}, + {.map = "PlayerHouse1F", .point = "door", .on = PointOn::DOOR}, + {.map = "RivalHouse1F", .point = "door", .on = PointOn::DOOR}, + {.map = "RowansLab", .point = "door", .on = PointOn::DOOR}, + }; + return cases; +} + +// An error naming what `pos` should be, or empty when it is. +std::string check_point_on(const MapData& map, const TilePos& pos, PointOn on){ + const TileKind tile = map.tiles.at(pos); + const MapObject* object = standing_object(map, pos); + switch (on){ + case PointOn::DOOR: + return tile == TileKind::DOOR ? "" : "not a door"; + case PointOn::SIGN: + return tile == TileKind::SIGN ? "" : "not a sign"; + case PointOn::PERSON: + return is_ground(tile) && object != nullptr && object->kind == Pokemon::ObjectKind::NPC ? "" : "nobody stands still there"; + case PointOn::WALL: + return tile == TileKind::WALL ? "" : "not a wall"; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown PointOn."); +} + +std::string check_ground(const MapData& map, const TilePos& pos){ + if (!is_ground(map.tiles.at(pos))){ + return std::format("'{}', not ground", Pokemon::glyph_of(map.tiles.at(pos))); + } + return standing_object(map, pos) == nullptr ? "" : "someone stands there"; +} + + +// Every point is ground with nobody on it, unless point_cases() says +// otherwise; every numbered region runs from 1 with no gap; every case +// names a point the map has; every warp leads to a point that exists. +UnitTestResult test_story_map_points(Logger& logger){ + for (const PointCase& item : point_cases()){ + const MapData& map = story_map(item.map); + if (!map.points.contains(item.point) && !map.points.contains(item.point + ".1")){ + return std::format("{}: no point or region \"{}\".", item.map, item.point); + } + } + size_t checked = 0; + for (const auto& [name, json] : bdsp_map_json()){ + const MapData& map = story_map(name); + for (const auto& [point, pos] : map.points){ + const std::string region = region_of(point); + // map_region() counts from 1 and stops at a gap. + if (region != point && std::stoul(point.substr(region.size() + 1)) > map_region(name, region).size()){ + return std::format("{}: {} is not reached counting {}.1, {}.2, ...", name, point, region, region); + } + const auto it = std::ranges::find_if(point_cases(), [&](const PointCase& item){ + return item.map == name && item.point == region; + }); + const std::string error = it == point_cases().end() ? check_ground(map, pos) : check_point_on(map, pos, it->on); + if (!error.empty()){ + return std::format("{}: {} {}: {}.", name, point, Pokemon::to_string(pos), error); + } + checked++; + } + for (const Pokemon::MapWarp& warp : map.warps){ + if (!warp.to){ + return std::format("{}: the warp at {} leads nowhere.", name, Pokemon::to_string(TilePos{warp.x, warp.y})); + } + try{ + map_point(warp.to->map, warp.to->point); + }catch (InternalProgramError& e){ + return std::format("{}: the warp at {}: {}", name, Pokemon::to_string(TilePos{warp.x, warp.y}), e.message()); + } + } + } + logger.log(std::format("{} points checked.", checked)); + return true; +} + + +// Everyone stands on ground, and a wanderer's box holds their tile. +UnitTestResult test_story_map_objects(Logger& logger){ + size_t checked = 0; + for (const auto& [name, json] : bdsp_map_json()){ + const MapData& map = story_map(name); + for (const MapObject& object : map.objects){ + const TilePos pos{object.x, object.y}; + if (!is_ground(map.tiles.at(pos))){ + return std::format("{}: {} stands on '{}' at {}.", name, object.name, Pokemon::glyph_of(map.tiles.at(pos)), Pokemon::to_string(pos)); + } + const auto& box = object.wander; + if (box && (pos.x < box->x0 || pos.x > box->x1 || pos.y < box->y0 || pos.y > box->y1)){ + return std::format("{}: {} at {} is outside their wander box.", name, object.name, Pokemon::to_string(pos)); + } + checked++; + } + } + logger.log(std::format("{} objects checked.", checked)); + return true; +} + + +// Route 202's regions are the tiles the story code computed before they +// were data: each case is the old helper's loop, over the map's tiles. +struct RegionCase{ + std::string region; + std::function(const MapData& map)> old_helper; +}; + +// The tiles of a box, both corners inclusive, row by row, that someone +// can stand on: ground with nobody standing still there. +std::vector standable_in_box(const MapData& map, const TilePos& low, const TilePos& high){ + std::vector tiles; + for (int y = low.y; y <= high.y; y++){ + for (int x = low.x; x <= high.x; x++){ + if (map.tiles.contains({x, y}) && is_ground(map.tiles.at({x, y})) && standing_object(map, {x, y}) == nullptr){ + tiles.push_back({x, y}); + } + } + } + return tiles; +} + +std::vector after_training_area(const MapData& map){ + const TilePos catch_spot = map_point(map.name, "catch_spot"); + return standable_in_box(map, {catch_spot.x - 1, catch_spot.y - 2}, {catch_spot.x + 1, catch_spot.y + 2}); +} + +UnitTestResult test_route202_regions(Logger& logger){ + const std::vector cases{ + { + .region = "catch_save_area", // story_catch_save_area() + .old_helper = [](const MapData& map){ return standable_in_box(map, {15, 25}, {21, 29}); }, + }, + { + .region = "after_training_area", // story_after_training_area() + .old_helper = after_training_area, + }, + { + .region = "catch_grass", // the story table's catch_grass() + .old_helper = [](const MapData& map){ + std::vector grass = after_training_area(map); + std::erase_if(grass, [&](const TilePos& pos){ return map.tiles.at(pos) != TileKind::TALL_GRASS; }); + return grass; + }, + }, + { + .region = "jubilife_exit", // the story table's jubilife_exit() + .old_helper = [](const MapData& map){ return standable_in_box(map, {0, 0}, {map.tiles.width - 1, 3}); }, + }, + }; + const MapData& map = story_map("Route202"); + for (const RegionCase& item : cases){ + const std::vector region = map_region("Route202", item.region); + const std::vector expected = item.old_helper(map); + if (region != expected){ + return std::format("Route202 {}: {} tiles, the old helper computes {}.", item.region, region.size(), expected.size()); + } + logger.log(std::format("Route202 {}: {} tiles.", item.region, region.size())); + } + return true; +} + + +// map_point() and map_region() find what the story asks for, regions in +// order, and throw for what a map does not have. +struct LookupCase{ + std::string name; + std::function()> lookup; + // The tiles, or empty when the lookup must throw InternalProgramError. + std::vector expected; +}; + +UnitTestResult test_story_map_lookups(Logger&){ + const std::vector cases{ + { + .name = "a point", + .lookup = []{ return std::vector{map_point("SandgemTown", "pokemon_center_door")}; }, + .expected = {{17, 10}}, + }, + { + .name = "a region, in order", + .lookup = []{ return map_region("Route204North", "floaroma_waypoints"); }, + .expected = {{20, 27}, {11, 27}, {11, 22}, {19, 22}, {19, 12}, {11, 12}, {11, 5}}, + }, + { + .name = "an unknown map", + .lookup = []{ return std::vector{map_point("Route299", "door")}; }, + .expected = {}, + }, + { + .name = "an unknown point", + .lookup = []{ return std::vector{map_point("SandgemTown", "gym_door")}; }, + .expected = {}, + }, + { + .name = "a point is not a region", + .lookup = []{ return map_region("SandgemTown", "lab_door"); }, + .expected = {}, + }, + }; + for (const LookupCase& item : cases){ + std::vector tiles; + try{ + tiles = item.lookup(); + }catch (InternalProgramError&){ + if (item.expected.empty()){ + continue; + } + return item.name + ": threw InternalProgramError."; + } + if (tiles != item.expected){ + std::string read; + for (const TilePos& pos : tiles){ + read += " " + Pokemon::to_string(pos); + } + return std::format("{}: read{}, expected {} tiles{}.", item.name, read, item.expected.size(), + item.expected.empty() ? " (an InternalProgramError)" : ""); + } + } + return true; +} + + +struct StoryMapsInput{ + std::string name; + std::function body; +}; + +struct StoryMapsExpectation{ +}; + +struct StoryMapsCase{ + StoryMapsInput input; + StoryMapsExpectation expectation; +}; + +class Test_StoryMaps : public UnitTest{ +public: + explicit Test_StoryMaps(StoryMapsCase test_case) + : UnitTest("PokemonBDSP::AutoStory - Maps: " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + StoryMapsCase m_case; +}; + +} // namespace + + +const StoryMapsCase STORY_MAPS_CASES[] = { + { + .input = {"the built-in JSON maps load", test_story_maps_load}, + .expectation = {}, + }, + { + .input = {"points stand where they can", test_story_map_points}, + .expectation = {}, + }, + { + .input = {"objects stand on open ground", test_story_map_objects}, + .expectation = {}, + }, + { + .input = {"map_point() and map_region()", test_story_map_lookups}, + .expectation = {}, + }, + { + .input = {"Route 202's regions are what the story computed", test_route202_regions}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_Maps(UnitTestDatabase& database){ + for (const auto& item : STORY_MAPS_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h new file mode 100644 index 0000000000..dc3d4cc52e --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h @@ -0,0 +1,24 @@ +/* BDSP AutoStory Maps Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Maps_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Maps_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Registers AutoStory map unit tests with the database. +void add_tests_AutoStory_Maps(UnitTestDatabase& database); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 74c6145c87..a1349c3d6b 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1785,6 +1785,10 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/PokemonBDSP_Settings.h Source/PokemonBDSP/PokemonBDSP_Tests.cpp Source/PokemonBDSP/PokemonBDSP_Tests.h + Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.cpp + Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h Source/PokemonBDSP/Programs/Eggs/PokemonBDSP_EggAutonomous.cpp Source/PokemonBDSP/Programs/Eggs/PokemonBDSP_EggAutonomous.h Source/PokemonBDSP/Programs/Eggs/PokemonBDSP_EggAutonomousState.cpp diff --git a/devtools/maps/README.md b/devtools/maps/README.md index a3c713c0dc..37f94d3416 100644 --- a/devtools/maps/README.md +++ b/devtools/maps/README.md @@ -1,5 +1,11 @@ # Map tools for AutoStory routes +BDSP maps are now pinned external build resources. See +[RESOURCE_MAPS.md](RESOURCE_MAPS.md) for preparation, publishing prerequisites +and native validation. The Sinnoh decomp CLI below forwards to the shared +PokemonSimulation parser; set `PYTHONPATH=/path/to/pokesim/src`. Author map +corrections and run decoder/builder tests there. Image fetch/overlay remain local. + Offline helpers that give a route planner its tile grid (standard library only, Python 3.8+): a decoder for the Pokemon Platinum and Diamond decompilations, an overlay that draws a grid and a run's routes over the map diff --git a/devtools/maps/RESOURCE_MAPS.md b/devtools/maps/RESOURCE_MAPS.md new file mode 100644 index 0000000000..874a94d618 --- /dev/null +++ b/devtools/maps/RESOURCE_MAPS.md @@ -0,0 +1,91 @@ +# BDSP map resources + +BDSP's existing `bdsp_map_json()` API embeds the 22 maps from prepared +`Packages/Resources/Pokemon/Maps/bdsp`. The shared nav-core reader handles their +compact v2 format. Source-map copies and the duplicate BDSP builder/Sinnoh parser +are removed; Python authoring and its tests live in PokemonSimulation. Native +map/route/story tests remain here. No controller behavior changes in this handoff. + +## Publishing prerequisite and pins + +[PokemonSimulation #65](https://github.com/dayfine/PokemonSimulation/pull/65) +merged by squash as `4de7742a764d27293b4525d6ad2a6fc8a95f3e3a`. The checked-in resource lock and +CI tools now use that approved revision. The squash preserves the reviewed +candidate's manifest/license and all 22 map bytes. Normal resource preparation +verifies the published ancestry, manifest hash and exports before configuration. + +Before enrichment, all 22 maps matched #226 at +`fc56a720b9c0b1f4d945dbef382d61b0a6382b97` byte-for-byte. The publishing change +adds resolved Diamond/Platinum-baseline identities, quantities, collection flags +and provenance to 15 pickups, with no geometry, point, NPC, trigger or warp edits. +Pickup names and quantity 1 are omitted on the wire: `source_event: 3` +restores internal label `item_3`; `item: escape_rope` identifies the reward. +Explicit custom names and quantities above one remain supported. +These are explicitly remake baselines, not verified BDSP item data. Reading a +map never grants an item or proves that a controller action collected it. + +The shared v2 normalizer preserves all JSON extensions. The existing native +`MapData` model consumes its known navigation fields (including collection +flags); typed battle/inventory consumers for reward metadata remain future work. + +## Prepare and build + +On a fresh Arduino checkout, clone Packages before preparing the map subdirectory. +Skip this command when the checkout already has its Packages repository: + +```sh +git clone --depth 1 --branch master https://github.com/PokemonAutomation/Packages.git /path/to/Arduino-Source/Packages +``` + +Use the same approved tool revision pinned in CI. Preparation reads the separate +data revision from the resource lock; tools and data pins are independent. + +```sh +git clone https://github.com/dayfine/PokemonSimulation.git /path/to/pokesim +git -C /path/to/pokesim checkout 4de7742a764d27293b4525d6ad2a6fc8a95f3e3a +PYTHONPATH=/path/to/pokesim/src python3 -m pokesim.maps.consumer_sync prepare \ + --layout resources --source /path/to/pokesim --consumer /path/to/Arduino-Source +cmake -S SerialPrograms -B /path/to/build \ + -DPOKESIM_TOOLS_SOURCE=/path/to/pokesim -DPOKESIM_SOURCE_CACHE=/path/to/pokesim +cmake --build /path/to/build -j4 +``` + +Prepared resources include license/provenance and are ignored by Git. CMake +checks their exact inventory, hashes and publishing ancestry before embedding. +Missing, modified, extra or unapproved files fail; configuration never fetches +maps or falls back to an old authored directory. + +To deliberately advance the approved BDSP data pin after publication: + +```sh +PYTHONPATH=/path/to/pokesim/src python3 -m pokesim.maps.consumer_sync update \ + --layout resources --source /path/to/pokesim --consumer /path/to/Arduino-Source \ + --game bdsp --revision FULL_APPROVED_PUBLISHING_SHA +``` + +Update validates the existing lock and files before changing either; it preserves +independent game pins. Commit the resource lock, not the generated Packages files. +Do not rewrite the resource pin merely to make a locally edited map pass. + +## Offline tools and tests + +The existing decomp CLI forwards to the publisher parser. Fetch and overlay +commands still work without the Python package. For decompilation, supply the +approved tools on PYTHONPATH: + +```sh +PYTHONPATH=/path/to/pokesim/src python3 -m devtools.maps decomp ROUTE_204_NORTH --text +python3 -m unittest devtools.maps.tests.test_overlay +cmake -S devtools/runtime-map-tests -B build/runtime-map-tests +cmake --build build/runtime-map-tests -j4 +ctest --test-dir build/runtime-map-tests --output-on-failure +python3 devtools/runtime-map-tests/prepare_parity.py --publisher /path/to/candidate \ + --publisher-revision 4de7742a764d27293b4525d6ad2a6fc8a95f3e3a --out /tmp/bdsp-map-pairs.json +build/runtime-map-tests/RuntimeMapTests /tmp/bdsp-map-pairs.json +``` + +Parity fixtures require a clean checkout of the exact supplied candidate SHA. +They can test an unmerged candidate; that does not approve it as a resource pin. +The native schema workflow compares 150 complete records on three platforms. +Full application build/story validation is a separate gate after publication. +This fork resource setup is not an upstream Packages submission. diff --git a/devtools/maps/__main__.py b/devtools/maps/__main__.py index 57656e2a62..5a89522c7f 100644 --- a/devtools/maps/__main__.py +++ b/devtools/maps/__main__.py @@ -10,7 +10,7 @@ import sys from pathlib import Path -from . import decomp, fetch, grid_source, route_log, svg +from . import fetch, grid_source, route_log, svg CACHE = Path.home() / '.cache' / 'maps' @@ -26,7 +26,7 @@ def main(argv=None): sub = parser.add_subparsers(dest='command', required=True) dec = sub.add_parser('decomp', help="decode a map's tile grid and objects from the pret decompilation") dec.add_argument('map', help='the map header name, e.g. ROUTE_204_NORTH or RAVAGED_PATH') - dec.add_argument('--repo', default=decomp.Platinum.REPO, choices=[decomp.Platinum.REPO, decomp.Diamond.REPO], + dec.add_argument('--repo', default="pret/pokeplatinum", choices=["pret/pokeplatinum", "pret/pokediamond"], help='which decompilation to read (default: pret/pokeplatinum)') dec.add_argument('--rect', nargs=4, type=int, metavar=('X0', 'Z0', 'W', 'H'), help="world rectangle to decode; default: the map's own cell(s)") @@ -53,6 +53,10 @@ def main(argv=None): args = parser.parse_args(argv) if args.command == 'decomp': + try: + from pokesim.maps import dppt_map_decomp_parser as decomp + except ModuleNotFoundError: + parser.error('Set PYTHONPATH to the pinned PokemonSimulation src directory; see devtools/maps/RESOURCE_MAPS.md.') game = decomp.game_for(args.repo, args.cache / 'decomp', args.local) decoded = decomp.decode(game, args.map, tuple(args.rect) if args.rect else None) print(decomp.render_text(decoded) if args.text else decomp.render_cpp(decoded)) diff --git a/devtools/maps/decomp.py b/devtools/maps/decomp.py deleted file mode 100644 index f5e85b6c55..0000000000 --- a/devtools/maps/decomp.py +++ /dev/null @@ -1,530 +0,0 @@ -"""Decodes a map's tile grid from the Pokemon Platinum or Diamond decompilation. - -The pret decompilations hold each 32 x 32 map cell's collision as a land data -file: a 16-byte header of four u32 sizes (attributes, movement models, map -models, height data), then 1024 little-endian u16 tile attributes, row-major, -index z * 32 + x. Bit 0x8000 blocks the tile; the low byte is its behaviour -(tall grass, water, ledges, doors, warps). Cells sit in a matrix: the -overworld's is 30 x 30 (matrix 000), caves have their own; world -x = 32 * column + lx, z = 32 * row + lz, and the events files place objects -in these world coordinates. - -pokeplatinum keeps matrices and events as JSON and its map headers in -include/data/map_headers.h. pokediamond keeps them as binary NARC entries -(the layout pokeplatinum's tools/jsoncnv packs) with the header table in -arm9/src/map_header.c, and gives sprites, movement types and trainer types -by number; pokeplatinum's generated/*.txt tables name them. BDSP keeps -Diamond/Pearl's layout; Platinum's few changes show up on the console. -""" -import datetime -import json -import re -import struct -import subprocess -import textwrap -from dataclasses import dataclass, field -from pathlib import Path - -CELL = 32 -# Tile attribute bits and behaviours (include/constants/field/map_tile_behaviors.h). -BLOCKED = 0x8000 -TALL_GRASS = {0x02, 0x03} -WATER = {0x10, 0x13, 0x15} # river, waterfall, sea -JUMP = {0x38: 'east', 0x39: 'west', 0x3a: 'north', 0x3b: 'south'} -JUMP_TWICE = {0x5a: 'north', 0x5b: 'south', 0x5c: 'west', 0x5d: 'east'} -WARP_STAIRS = {0x5e, 0x5f} -CAVE_MOUTHS = {0x62, 0x63, 0x64, 0x65} # blocked, walked into -DOOR = 0x69 -WARP_TILES = {0x6c, 0x6d, 0x6e, 0x6f} # walkable, stood on -# Objects that stand on the ground and block it. -OBSTACLES = {'CUT_TREE', 'ROCK_SMASH', 'STRENGTH_BOULDER'} -ITEM = 'POKEBALL' -# bg events: 0 facing, 1 wall sign, 2 hidden item (generated/bg_event_types.txt). -BG_SIGNS = {0, 1} - - -def tile_char(attr): - """Our legend for one tile attribute: (char, note or None).""" - behaviour = attr & 0xff - if behaviour in JUMP or behaviour in JUMP_TWICE: - twice = behaviour in JUMP_TWICE - side = JUMP_TWICE[behaviour] if twice else JUMP[behaviour] - if side == 'south': - return 'v', 'double ledge' if twice else None - return '#', f'ledge jumped {side}{" twice" if twice else ""}, shown as a wall' - if behaviour == DOOR or behaviour in CAVE_MOUTHS: - return 'D', None - if behaviour in WARP_TILES or behaviour in WARP_STAIRS: - return 'W', None - if behaviour in WATER: - return '#', 'water' - if attr & BLOCKED: - return '#', None - if behaviour in TALL_GRASS: - return '"', None - return '.', None - - -def decode_land_data(data): - """The 1024 tile attributes of a land data file (map_data_NNN.bin / narc_NNNN.bin).""" - sizes = struct.unpack('<4I', data[:16]) - if sizes[0] != 2 * CELL * CELL: - raise ValueError(f'land data: attribute block is {sizes[0]} bytes, expected {2 * CELL * CELL}') - return list(struct.unpack(f'<{CELL * CELL}H', data[16:16 + sizes[0]])) - - -def gh_fetch(repo, path): - """One file of a GitHub repository, raw; binary works.""" - result = subprocess.run( - ['gh', 'api', f'repos/{repo}/contents/{path}', '-H', 'Accept: application/vnd.github.raw'], - check=True, stdout=subprocess.PIPE) - return result.stdout - - -class Source: - """Files of one decompilation: a local checkout, or fetched once into the cache.""" - - def __init__(self, repo, cache, local=None, fetch=gh_fetch): - self.repo = repo - self.cache = Path(cache) - self.local = Path(local) if local else None - self.fetch = fetch - - def read(self, path): - if self.local: - return (self.local / path).read_bytes() - cached = self.cache / self.repo / path - if not cached.exists(): - data = self.fetch(self.repo, path) - cached.parent.mkdir(parents=True, exist_ok=True) - cached.write_bytes(data) - return cached.read_bytes() - - def text(self, path): - return self.read(path).decode('utf-8', errors='replace') - - -@dataclass -class MapHeader: - name: str - matrix_id: int - events_id: object # Platinum: events archive name; Diamond: NARC index - - -@dataclass -class Matrix: - matrix_id: int - width: int - height: int - maps: list # land data id per cell, row-major, None for MAP_NONE - headers: list = None # map header name per cell, or None when the matrix has none - - def cells_of(self, header_name): - """(row, col) of every cell carrying this header; all cells when the matrix has no headers.""" - cells = [(i // self.width, i % self.width) for i in range(self.width * self.height) - if self.maps[i] is not None and (self.headers is None or self.headers[i] == header_name)] - return cells - - -@dataclass -class ObjectEvent: - name: str - gfx: str - movement: str - trainer: bool - sight: int - range_x: int - range_z: int - x: int - z: int - hidden: str = '' - - -@dataclass -class Warp: - x: int - z: int - dest: str - - -@dataclass -class Sign: - x: int - z: int - - -@dataclass -class Events: - objects: list = field(default_factory=list) - warps: list = field(default_factory=list) - signs: list = field(default_factory=list) - - -def strip_prefix(name, prefix): - return name[len(prefix):] if name.startswith(prefix) else name - - -class Platinum: - """pret/pokeplatinum: JSON matrices and events, map headers in include/data/map_headers.h.""" - REPO = 'pret/pokeplatinum' - GAME = 'Pokemon Platinum' - HEADER = re.compile(r'\[MAP_HEADER_(\w+)\]\s*=\s*\{(.*?)\}', re.S) - - def __init__(self, source): - self.source = source - self._headers = None - - def header(self, name): - if self._headers is None: - text = self.source.text('include/data/map_headers.h') - self._headers = {m.group(1): m.group(2) for m in self.HEADER.finditer(text)} - body = self._headers.get(name) - if body is None: - raise KeyError(f'{name}: no MAP_HEADER_{name} in include/data/map_headers.h') - matrix = re.search(r'\.mapMatrixID\s*=\s*map_matrix_(\d+)', body) - events = re.search(r'\.eventsArchiveID\s*=\s*(\w+)', body) - return MapHeader(name, int(matrix.group(1)), events.group(1)) - - def matrix(self, matrix_id): - data = json.loads(self.source.text(f'res/field/matrices/map_matrix_{matrix_id:03d}.json')) - maps = [None if m == 'MAP_NONE' else int(m[len('MAP_'):]) for row in data['maps'] for m in row] - headers = [strip_prefix(h, 'MAP_HEADER_') for row in data['headers'] for h in row] or None - return Matrix(matrix_id, len(data['maps'][0]), len(data['maps']), maps, headers) - - def land_name(self, land_id): - return f'map_data_{land_id:03d}' - - def land_path(self, land_id): - return f'res/field/maps/data/{self.land_name(land_id)}.bin' - - def land_data(self, land_id): - return decode_land_data(self.source.read(self.land_path(land_id))) - - def events_name(self, events_id): - return f'{events_id}.json' - - def events(self, events_id): - data = json.loads(self.source.text(f'res/field/events/{events_id}.json')) - events = Events() - for o in data.get('object_events', []): - trainer = o['trainer_type'] != 'TRAINER_TYPE_NONE' - events.objects.append(ObjectEvent( - name=title(strip_prefix(o['id'], 'LOCALID_')), - gfx=strip_prefix(o['graphics_id'], 'OBJ_EVENT_GFX_'), - movement=strip_prefix(o['movement_type'], 'MOVEMENT_TYPE_'), - trainer=trainer, - sight=(o.get('data') or [0])[0] if trainer else 0, - range_x=o.get('movement_range_x', 0), range_z=o.get('movement_range_z', 0), - x=o['x'], z=o['z'], - hidden='' if str(o.get('hidden_flag', '0')) == '0' else str(o['hidden_flag']))) - for w in data.get('warp_events', []): - events.warps.append(Warp(w['x'], w['z'], strip_prefix(w['dest_header_id'], 'MAP_HEADER_'))) - for b in data.get('bg_events', []): - if b['type'] in BG_SIGNS: - events.signs.append(Sign(b['x'], b['z'])) - return events - - -class Diamond: - """pret/pokediamond: binary matrices and events, the header table in arm9/src/map_header.c. - - Sprites, movement types and trainer types are numbers there; `names` is a - Source for pret/pokeplatinum, whose generated/*.txt tables list them in - order (checked on Route 204 North: the same twelve objects come out). - """ - REPO = 'pret/pokediamond' - GAME = 'Pokemon Diamond' - HEADER_LINE = re.compile(r'\{([^{}]*)\}\s*,?\s*//\s*(MAP_\w+)') - DEFINE = re.compile(r'#define\s+(MAP_\w+)\s+(\d+)') - - def __init__(self, source, names): - self.source = source - self.names = names - self._headers = None - self._map_ids = None - self._tables = {} - - def header(self, name): - if self._headers is None: - text = self.source.text('arm9/src/map_header.c') - self._headers = {m.group(2): m.group(1) for m in self.HEADER_LINE.finditer(text)} - body = self._headers.get(f'MAP_{name}') - if body is None: - raise KeyError(f'{name}: no MAP_{name} row in arm9/src/map_header.c') - matrix = re.search(r'NARC_map_matrix_narc_(\d+)_bin', body) - events = re.search(r'NARC_zone_event_release_narc_(\d+)_bin', body) - return MapHeader(name, int(matrix.group(1)), int(events.group(1))) - - def map_name(self, map_id): - """MAP_ constant name (without the prefix) for a numeric map header id.""" - if self._map_ids is None: - text = self.source.text('include/constants/maps.h') - self._map_ids = {int(m.group(2)): m.group(1) for m in self.DEFINE.finditer(text)} - return strip_prefix(self._map_ids.get(map_id, f'MAP_{map_id}'), 'MAP_') - - def table(self, name): - if name not in self._tables: - self._tables[name] = self.names.text(f'generated/{name}.txt').split() - return self._tables[name] - - def named(self, table, prefix, value): - entries = self.table(table) - return strip_prefix(entries[value], prefix) if value < len(entries) else str(value) - - def matrix(self, matrix_id): - data = self.source.read(f'files/fielddata/mapmatrix/map_matrix/narc_{matrix_id:04d}.bin') - width, height, has_headers, has_altitudes, name_len = struct.unpack_from('<5B', data, 0) - offset = 5 + name_len - count = width * height - headers = None - if has_headers: - headers = [self.map_name(h) for h in struct.unpack_from(f'<{count}H', data, offset)] - offset += 2 * count - if has_altitudes: - offset += count - maps = [None if m == 0xffff else m for m in struct.unpack_from(f'<{count}H', data, offset)] - return Matrix(matrix_id, width, height, maps, headers) - - def land_name(self, land_id): - return f'land_data narc_{land_id:04d}' - - def land_path(self, land_id): - return f'files/fielddata/land_data/land_data_release/narc_{land_id:04d}.bin' - - def land_data(self, land_id): - return decode_land_data(self.source.read(self.land_path(land_id))) - - def events_name(self, events_id): - return f'zone_event narc_{events_id:04d}' - - def events(self, events_id): - data = self.source.read(f'files/fielddata/eventdata/zone_event_release/narc_{events_id:04d}.bin') - events = Events() - offset = 0 - (count,) = struct.unpack_from('