Skip to content

Commit

Permalink
More firered impl
Browse files Browse the repository at this point in the history
  • Loading branch information
grunt-lucas committed Sep 17, 2023
1 parent e65d6b4 commit bcc069c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
2 changes: 2 additions & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/cli_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 15 additions & 8 deletions src/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
}
36 changes: 35 additions & 1 deletion src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit bcc069c

Please sign in to comment.