From bcc069c923f0ac82ab5280adb08442de46e10fc9 Mon Sep 17 00:00:00 2001 From: grunt-lucas Date: Sun, 17 Sep 2023 09:00:58 -0700 Subject: [PATCH] More firered impl --- include/types.h | 2 ++ src/cli_parser.cpp | 2 ++ src/importer.cpp | 23 +++++++++++++++-------- src/types.cpp | 36 +++++++++++++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/include/types.h b/include/types.h index b0f05f8f..67483b6b 100644 --- a/include/types.h +++ b/include/types.h @@ -142,11 +142,13 @@ enum class EncounterType { NONE, LAND, WATER }; std::uint8_t encounterTypeValue(EncounterType encounterType); std::string encounterTypeString(EncounterType encounterType); EncounterType stringToEncounterType(std::string string); +EncounterType encounterTypeFromInt(std::uint8_t encounterInt); enum class TerrainType { NORMAL, GRASS, WATER, WATERFALL }; std::uint8_t terrainTypeValue(TerrainType terrainType); std::string terrainTypeString(TerrainType terrainType); TerrainType stringToTerrainType(std::string string); +TerrainType terrainTypeFromInt(std::uint8_t terrainInt); enum class TargetBaseGame { EMERALD, FIRERED, RUBY }; std::string targetBaseGameString(TargetBaseGame game); diff --git a/src/cli_parser.cpp b/src/cli_parser.cpp index 5be86408..3387e430 100644 --- a/src/cli_parser.cpp +++ b/src/cli_parser.cpp @@ -346,6 +346,8 @@ static void parseDecompile(PtContext &ctx, int argc, char *const *argv) ctx.targetBaseGame = parseTargetBaseGame(ctx.err, TARGET_BASE_GAME, optarg); break; + // TODO : decompile should have fieldmap override for numTiles, since that affects the offsets in metatiles.bin + // Help message upon '-h/--help' goes to stdout case HELP_VAL: fmt::println("{}", DECOMPILE_HELP); diff --git a/src/importer.cpp b/src/importer.cpp index 78124a44..8b962790 100644 --- a/src/importer.cpp +++ b/src/importer.cpp @@ -795,13 +795,15 @@ importCompiledMetatileAttributes(PtContext &ctx, std::ifstream &metatileAttribut for (std::size_t metatileIndex = 0; metatileIndex < metatileCount; metatileIndex++) { Attributes attributes{}; if (ctx.targetBaseGame == TargetBaseGame::FIRERED) { - // std::uint32_t byte0 = attributesDataBuf.at((metatileIndex * 4)); - // std::uint32_t byte1 = attributesDataBuf.at((metatileIndex * 4) + 1); - // std::uint32_t byte2 = attributesDataBuf.at((metatileIndex * 4) + 2); - // std::uint32_t byte3 = attributesDataBuf.at((metatileIndex * 4) + 3); - // std::uint32_t attribute = (byte3 << 24) | (byte2 << 16) | (byte1 << 8) | byte0; - // TODO : implement FIRERED case - throw std::runtime_error{"TODO : implement FIRERED case"}; + std::uint32_t byte0 = attributesDataBuf.at((metatileIndex * 4)); + std::uint32_t byte1 = attributesDataBuf.at((metatileIndex * 4) + 1); + std::uint32_t byte2 = attributesDataBuf.at((metatileIndex * 4) + 2); + std::uint32_t byte3 = attributesDataBuf.at((metatileIndex * 4) + 3); + std::uint32_t attribute = (byte3 << 24) | (byte2 << 16) | (byte1 << 8) | byte0; + attributes.metatileBehavior = attribute & 0x000001FF; + attributes.terrainType = terrainTypeFromInt((attribute >> 9) & 0x0000001F); + attributes.encounterType = encounterTypeFromInt((attribute >> 24) & 0x00000007); + attributes.layerType = layerTypeFromInt((attribute >> 29) & 0x00000003); } else { std::uint16_t byte0 = attributesDataBuf.at((metatileIndex * 2)); @@ -1390,7 +1392,7 @@ TEST_CASE("importAttributesFromCsv should parse source CSVs as expected") } } -TEST_CASE("importCompiledTileset should import a triple layer pokeemerald tileset correctly") +TEST_CASE("importCompiledTileset should import a triple-layer pokeemerald tileset correctly") { porytiles::PtContext compileCtx{}; std::filesystem::path parentDir = porytiles::createTmpdir(); @@ -1456,3 +1458,8 @@ TEST_CASE("importCompiledTileset should import a triple layer pokeemerald tilese std::filesystem::remove_all(parentDir); } + +TEST_CASE("importCompiledTileset should import a dual-layer pokefirered tileset correctly") +{ + // TODO : test impl importCompiledTileset should import a dual-layer pokefirered tileset correctly +} diff --git a/src/types.cpp b/src/types.cpp index b5ea432c..44a67fa6 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -156,7 +156,7 @@ LayerType layerTypeFromInt(std::uint8_t layerInt) internalerror("types::layerTypeValue unknown LayerType int " + std::to_string(layerInt)); } // unreachable, here for compiler - throw std::runtime_error("types::layerTypeValue reached unreachable code path"); + throw std::runtime_error("types::layerTypeFromInt reached unreachable code path"); } std::uint8_t encounterTypeValue(EncounterType encounterType) @@ -205,6 +205,22 @@ EncounterType stringToEncounterType(std::string string) throw std::invalid_argument{"invalid EnounterType string"}; } +EncounterType encounterTypeFromInt(std::uint8_t encounterInt) +{ + switch (encounterInt) { + case 0: + return EncounterType::NONE; + case 1: + return EncounterType::LAND; + case 2: + return EncounterType::WATER; + default: + internalerror("types::encounterTypeFromInt unknown EncounterType int " + std::to_string(encounterInt)); + } + // unreachable, here for compiler + throw std::runtime_error("types::encounterTypeFromInt reached unreachable code path"); +} + std::uint8_t terrainTypeValue(TerrainType terrainType) { switch (terrainType) { @@ -259,6 +275,24 @@ TerrainType stringToTerrainType(std::string string) throw std::invalid_argument{"invalid TerrainType string"}; } +TerrainType terrainTypeFromInt(std::uint8_t terrainInt) +{ + switch (terrainInt) { + case 0: + return TerrainType::NORMAL; + case 1: + return TerrainType::GRASS; + case 2: + return TerrainType::WATER; + case 3: + return TerrainType::WATERFALL; + default: + internalerror("types::terrainTypeFromInt unknown TerrainType int " + std::to_string(terrainInt)); + } + // unreachable, here for compiler + throw std::runtime_error("types::terrainTypeFromInt reached unreachable code path"); +} + std::string targetBaseGameString(TargetBaseGame game) { switch (game) {