// tests/test_regional_terrain.cpp — canonical 3x3 generation, exact crops, seams, and version identity. #include "sc2k/terrain/region.hpp" #include #include #include #include #include #include "test_harness.hpp" namespace { using namespace sc2k::terrain; RegionGenerationSpec river_region(std::uint64_t seed) { RegionGenerationSpec spec; spec.cell.extent = {32, 32}; spec.cell.centre = CentreFeature::RiverNorthSouth; spec.cell.north = EdgeFeature::River; spec.cell.east = EdgeFeature::Land; spec.cell.south = EdgeFeature::Sea; spec.cell.west = EdgeFeature::Land; spec.cell.mountainousness = 42; spec.cell.wateriness = 58; spec.cell.trees = 47; spec.cell.seed = seed; return spec; } void hash_byte(std::uint64_t& hash, std::uint8_t value) { hash ^= value; hash *= 1099511628211ull; } void hash_i32(std::uint64_t& hash, std::int32_t value) { const std::uint32_t bits = static_cast(value); for (int shift = 0; shift < 32; shift += 8) hash_byte(hash, static_cast(bits >> shift)); } std::uint64_t region_hash(const RegionGenerationResult& region) { std::uint64_t hash = 1469598103934665603ull; for (int cell_row = 0; cell_row < kRegionCellRows; ++cell_row) { for (int cell_column = 0; cell_column < kRegionCellColumns; ++cell_column) { const RegionCell* cell = region.cell({cell_row, cell_column}); if (!cell) continue; hash_byte(hash, static_cast(cell_row)); hash_byte(hash, static_cast(cell_column)); for (const Height height : cell->terrain.heights()) hash_i32(hash, height); for (const WaterCell water : cell->water) { hash_byte(hash, static_cast(water.kind)); hash_i32(hash, water.surface_height); } for (const std::uint8_t trees : cell->trees) hash_byte(hash, trees); } } return hash; } void check_exact_crops(const RegionGenerationResult& region, const GenerationResult& expected, Extent cell_extent) { const Extent regional_extent = expected.terrain->extent(); const std::size_t regional_vertex_stride = static_cast(regional_extent.width) + 1; for (int cell_row = 0; cell_row < kRegionCellRows; ++cell_row) { for (int cell_column = 0; cell_column < kRegionCellColumns; ++cell_column) { const RegionCell* cell = region.cell({cell_row, cell_column}); CHECK(cell != nullptr); if (!cell) continue; CHECK(cell->coord == (RegionCellCoord{cell_row, cell_column})); CHECK(cell->terrain.extent() == cell_extent); CHECK(cell->terrain.validate().valid()); CHECK(cell->terrain.surface_patches().empty()); CHECK(cell->terrain.split_edges().empty()); for (int row = 0; row <= cell_extent.height; ++row) { for (int column = 0; column <= cell_extent.width; ++column) { const std::size_t regional_index = static_cast(cell_row * cell_extent.height + row) * regional_vertex_stride + cell_column * cell_extent.width + column; CHECK(cell->terrain.vertex({row, column}) == expected.terrain->heights()[regional_index]); } } for (int row = 0; row < cell_extent.height; ++row) { for (int column = 0; column < cell_extent.width; ++column) { const std::size_t regional_index = static_cast(cell_row * cell_extent.height + row) * regional_extent.width + cell_column * cell_extent.width + column; const std::size_t cell_index = static_cast(row) * cell_extent.width + column; CHECK(cell->water[cell_index] == expected.water[regional_index]); CHECK(cell->trees[cell_index] == expected.trees[regional_index]); } } } } } void check_exact_seams(const RegionGenerationResult& region, Extent extent) { std::size_t wet_crossings = 0; for (int cell_row = 0; cell_row < kRegionCellRows; ++cell_row) { for (int cell_column = 0; cell_column + 1 < kRegionCellColumns; ++cell_column) { const RegionCell& left = *region.cell({cell_row, cell_column}); const RegionCell& right = *region.cell({cell_row, cell_column + 1}); for (int row = 0; row <= extent.height; ++row) CHECK(left.terrain.vertex({row, extent.width}) == right.terrain.vertex({row, 0})); for (int row = 0; row < extent.height; ++row) { const WaterCell left_water = left.water[static_cast(row) * extent.width + extent.width - 1]; const WaterCell right_water = right.water[static_cast(row) * extent.width]; if (left_water && right_water) { ++wet_crossings; CHECK(left_water.surface_height == right_water.surface_height); } } } } for (int cell_row = 0; cell_row + 1 < kRegionCellRows; ++cell_row) { for (int cell_column = 0; cell_column < kRegionCellColumns; ++cell_column) { const RegionCell& upper = *region.cell({cell_row, cell_column}); const RegionCell& lower = *region.cell({cell_row + 1, cell_column}); for (int column = 0; column <= extent.width; ++column) CHECK(upper.terrain.vertex({extent.height, column}) == lower.terrain.vertex({0, column})); for (int column = 0; column < extent.width; ++column) { const WaterCell upper_water = upper.water[static_cast(extent.height - 1) * extent.width + column]; const WaterCell lower_water = lower.water[column]; if (upper_water && lower_water) { ++wet_crossings; CHECK(upper_water.surface_height == lower_water.surface_height); } } } } CHECK(wet_crossings > 0); } void check_centre_identity(const RegionGenerationResult& region, const RegionCell& expected) { const RegionCell* centre = region.cell(kPlayableRegionCell); CHECK(centre != nullptr); if (!centre) return; CHECK(std::equal(centre->terrain.heights().begin(), centre->terrain.heights().end(), expected.terrain.heights().begin(), expected.terrain.heights().end())); CHECK(centre->water == expected.water); CHECK(centre->trees == expected.trees); } void check_centre_water_continuation(const RegionGenerationResult& region, Extent extent) { const RegionCell& centre = *region.cell(kPlayableRegionCell); const RegionCell& north = *region.cell({0, 1}); const RegionCell& east = *region.cell({1, 2}); const RegionCell& south = *region.cell({2, 1}); const RegionCell& west = *region.cell({1, 0}); for (int offset = 0; offset < extent.width; ++offset) { CHECK(north.water[static_cast(extent.height - 1) * extent.width + offset] == centre.water[offset]); CHECK(south.water[offset] == centre.water[static_cast(extent.height - 1) * extent.width + offset]); } for (int offset = 0; offset < extent.height; ++offset) { CHECK(west.water[static_cast(offset) * extent.width + extent.width - 1] == centre.water[static_cast(offset) * extent.width]); CHECK(east.water[static_cast(offset) * extent.width] == centre.water[static_cast(offset) * extent.width + extent.width - 1]); } } void check_exterior_is_generated(const RegionGenerationResult& region) { std::set heights; std::size_t wet_tiles = 0; std::size_t tree_tiles = 0; for (int row = 0; row < kRegionCellRows; ++row) { for (int column = 0; column < kRegionCellColumns; ++column) { if (RegionCellCoord{row, column} == kPlayableRegionCell) continue; const RegionCell& cell = *region.cell({row, column}); CHECK(cell.terrain.validate().valid()); heights.insert(cell.terrain.heights().begin(), cell.terrain.heights().end()); wet_tiles += std::count_if(cell.water.begin(), cell.water.end(), [](WaterCell water) { return static_cast(water); }); for (int tile_row = 0; tile_row < cell.terrain.extent().height; ++tile_row) { for (int tile_column = 0; tile_column < cell.terrain.extent().width; ++tile_column) { const WaterCell water = cell.water[static_cast(tile_row) * cell.terrain.extent().width + tile_column]; if (!water) continue; const ShapeClassification ground = classify(*cell.terrain.corners({tile_row, tile_column})); CHECK(ground && ground.base_height < water.surface_height); } } tree_tiles += std::count_if(cell.trees.begin(), cell.trees.end(), [](std::uint8_t trees) { return trees != 0; }); } } CHECK(heights.size() >= 4); CHECK(wet_tiles > 0); CHECK(tree_tiles > 0); } void check_ocean_neighbour(const RegionGenerationResult& region, RegionCellCoord ocean_coord, RegionCellCoord first_coast, RegionCellCoord second_coast) { const RegionCell& ocean = *region.cell(ocean_coord); CHECK(std::all_of(ocean.water.begin(), ocean.water.end(), [](WaterCell water) { return water.kind == WaterKind::Salt; })); CHECK(std::none_of(ocean.trees.begin(), ocean.trees.end(), [](std::uint8_t trees) { return trees != 0; })); for (const RegionCellCoord coord : {first_coast, second_coast}) { const RegionCell& coast = *region.cell(coord); const std::size_t salt = std::count_if(coast.water.begin(), coast.water.end(), [](WaterCell water) { return water.kind == WaterKind::Salt; }); CHECK(salt > 0); CHECK(salt < coast.water.size()); } } } // namespace int main() { using namespace sc2k::terrain; RegionGenerationSpec unsupported = river_region(1); unsupported.generator_version = kCurrentRegionGeneratorVersion + 1; CHECK(generate_region(unsupported).error == RegionGenerationError::UnsupportedGeneratorVersion); unsupported.generator_version = 0; CHECK(generate_region(unsupported).error == RegionGenerationError::UnsupportedGeneratorVersion); RegionGenerationSpec too_small = river_region(1); too_small.cell.extent = {16, 16}; const RegionGenerationResult small_result = generate_region(too_small); CHECK(small_result.error == RegionGenerationError::InvalidSpec); CHECK(small_result.generation_error == GenerationError::InvalidExtent); RegionGenerationSpec too_large = river_region(1); too_large.cell.extent = {512, 512}; const RegionGenerationResult large_result = generate_region(too_large); CHECK(large_result.error == RegionGenerationError::InvalidSpec); CHECK(large_result.generation_error == GenerationError::InvalidExtent); const RegionGenerationSpec spec = river_region(0xA11CE5EEDull); const RegionGenerationResult region = generate_region(spec); CHECK(region.ok()); CHECK(region.cell(kPlayableRegionCell) != nullptr); CHECK(region.cell({-1, 0}) == nullptr); CHECK(region.cell({3, 0}) == nullptr); GenerationError expansion_error = GenerationError::None; const std::optional expanded = region_detail::expanded_spec(spec, expansion_error); CHECK(expanded.has_value()); CHECK(expansion_error == GenerationError::None); CHECK(expanded && expanded->extent == (Extent{96, 96})); const GenerationResult expected = expanded ? generate_terrain(*expanded) : GenerationResult{}; CHECK(expected.ok()); if (region.ok() && expected.ok()) { CHECK(!region_detail::crop_cell(expected, spec.cell.extent, {-1, 0})); GenerationResult malformed = expected; malformed.water.pop_back(); CHECK(!region_detail::crop_cell(malformed, spec.cell.extent, {0, 0})); check_exact_crops(region, expected, spec.cell.extent); check_exact_seams(region, spec.cell.extent); CHECK(region.regional_stats.accepted_seed == expected.stats.accepted_seed); } const RegionGenerationResult repeated = generate_region(spec); CHECK(repeated.ok()); constexpr std::uint64_t kCanonicalKnownAnswer = 0x1a29de9122f78ae0ull; CHECK(region_hash(region) == kCanonicalKnownAnswer); CHECK(region_hash(repeated) == kCanonicalKnownAnswer); RegionGenerationSpec old_canonical_spec = spec; old_canonical_spec.generator_version = 1; const RegionGenerationResult old_canonical = generate_region(old_canonical_spec); CHECK(old_canonical.ok()); CHECK(region_hash(old_canonical) == kCanonicalKnownAnswer); RegionGenerationSpec different_seed = spec; ++different_seed.cell.seed; const RegionGenerationResult changed = generate_region(different_seed); CHECK(changed.ok()); if (changed.ok()) CHECK(region_hash(region) != region_hash(changed)); // An authoritative existing centre is immutable, but version 2 derives genuine seeded terrain // outside it and carries every wet/dry edge sample into the immediately adjacent neighbour tile. const RegionCell* authoritative_centre = region.cell(kPlayableRegionCell); if (authoritative_centre) { RegionGenerationSpec constrained_spec = spec; constrained_spec.generator_version = 2; constrained_spec.cell.seed ^= 0xD1CEB00Cull; const RegionGenerationResult constrained = generate_region_around_center(constrained_spec, authoritative_centre->terrain, authoritative_centre->water, authoritative_centre->trees); const RegionGenerationResult constrained_repeat = generate_region_around_center(constrained_spec, authoritative_centre->terrain, authoritative_centre->water, authoritative_centre->trees); CHECK(constrained.ok()); CHECK(constrained_repeat.ok()); if (constrained.ok() && constrained_repeat.ok()) { check_centre_identity(constrained, *authoritative_centre); check_exact_seams(constrained, spec.cell.extent); check_centre_water_continuation(constrained, spec.cell.extent); check_exterior_is_generated(constrained); CHECK(region_hash(constrained) == region_hash(constrained_repeat)); constexpr std::uint64_t kConstrainedVersion2KnownAnswer = 0xf15518f2cb7d2e8aull; CHECK(region_hash(constrained) == kConstrainedVersion2KnownAnswer); CHECK(region_hash(constrained) != kCanonicalKnownAnswer); } RegionGenerationSpec changed_constrained_spec = constrained_spec; ++changed_constrained_spec.cell.seed; const RegionGenerationResult changed_constrained = generate_region_around_center( changed_constrained_spec, authoritative_centre->terrain, authoritative_centre->water, authoritative_centre->trees); CHECK(changed_constrained.ok()); if (constrained.ok() && changed_constrained.ok()) { check_centre_identity(changed_constrained, *authoritative_centre); CHECK(region_hash(changed_constrained) != region_hash(constrained)); } RegionGenerationSpec legacy_constrained_spec = constrained_spec; legacy_constrained_spec.generator_version = 1; const RegionGenerationResult legacy_constrained = generate_region_around_center( legacy_constrained_spec, authoritative_centre->terrain, authoritative_centre->water, authoritative_centre->trees); CHECK(legacy_constrained.ok()); if (legacy_constrained.ok()) { check_centre_identity(legacy_constrained, *authoritative_centre); CHECK(region_hash(legacy_constrained) != region_hash(constrained)); } for (std::uint64_t seed = 1; seed <= 8; ++seed) { RegionGenerationSpec adversarial_spec = constrained_spec; adversarial_spec.cell.seed = seed * 0x9E3779B97F4A7C15ull; const RegionGenerationResult adversarial = generate_region_around_center( adversarial_spec, authoritative_centre->terrain, authoritative_centre->water, authoritative_centre->trees); CHECK(adversarial.ok()); if (!adversarial.ok()) continue; check_centre_identity(adversarial, *authoritative_centre); check_centre_water_continuation(adversarial, spec.cell.extent); check_exterior_is_generated(adversarial); } } // Version 3 promotes an entirely salt-water centre edge to an ocean cell and lets the // neighbouring cells approach it through seeded coastlines instead of isolated water patches. constexpr Extent kOceanExtent{32, 32}; std::vector flat_heights(*vertex_count(kOceanExtent)); const std::optional flat_centre = Model::create(kOceanExtent, std::move(flat_heights)); std::vector ocean_centre_water( static_cast(kOceanExtent.width) * kOceanExtent.height); for (int row = 0; row < kOceanExtent.height; ++row) ocean_centre_water[static_cast(row) * kOceanExtent.width + kOceanExtent.width - 1] = {WaterKind::Salt, kDefaultGeneratedWaterSurface}; RegionGenerationSpec ocean_spec; ocean_spec.cell.extent = kOceanExtent; ocean_spec.cell.centre = CentreFeature::Plain; ocean_spec.cell.north = EdgeFeature::Land; ocean_spec.cell.east = EdgeFeature::Land; ocean_spec.cell.south = EdgeFeature::Land; ocean_spec.cell.west = EdgeFeature::Land; ocean_spec.cell.mountainousness = 30; ocean_spec.cell.wateriness = 0; ocean_spec.cell.trees = 35; ocean_spec.cell.seed = 0xC0A57A11ull; const RegionGenerationResult ocean_region = flat_centre ? generate_region_around_center(ocean_spec, *flat_centre, ocean_centre_water) : RegionGenerationResult{}; const RegionGenerationResult ocean_repeat = flat_centre ? generate_region_around_center(ocean_spec, *flat_centre, ocean_centre_water) : RegionGenerationResult{}; CHECK(ocean_region.ok()); CHECK(ocean_repeat.ok()); if (ocean_region.ok()) { constexpr std::uint64_t kOceanVersion3KnownAnswer = 0x1b4d11f03bfd127dull; CHECK(region_hash(ocean_region) == kOceanVersion3KnownAnswer); CHECK(region_hash(ocean_repeat) == kOceanVersion3KnownAnswer); check_ocean_neighbour(ocean_region, {1, 2}, {0, 2}, {2, 2}); check_centre_water_continuation(ocean_region, kOceanExtent); } RegionGenerationSpec old_ocean_spec = ocean_spec; old_ocean_spec.generator_version = 2; const RegionGenerationResult old_ocean_region = flat_centre ? generate_region_around_center(old_ocean_spec, *flat_centre, ocean_centre_water) : RegionGenerationResult{}; CHECK(old_ocean_region.ok()); if (old_ocean_region.ok()) { const RegionCell& old_ocean = *old_ocean_region.cell({1, 2}); CHECK(std::any_of(old_ocean.water.begin(), old_ocean.water.end(), [](WaterCell water) { return !water; })); } struct OceanOrientation { int slot; RegionCellCoord ocean; RegionCellCoord first_coast; RegionCellCoord second_coast; }; constexpr std::array kOceanOrientations = {{ {0, {2, 1}, {2, 0}, {2, 2}}, {1, {1, 2}, {0, 2}, {2, 2}}, {2, {0, 1}, {0, 0}, {0, 2}}, {3, {1, 0}, {0, 0}, {2, 0}}, }}; for (const OceanOrientation orientation : kOceanOrientations) { std::vector oriented_water( static_cast(kOceanExtent.width) * kOceanExtent.height); for (int offset = 0; offset < kOceanExtent.width; ++offset) { int row = 0; int column = 0; if (orientation.slot == 0) { row = kOceanExtent.height - 1; column = offset; } else if (orientation.slot == 1) { row = offset; column = kOceanExtent.width - 1; } else if (orientation.slot == 2) { column = offset; } else { row = offset; } oriented_water[static_cast(row) * kOceanExtent.width + column] = {WaterKind::Salt, kDefaultGeneratedWaterSurface}; } RegionGenerationSpec oriented_spec = ocean_spec; oriented_spec.cell.seed += static_cast(orientation.slot + 1); const RegionGenerationResult oriented = flat_centre ? generate_region_around_center(oriented_spec, *flat_centre, oriented_water) : RegionGenerationResult{}; CHECK(oriented.ok()); if (oriented.ok()) check_ocean_neighbour(oriented, orientation.ocean, orientation.first_coast, orientation.second_coast); } if (failures == 0) { std::printf("regional terrain: canonical crops and seeded constrained neighbours with welded seams\n"); return 0; } std::printf("regional terrain: %d failure(s)\n", failures); return 1; }