// tests/test_terrain_import.cpp — deterministic shadow import from classic tile terrain to the // canonical shared-vertex model. The live City remains untouched. #include "sc2k/terrain/legacy_import.hpp" #include "sc2k/io/sc2_file.hpp" #include "sc2k/render/terrain_render_cache.hpp" #include #include #include #include #include #include "test_harness.hpp" #include "test_paths.hpp" int main() { using namespace sc2k; using namespace sc2k::terrain; const auto same_heights = [](const Model& left, const Model& right) { return left.heights().size() == right.heights().size() && std::equal(left.heights().begin(), left.heights().end(), right.heights().begin()); }; // A clean flat city imports exactly, including all 129x129 boundary vertices. City flat; for (std::size_t i = 0; i < flat.altm.cells.size(); ++i) { flat.altm.cells[i] = 4; flat.xter.cells[i] = xter::kFlat; } LegacyImportResult flat_import = import_legacy_terrain(flat); CHECK(flat_import.terrain.has_value()); CHECK(flat_import.terrain->validate().valid()); CHECK(flat_import.terrain->heights().size() == 129 * 129); CHECK(flat_import.terrain->vertex({0, 0}) == 4 * kHeightQuantum); CHECK(flat_import.terrain->vertex({128, 128}) == 4 * kHeightQuantum); CHECK(flat_import.report.conflicting_vertices == 0); CHECK(flat_import.report.repair.changed_vertices == 0); // Contradictory adjacent tile shapes are observed and repaired, never admitted into Model. City damaged = flat; damaged.xter.at(20, 20) = static_cast(Shape::BottomHigh); damaged.altm.at(21, 21) = 7; damaged.xter.at(21, 21) = xter::kCube; damaged.xbld.at(21, 21) = xbld::kHighwaySlopeFirst; LegacyImportResult damaged_import = import_legacy_terrain(damaged); CHECK(damaged_import.terrain.has_value()); CHECK(damaged_import.terrain->validate().valid()); CHECK(damaged_import.report.conflicting_vertices > 0); CHECK(damaged_import.report.raised_cube_tiles == 1); CHECK(damaged_import.report.constructed_foundation_tiles == 1); CHECK(damaged_import.report.unclaimed_cube_tiles == 0); CHECK(damaged_import.report.maximum_claim_disagreement_levels >= 1); // The import is byte-deterministic and does not alter its source city. const City source_copy = damaged; LegacyImportResult damaged_again = import_legacy_terrain(damaged); CHECK(damaged_again.terrain.has_value()); CHECK(same_heights(*damaged_again.terrain, *damaged_import.terrain)); CHECK(damaged.xter.cells == source_copy.xter.cells); CHECK(damaged.altm.cells == source_copy.altm.cells); CHECK(damaged.xbld.cells == source_copy.xbld.cells); // Real cities are the actual acceptance gate: every available reference must become a valid // canonical model. Exact conflict counts are reported rather than baked into the architecture. constexpr std::array city_names = { "TOKYO.SC2", "BAYVIEW.SC2", "LONDON.SC2", "NYC.SC2", "PARIS.SC2", "BIGGESTC.SC2", }; int imported = 0; std::size_t conflicts = 0; std::size_t adjusted = 0; std::size_t comparable = 0; std::size_t matching = 0; std::size_t foundations = 0; std::size_t unclaimedCubes = 0; for (const char* name : city_names) { City city; if (!io::load_sc2(city_file(name), city).ok) continue; const auto xter_before = city.xter.cells; const auto altm_before = city.altm.cells; LegacyImportResult first = import_legacy_terrain(city); LegacyImportResult second = import_legacy_terrain(city); CHECK(first.terrain.has_value()); CHECK(second.terrain.has_value()); if (!first.terrain || !second.terrain) continue; CHECK(first.terrain->validate().valid()); CHECK(same_heights(*first.terrain, *second.terrain)); CHECK(!first.report.repair.used_flat_fallback); CHECK(city.xter.cells == xter_before); CHECK(city.altm.cells == altm_before); conflicts += first.report.conflicting_vertices; adjusted += first.report.repair.changed_vertices; comparable += first.report.comparable_shape_tiles; matching += first.report.matching_shape_tiles; foundations += first.report.constructed_foundation_tiles; unclaimedCubes += first.report.unclaimed_cube_tiles; ++imported; } if (imported > 0) { CHECK(conflicts > 0); // This catches corner-axis regressions: the wrong square-grid interpretation drops to ~91%, // while the city's two isometric lattice axes preserve over 98% across this reference set. CHECK(matching * 100 >= comparable * 97); std::printf("terrain import: %d cities, conflicts=%zu repaired-vertices=%zu shape-match=%zu/%zu foundations=%zu unclaimed-cubes=%zu\n", imported, conflicts, adjusted, matching, comparable, foundations, unclaimedCubes); } else { std::printf("terrain import: reference cities unavailable; synthetic checks only\n"); } // A visually audited classic city contains route grades that contradict neighbouring tile claims. // They migrate to authoritative surface patches and explicit edge-owned faces, never structures. { City city; if (io::load_sc2(city_file("FOURCITI.SC2"), city).ok) { LegacyImportResult importedCity = import_legacy_terrain(city); CHECK(importedCity.terrain.has_value()); if (importedCity.terrain) { const auto structures = structure::import_legacy_structures(city, &*importedCity.terrain); CHECK(structures.has_value()); if (structures) { CHECK(structures->support({26, 64}).kind == structure::SupportKind::Foundation); for (const terrain::TileCoord tile : {terrain::TileCoord{25, 64}, {27, 64}, {37, 69}, {47, 81}}) CHECK(!structures->support(tile)); for (const terrain::TileCoord tile : {terrain::TileCoord{97, 73}, {97, 65}, {127, 33}, {127, 45}}) { CHECK(structures->support(tile).kind == structure::SupportKind::Ramp); CHECK(structures->support(tile).elevation_offset == 1); } for (const terrain::TileCoord tile : {terrain::TileCoord{70, 21}, {76, 6}, {77, 8}, {109, 6}, {127, 10}}) { CHECK(structures->support(tile).kind == structure::SupportKind::Foundation); CHECK(structures->support(tile).elevation_offset == -1); } CHECK(structures->support({88, 78}).kind == structure::SupportKind::Foundation); CHECK(structures->support({88, 78}).elevation_offset == 0); const auto render_cache = render::TerrainRenderCache::create( *importedCity.terrain, city, nullptr, &*structures, true); CHECK(render_cache.has_value()); if (render_cache) { const render::TerrainRenderTile* edgeRamp = render_cache->tile(126, 75); CHECK(edgeRamp != nullptr); CHECK(structures->support({126, 75}).kind == structure::SupportKind::Ramp); if (edgeRamp) { CHECK(!edgeRamp->water); CHECK(edgeRamp->surface_kind == render::TerrainSurfaceKind::Natural); CHECK(xter::is_cardinal_slope(edgeRamp->presentation_code)); } const render::TerrainRenderTile* causeway = render_cache->tile(88, 78); CHECK(causeway != nullptr); if (causeway) { CHECK(causeway->surface_height == causeway->water_height); CHECK(!causeway->feature_lift); } } } const auto cube_top = importedCity.terrain->corners({44, 92}); const auto neighbour_top = importedCity.terrain->corners({44, 93}); const auto repaired_cube = importedCity.terrain->fabric_corners({44, 92}); const auto repaired_neighbour = importedCity.terrain->fabric_corners({44, 93}); std::printf("FOURCITI 44,92 xter=%02x xbld=%02x altm=%u top=%d,%d,%d,%d fabric=%d,%d,%d,%d patches=%zu splits=%zu\n", city.xter.at(44, 92), city.xbld.at(44, 92), city.altm.at(44, 92) & 0x1F, (*cube_top)[0] / kHeightQuantum, (*cube_top)[1] / kHeightQuantum, (*cube_top)[2] / kHeightQuantum, (*cube_top)[3] / kHeightQuantum, (*repaired_cube)[0] / kHeightQuantum, (*repaired_cube)[1] / kHeightQuantum, (*repaired_cube)[2] / kHeightQuantum, (*repaired_cube)[3] / kHeightQuantum, importedCity.terrain->surface_patches().size(), importedCity.terrain->split_edges().size()); CHECK(cube_top == repaired_cube); CHECK((neighbour_top == Corners{8 * kHeightQuantum, 8 * kHeightQuantum, 7 * kHeightQuantum, 7 * kHeightQuantum})); CHECK((repaired_cube == Corners{6 * kHeightQuantum, 6 * kHeightQuantum, 6 * kHeightQuantum, 7 * kHeightQuantum})); CHECK((repaired_neighbour == Corners{7 * kHeightQuantum, 6 * kHeightQuantum, 7 * kHeightQuantum, 7 * kHeightQuantum})); const SplitEdge* face = importedCity.terrain->split_edge( {EdgeAxis::Column, {44, 93}}); CHECK(face != nullptr); if (face) { CHECK((face->negative_profile == EdgeProfile{7 * kHeightQuantum, 6 * kHeightQuantum})); CHECK((face->positive_profile == EdgeProfile{8 * kHeightQuantum, 8 * kHeightQuantum})); CHECK(face->owner == FaceOwner::Positive); } } } } // Sweep every city and scenario in the active classic installation. This is intentionally data- // driven: new fixtures automatically join the validity gate without adding filename assertions. int corpus_imported = 0; std::size_t corpus_conflicts = 0; std::size_t corpus_adjusted = 0; std::size_t corpusFoundations = 0; std::size_t corpusUnclaimedCubes = 0; for (const std::string& directory : {cities_dir(), scenario_dir()}) { std::error_code error; if (!std::filesystem::is_directory(directory, error)) continue; for (const auto& entry : std::filesystem::directory_iterator(directory, error)) { if (error || !entry.is_regular_file()) continue; const std::string extension = entry.path().extension().string(); if (extension != ".SC2" && extension != ".SCN") continue; City city; if (!io::load_sc2(entry.path().string(), city).ok) continue; LegacyImportResult imported_city = import_legacy_terrain(city); CHECK(imported_city.terrain.has_value()); if (!imported_city.terrain) continue; CHECK(imported_city.terrain->validate().valid()); CHECK(!imported_city.report.repair.used_flat_fallback); CHECK(imported_city.report.split_edges == imported_city.terrain->split_edges().size()); CHECK(imported_city.report.retaining_wall_faces == 0); corpus_conflicts += imported_city.report.conflicting_vertices; corpus_adjusted += imported_city.report.repair.changed_vertices; corpusFoundations += imported_city.report.constructed_foundation_tiles; corpusUnclaimedCubes += imported_city.report.unclaimed_cube_tiles; ++corpus_imported; } } if (corpus_imported > 0) std::printf("terrain import corpus: %d maps, conflicts=%zu repaired-vertices=%zu foundations=%zu unclaimed-cubes=%zu\n", corpus_imported, corpus_conflicts, corpus_adjusted, corpusFoundations, corpusUnclaimedCubes); if (failures == 0) { std::printf("terrain import: all checks passed\n"); return 0; } std::printf("terrain import: %d failure(s)\n", failures); return 1; }