Skip to content

Commit

Permalink
Better handling of default values
Browse files Browse the repository at this point in the history
  • Loading branch information
grunt-lucas committed Oct 12, 2023
1 parent 68a3b17 commit ee0d3e0
Showing 2 changed files with 74 additions and 36 deletions.
3 changes: 1 addition & 2 deletions src/driver.cpp
Original file line number Diff line number Diff line change
@@ -638,6 +638,7 @@ static void driveCompile(PtContext &ctx)
* supplied. If the user provided an integer, just use that. Otherwise, if the user provided a label string, check
* it against the behavior header here and replace that label string with the integral value.
*/
// FIXME : default behavior/encounter/terrain parsing code is duped
try {
parseInteger<std::uint16_t>(ctx.compilerConfig.defaultBehavior.c_str());
}
@@ -650,8 +651,6 @@ static void driveCompile(PtContext &ctx)
}
ctx.compilerConfig.defaultBehavior = std::to_string(behaviorMap.at(ctx.compilerConfig.defaultBehavior));
}

// Bring in the default encounter type and terrain type, if relevant
try {
parseInteger<std::uint16_t>(ctx.compilerConfig.defaultEncounterType.c_str());
}
107 changes: 73 additions & 34 deletions src/importer.cpp
Original file line number Diff line number Diff line change
@@ -155,47 +155,48 @@ DecompiledTileset importLayeredTilesFromPngs(PtContext &ctx,
std::size_t widthInMetatiles = bottom.get_width() / METATILE_SIDE_LENGTH;
std::size_t heightInMetatiles = bottom.get_height() / METATILE_SIDE_LENGTH;

// Grab the supplied default behavior, encounter/terrain types
std::uint16_t defaultBehavior;
EncounterType defaultEncounterType;
TerrainType defaultTerrainType;
try {
defaultBehavior = parseInteger<std::uint16_t>(ctx.compilerConfig.defaultBehavior.c_str());
}
catch (const std::exception &e) {
defaultBehavior = 0;
fatalerror(ctx.err, ctx.compilerSrcPaths, ctx.compilerConfig.mode,
fmt::format("supplied default behavior '{}' was not valid",
fmt::styled(ctx.compilerConfig.defaultBehavior, fmt::emphasis::bold)));
}
try {
std::uint8_t encounterValue = parseInteger<std::uint16_t>(ctx.compilerConfig.defaultEncounterType.c_str());
defaultEncounterType = encounterTypeFromInt(encounterValue);
}
catch (const std::exception &e) {
defaultEncounterType = EncounterType::NONE;
fatalerror(ctx.err, ctx.compilerSrcPaths, ctx.compilerConfig.mode,
fmt::format("supplied default EncounterType '{}' was not valid",
fmt::styled(ctx.compilerConfig.defaultEncounterType, fmt::emphasis::bold)));
}
try {
std::uint8_t terrainValue = parseInteger<std::uint16_t>(ctx.compilerConfig.defaultTerrainType.c_str());
defaultTerrainType = terrainTypeFromInt(terrainValue);
}
catch (const std::exception &e) {
defaultTerrainType = TerrainType::NORMAL;
fatalerror(ctx.err, ctx.compilerSrcPaths, ctx.compilerConfig.mode,
fmt::format("supplied default TerrainType '{}' was not valid",
fmt::styled(ctx.compilerConfig.defaultTerrainType, fmt::emphasis::bold)));
}

for (size_t metatileIndex = 0; metatileIndex < widthInMetatiles * heightInMetatiles; metatileIndex++) {
size_t metatileRow = metatileIndex / widthInMetatiles;
size_t metatileCol = metatileIndex % widthInMetatiles;
std::vector<RGBATile> bottomTiles{};
std::vector<RGBATile> middleTiles{};
std::vector<RGBATile> topTiles{};

// Grab the supplied default behavior, encounter/terrain types
// FIXME : default behavior/encounter/terrain parsing code is duped
std::uint16_t defaultBehavior;
EncounterType defaultEncounterType;
TerrainType defaultTerrainType;
try {
defaultBehavior = parseInteger<std::uint16_t>(ctx.compilerConfig.defaultBehavior.c_str());
}
catch (const std::exception &e) {
defaultBehavior = 0;
fatalerror(ctx.err, ctx.compilerSrcPaths, ctx.compilerConfig.mode,
fmt::format("supplied default behavior '{}' was not valid",
fmt::styled(ctx.compilerConfig.defaultBehavior, fmt::emphasis::bold)));
}
try {
std::uint8_t encounterValue = parseInteger<std::uint16_t>(ctx.compilerConfig.defaultEncounterType.c_str());
defaultEncounterType = encounterTypeFromInt(encounterValue);
}
catch (const std::exception &e) {
defaultEncounterType = EncounterType::NONE;
fatalerror(ctx.err, ctx.compilerSrcPaths, ctx.compilerConfig.mode,
fmt::format("supplied default EncounterType '{}' was not valid",
fmt::styled(ctx.compilerConfig.defaultEncounterType, fmt::emphasis::bold)));
}
try {
std::uint8_t terrainValue = parseInteger<std::uint16_t>(ctx.compilerConfig.defaultTerrainType.c_str());
defaultTerrainType = terrainTypeFromInt(terrainValue);
}
catch (const std::exception &e) {
defaultTerrainType = TerrainType::NORMAL;
fatalerror(ctx.err, ctx.compilerSrcPaths, ctx.compilerConfig.mode,
fmt::format("supplied default TerrainType '{}' was not valid",
fmt::styled(ctx.compilerConfig.defaultTerrainType, fmt::emphasis::bold)));
}

// Attributes are per-metatile so we can compute them once here
Attributes metatileAttributes{};
metatileAttributes.baseGame = ctx.targetBaseGame;
@@ -533,6 +534,41 @@ importAttributesFromCsv(PtContext &ctx, const std::unordered_map<std::string, st
warn_tooManyAttributesForTargetGame(ctx.err, filePath, ctx.targetBaseGame);
}

// Grab the supplied default behavior, encounter/terrain types
// FIXME : default behavior/encounter/terrain parsing code is duped
std::uint16_t defaultBehavior;
EncounterType defaultEncounterType;
TerrainType defaultTerrainType;
try {
defaultBehavior = parseInteger<std::uint16_t>(ctx.compilerConfig.defaultBehavior.c_str());
}
catch (const std::exception &e) {
defaultBehavior = 0;
fatalerror(ctx.err, ctx.compilerSrcPaths, ctx.compilerConfig.mode,
fmt::format("supplied default behavior '{}' was not valid",
fmt::styled(ctx.compilerConfig.defaultBehavior, fmt::emphasis::bold)));
}
try {
std::uint8_t encounterValue = parseInteger<std::uint16_t>(ctx.compilerConfig.defaultEncounterType.c_str());
defaultEncounterType = encounterTypeFromInt(encounterValue);
}
catch (const std::exception &e) {
defaultEncounterType = EncounterType::NONE;
fatalerror(ctx.err, ctx.compilerSrcPaths, ctx.compilerConfig.mode,
fmt::format("supplied default EncounterType '{}' was not valid",
fmt::styled(ctx.compilerConfig.defaultEncounterType, fmt::emphasis::bold)));
}
try {
std::uint8_t terrainValue = parseInteger<std::uint16_t>(ctx.compilerConfig.defaultTerrainType.c_str());
defaultTerrainType = terrainTypeFromInt(terrainValue);
}
catch (const std::exception &e) {
defaultTerrainType = TerrainType::NORMAL;
fatalerror(ctx.err, ctx.compilerSrcPaths, ctx.compilerConfig.mode,
fmt::format("supplied default TerrainType '{}' was not valid",
fmt::styled(ctx.compilerConfig.defaultTerrainType, fmt::emphasis::bold)));
}

// processedUpToLine starts at 1 since we processed the header already, which was on line 1
std::size_t processedUpToLine = 1;
while (true) {
@@ -553,6 +589,9 @@ importAttributesFromCsv(PtContext &ctx, const std::unordered_map<std::string, st

Attributes attribute{};
attribute.baseGame = ctx.targetBaseGame;
attribute.metatileBehavior = defaultBehavior;
attribute.encounterType = defaultEncounterType;
attribute.terrainType = defaultTerrainType;
if (behaviorMap.contains(behavior)) {
attribute.metatileBehavior = behaviorMap.at(behavior);
}

0 comments on commit ee0d3e0

Please sign in to comment.