#include "sc2k/io/sc2_file.hpp" #include "sc2k/hydrology/edit.hpp" #include "sc2k/hydrology/legacy.hpp" #include "sc2k/hydrology/transform.hpp" #include "sc2k/render/terrain_material.hpp" #include "sc2k/render/asset_loader.hpp" #include "sc2k/render/city_renderer.hpp" #include "sc2k/render/rotate.hpp" #include "sc2k/structure/legacy_import.hpp" #include "sc2k/structure/transform.hpp" #include "sc2k/terrain/legacy_import.hpp" #include "sc2k/terrain/transform.hpp" #include "sc2k/ui/pick.hpp" #include #include #include #include #include #include #include "test_paths.hpp" using namespace sc2k; using namespace sc2k::render; namespace { void write_palette(const std::filesystem::path& path, int width, int height, Rgba base, int row_offset = 0) { Framebuffer image(width, height); for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) image.set(x, y, Rgba{ std::uint8_t(std::min(255, int(base.r) + x)), std::uint8_t(std::min(255, int(base.g) + (row_offset + y) / 8)), base.b, 0xFF, }); assert(image.write_bmp(path.string().c_str())); } struct HoleStats { long pixels = 0; long largest_component = 0; }; HoleStats enclosed_background(const Framebuffer& rendered) { const int width = rendered.width(); const int height = rendered.height(); const auto is_background = [&](int x, int y) { const Rgba pixel = rendered.at(x, y); return pixel.r == kBackground.r && pixel.g == kBackground.g && pixel.b == kBackground.b; }; std::vector exterior(std::size_t(width) * height, 0); std::vector queue; queue.reserve(std::size_t(width) * height / 2); const auto seed = [&](int x, int y) { const int index = y * width + x; if (!exterior[std::size_t(index)] && is_background(x, y)) { exterior[std::size_t(index)] = 1; queue.push_back(index); } }; for (int x = 0; x < width; ++x) { seed(x, 0); seed(x, height - 1); } for (int y = 0; y < height; ++y) { seed(0, y); seed(width - 1, y); } for (std::size_t next = 0; next < queue.size(); ++next) { const int index = queue[next]; const int x = index % width; const int y = index / width; if (x > 0) seed(x - 1, y); if (x + 1 < width) seed(x + 1, y); if (y > 0) seed(x, y - 1); if (y + 1 < height) seed(x, y + 1); } HoleStats result; std::vector counted = exterior; for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) { const std::size_t index = std::size_t(y) * width + x; if (!is_background(x, y) || counted[index]) continue; long component = 0; queue.clear(); queue.push_back(static_cast(index)); counted[index] = 1; for (std::size_t next = 0; next < queue.size(); ++next) { const int current = queue[next]; const int current_x = current % width; const int current_y = current / width; ++component; const auto append = [&](int adjacent_x, int adjacent_y) { const std::size_t adjacent = std::size_t(adjacent_y) * width + adjacent_x; if (!counted[adjacent] && is_background(adjacent_x, adjacent_y)) { counted[adjacent] = 1; queue.push_back(static_cast(adjacent)); } }; if (current_x > 0) append(current_x - 1, current_y); if (current_x + 1 < width) append(current_x + 1, current_y); if (current_y > 0) append(current_x, current_y - 1); if (current_y + 1 < height) append(current_x, current_y + 1); } result.pixels += component; result.largest_component = std::max(result.largest_component, component); } return result; } } // namespace int main() { namespace fs = std::filesystem; const fs::path root = fs::temp_directory_path() / "libreopolis-terrain-material-test"; fs::remove_all(root); fs::create_directories(root); write_palette(root / "surface-gradient.bmp", 1, 256, {80, 70, 45, 0xFF}); write_palette(root / "water-gradient.bmp", 1, 29, {25, 55, 105, 0xFF}); write_palette(root / "cutaway-water-gradient.bmp", 1, 256, {70, 55, 45, 0xFF}); write_palette(root / "cutaway-shallow-gradient.bmp", 1, 44, {70, 55, 45, 0xFF}, 256); write_palette(root / "cutaway-deep-gradient.bmp", 1, 212, {70, 55, 45, 0xFF}, 300); std::optional material = TerrainMaterial::load(root.string(), "test-material"); assert(material); assert(material->name() == "test-material"); fs::remove(root / "cutaway-deep-gradient.bmp"); assert(!TerrainMaterial::load(root.string(), "incomplete-material")); write_palette(root / "cutaway-deep-gradient.bmp", 1, 212, {70, 55, 45, 0xFF}, 300); const fs::path assets_root = root / "assets-root"; const fs::path named_pack = assets_root / "terrain" / "named" / "palettes"; fs::create_directories(named_pack); write_palette(named_pack / "surface-gradient.bmp", 1, 256, {80, 70, 45, 0xFF}); write_palette(named_pack / "water-gradient.bmp", 1, 29, {25, 55, 105, 0xFF}); write_palette(named_pack / "cutaway-water-gradient.bmp", 1, 256, {70, 55, 45, 0xFF}); write_palette(named_pack / "cutaway-shallow-gradient.bmp", 1, 44, {70, 55, 45, 0xFF}, 256); write_palette(named_pack / "cutaway-deep-gradient.bmp", 1, 212, {70, 55, 45, 0xFF}, 300); std::optional named_material = load_terrain_material(assets_root.string(), "named"); assert(named_material); assert(named_material->name() == "named"); TerrainRenderTile land; land.corner_heights = {4, 4, 4, 4}; land.base_height = 4; land.surface_height = 4; land.water_height = 2; const Sprite& flat = material->surface_sprite(land, {0, 0, 0, 0}, {0, 0, 0, 0}, 7, 11, 2); assert(flat.width == 32); assert(flat.height == 17); assert(flat.direct_colour()); assert(flat.clamp_edges); int flat_coverage = 0; for (const std::uint8_t covered : flat.mask) flat_coverage += covered != 0; assert(flat_coverage > 240 && flat_coverage < 300); // Native underground terrain retains the canonical surface geometry but owns a distinct earth // material and grid. It must not silently collapse to either the modern grass top or classic // compatibility sprites. The material generator lowers world height; relative sprite coverage // therefore stays identical for a flat tile. const Sprite& underground_floor = material->underground_surface_sprite(land, 7, 11, 2); const Sprite& underground_floor_without_grid = material->underground_surface_sprite( land, 7, 11, 2, false); assert(underground_floor.direct_colour()); assert(underground_floor.mask == flat.mask); assert(underground_floor.mask == underground_floor_without_grid.mask); const std::size_t underground_centre = std::size_t(8) * underground_floor.width + 16; assert(underground_floor.mask[underground_centre]); assert(underground_floor.rgba[underground_centre].r != flat.rgba[underground_centre].r || underground_floor.rgba[underground_centre].g != flat.rgba[underground_centre].g || underground_floor.rgba[underground_centre].b != flat.rgba[underground_centre].b); int underground_grid_difference = 0; for (std::size_t pixel = 0; pixel < underground_floor.rgba.size(); ++pixel) underground_grid_difference += underground_floor.rgba[pixel].r != underground_floor_without_grid.rgba[pixel].r || underground_floor.rgba[pixel].g != underground_floor_without_grid.rgba[pixel].g || underground_floor.rgba[pixel].b != underground_floor_without_grid.rgba[pixel].b; assert(underground_grid_difference > 20); // Water coverage is a material treatment on the lowered floor, not another flat overlay. Supplied // and unmet consumers retain identical terrain geometry while shifting the top toward blue/orange. const Sprite& supplied_underground_floor = material->underground_surface_sprite( land, 7, 11, 2, false, {}, UndergroundWaterCoverage::Supplied); const Sprite& needed_underground_floor = material->underground_surface_sprite( land, 7, 11, 2, false, {}, UndergroundWaterCoverage::Needed); const Sprite& neutral_underground_floor = material->underground_surface_sprite( land, 7, 11, 2, false, {}, UndergroundWaterCoverage::Neutral); assert(supplied_underground_floor.mask == underground_floor_without_grid.mask); assert(needed_underground_floor.mask == underground_floor_without_grid.mask); assert(neutral_underground_floor.mask == underground_floor_without_grid.mask); const Rgba supplied_centre = supplied_underground_floor.rgba[underground_centre]; const Rgba needed_centre = needed_underground_floor.rgba[underground_centre]; const Rgba neutral_centre = neutral_underground_floor.rgba[underground_centre]; assert(supplied_centre.b > supplied_centre.r); assert(needed_centre.r > needed_centre.b); assert(neutral_centre.b > neutral_centre.r); assert(supplied_centre.r != needed_centre.r || supplied_centre.g != needed_centre.g || supplied_centre.b != needed_centre.b); assert(neutral_centre.r != supplied_centre.r || neutral_centre.g != supplied_centre.g || neutral_centre.b != supplied_centre.b); // The skirt exclusively owns viewer-facing map-boundary walls. A raised front edge still needs // its ordinary side face on an interior tile, but that face must be suppressed at the boundary // or it paints a terrain-coloured strip over the underground cutaway. TerrainRenderTile underground_boundary_slope = land; underground_boundary_slope.corner_heights = {4, 5, 5, 4}; const Sprite& underground_interior_slope = material->underground_surface_sprite( underground_boundary_slope, 7, 11, 2, false); const Sprite& underground_boundary_slope_sprite = material->underground_surface_sprite( underground_boundary_slope, 7, 11, 2, false, SurfaceBoundaryOwnership{.row_cutaway = true}); assert(underground_interior_slope.width == underground_boundary_slope_sprite.width); assert(underground_interior_slope.height == underground_boundary_slope_sprite.height); int boundary_face_pixels_removed = 0; for (std::size_t pixel = 0; pixel < underground_interior_slope.mask.size(); ++pixel) { assert(!underground_boundary_slope_sprite.mask[pixel] || underground_interior_slope.mask[pixel]); boundary_face_pixels_removed += underground_interior_slope.mask[pixel] && !underground_boundary_slope_sprite.mask[pixel]; } assert(boundary_face_pixels_removed > 40); TerrainRenderTile wet_edge = land; wet_edge.water = true; wet_edge.water_height = 6; const Sprite& underground_skirt = material->underground_skirt_sprite( wet_edge, true, true, 2); assert(underground_skirt.direct_colour()); assert(underground_skirt.anchor_adjust == TerrainMaterial::kCutawayVisualDepth * 12); int underground_skirt_coverage = 0; for (const std::uint8_t covered : underground_skirt.mask) underground_skirt_coverage += covered != 0; assert(underground_skirt_coverage > 3000); // Procedural detail repeats over a bounded phase bank. Terrain sculpting changes thousands of // tiles at once, so an 8x8 bank multiplied every new altitude into enough distinct sprites and // GPU uploads to stall live dragging. A 4x4 bank retains spatial variation without that blow-up. std::unordered_set phase_sprites; for (int row = 0; row < 8; ++row) for (int column = 0; column < 8; ++column) phase_sprites.insert(&material->surface_sprite( land, {0, 0, 0, 0}, {0, 0, 0, 0}, row, column, 2)); assert(phase_sprites.size() == 16); // Dry ground uses the imported material-altitude domain, not its synthetic four-level water // reference. Higher terrain must therefore reach earlier (darker) rows of the surface palette. TerrainRenderTile low_land = land; low_land.corner_heights.fill(0); low_land.base_height = 0; low_land.surface_height = 0; const Sprite& low_surface = material->surface_sprite( low_land, {-4, -4, -4, -4}, {0, 0, 0, 0}, 7, 11, 2); TerrainRenderTile high_land = land; high_land.corner_heights.fill(31); high_land.base_height = 31; high_land.surface_height = 31; const Sprite& high_surface = material->surface_sprite( high_land, {27, 27, 27, 27}, {0, 0, 0, 0}, 7, 11, 2); const std::size_t centre_pixel = std::size_t(8) * low_surface.width + 16; assert(low_surface.mask[centre_pixel] && high_surface.mask[centre_pixel]); assert(low_surface.rgba[centre_pixel].g == high_surface.rgba[centre_pixel].g + 8); // Shared shoreline coverage is fractional. Moving from zero to one coverage unit must not switch // a grass vertex wholesale to the submerged end of the surface palette and fan that pale colour // across its complete raster triangle. const Sprite& zero_coverage_surface = material->surface_sprite( land, {4, 4, 4, 4}, {0, 0, 0, 0}, 5, 4, 2); const Sprite& trace_coverage_surface = material->surface_sprite( land, {4, 4, 4, 4}, {1, 1, 1, 1}, 5, 4, 2); assert(zero_coverage_surface.mask[centre_pixel] && trace_coverage_surface.mask[centre_pixel]); const Rgba zero_coverage_colour = zero_coverage_surface.rgba[centre_pixel]; const Rgba trace_coverage_colour = trace_coverage_surface.rgba[centre_pixel]; assert(std::abs(int(zero_coverage_colour.r) - int(trace_coverage_colour.r)) <= 1); assert(std::abs(int(zero_coverage_colour.g) - int(trace_coverage_colour.g)) <= 1); assert(std::abs(int(zero_coverage_colour.b) - int(trace_coverage_colour.b)) <= 1); // Continuously selecting the land palette row must not erase the separate beach material at the // actual shoreline contour. TerrainRenderTile boundary_material = land; boundary_material.surface_kind = TerrainSurfaceKind::Shore; const Sprite& sandy_boundary = material->surface_sprite( boundary_material, {4, 4, 4, 4}, {128, 128, 128, 128}, 5, 4, 2); assert(sandy_boundary.mask[centre_pixel]); assert(sandy_boundary.rgba[centre_pixel].r > sandy_boundary.rgba[centre_pixel].b); // A one-level front corner rises farther than the diamond half-height in screen space. The // procedural sprite must include the exposed grassy face that classic slope art baked in, // rather than leaving a transparent triangle below the folded top. TerrainRenderTile slope = land; slope.corner_heights = {4, 4, 5, 4}; const Sprite& filled_slope = material->surface_sprite( slope, {0, 0, 0, 0}, {0, 0, 0, 0}, 7, 11, 2); int slope_coverage = 0; for (const std::uint8_t covered : filled_slope.mask) slope_coverage += covered != 0; assert(slope_coverage > 240); assert(filled_slope.mask[std::size_t(15) * filled_slope.width + 16]); // A cardinal grade has two high corners and two low corners, so its centre lies at exactly half // a terrain level. Rounding that centre upward folds the planar top into four triangles and fills // pixels above its true back edge. TerrainRenderTile cardinal_slope = land; cardinal_slope.corner_heights = {4, 5, 5, 4}; const Sprite& planar_slope = material->surface_sprite( cardinal_slope, {0, 0, 0, 0}, {0, 0, 0, 0}, 7, 11, 2); assert(!planar_slope.mask[std::size_t(9) * planar_slope.width + 16]); assert(planar_slope.mask[std::size_t(13) * planar_slope.width + 16]); // Road-grade sides use one visual material even when imported edge semantics call the same // profile a quay. The override changes shading only: projected coverage remains identical. TerrainRenderTile quay_grade = cardinal_slope; quay_grade.row_face.present = true; quay_grade.row_face.negative_profile = {5, 5}; quay_grade.row_face.positive_profile = {4, 4}; quay_grade.row_face.material = terrain::FaceMaterial::Quay; const Sprite& quay_face = material->surface_sprite( quay_grade, {0, 0, 0, 0}, {0, 0, 0, 0}, 7, 11, 2); const Sprite& forced_rock_face = material->surface_sprite( quay_grade, {0, 0, 0, 0}, {0, 0, 0, 0}, 7, 11, 2, false, false, false, nullptr, false, {}, true); TerrainRenderTile natural_grade = quay_grade; natural_grade.row_face.material = terrain::FaceMaterial::NaturalRock; const Sprite& natural_rock_face = material->surface_sprite( natural_grade, {0, 0, 0, 0}, {0, 0, 0, 0}, 7, 11, 2, false, false, false, nullptr, true); assert(forced_rock_face.mask == quay_face.mask); assert(std::equal(forced_rock_face.rgba.begin(), forced_rock_face.rgba.end(), natural_rock_face.rgba.begin(), [](const Rgba& left, const Rgba& right) { return left.r == right.r && left.g == right.g && left.b == right.b && left.a == right.a; })); TerrainRenderTile shore; shore.corner_heights = {6, 5, 5, 6}; shore.base_height = 5; shore.surface_height = 5; shore.water_height = 5; shore.water = true; shore.surface_kind = TerrainSurfaceKind::Shore; const Sprite& blended = material->surface_sprite( shore, {5, 5, 5, 5}, {128, 255, 255, 128}, 7, 11, 2); assert(blended.width == 32); assert(blended.height == 29); bool saw_water = false; for (std::size_t i = 0; i < blended.mask.size(); ++i) { if (!blended.mask[i]) continue; const Rgba colour = blended.rgba[i]; saw_water |= colour.b > colour.r; } assert(saw_water); // Shore colour is a continuous field owned by the four shared corner samples. Wet/dry tile // identity must not pull the centre to an opposite extreme and kink the contour once per tile. TerrainRenderTile wet_contour; wet_contour.corner_heights.fill(4); wet_contour.base_height = 4; wet_contour.surface_height = 4; wet_contour.water_height = 4; wet_contour.water = true; wet_contour.surface_kind = TerrainSurfaceKind::Shore; TerrainRenderTile dry_contour = wet_contour; dry_contour.water = false; const std::array contour_coverage{32, 112, 224, 144}; const Sprite& wet_contour_surface = material->surface_sprite( wet_contour, {4, 4, 4, 4}, contour_coverage, 2, 3, 2); const Sprite& dry_contour_surface = material->surface_sprite( dry_contour, {4, 4, 4, 4}, contour_coverage, 2, 3, 2); assert(wet_contour_surface.mask == dry_contour_surface.mask); assert(std::equal(wet_contour_surface.rgba.begin(), wet_contour_surface.rgba.end(), dry_contour_surface.rgba.begin(), [](const Rgba& left, const Rgba& right) { return left.r == right.r && left.g == right.g && left.b == right.b && left.a == right.a; })); // One wet neighbour contributes 1/4 coverage to a shared vertex. If a tile receives that value // at all four corners, it remains wholly on the land side of the shoreline contour: the shore // material must not turn the complete diamond (or one of its centre triangles) into sand. TerrainRenderTile quarter_coverage_shore = wet_contour; quarter_coverage_shore.water = false; const std::array quarter_coverage{64, 64, 64, 64}; const Sprite& quarter_coverage_surface = material->surface_sprite( quarter_coverage_shore, {4, 4, 4, 4}, quarter_coverage, 6, 1, 2); TerrainRenderTile quarter_coverage_land = quarter_coverage_shore; quarter_coverage_land.surface_kind = TerrainSurfaceKind::Natural; const Sprite& unsanded_surface = material->surface_sprite( quarter_coverage_land, {4, 4, 4, 4}, quarter_coverage, 6, 1, 2); assert(quarter_coverage_surface.mask == unsanded_surface.mask); assert(std::equal(quarter_coverage_surface.rgba.begin(), quarter_coverage_surface.rgba.end(), unsanded_surface.rgba.begin(), [](const Rgba& left, const Rgba& right) { return left.r == right.r && left.g == right.g && left.b == right.b && left.a == right.a; })); // The top material is rasterized as four triangles, but shoreline coverage is a single // bilinear four-corner field. A non-planar corner set must therefore match an independently // evaluated uniform field at the same sample instead of revealing the centre-triangle fan. const std::array curved_coverage{50, 255, 50, 255}; const Sprite& curved_shore = material->surface_sprite( quarter_coverage_shore, {4, 4, 4, 4}, curved_coverage, 6, 1, 2); const Sprite& reference_shore = material->surface_sprite( quarter_coverage_shore, {4, 4, 4, 4}, {120, 120, 120, 120}, 6, 1, 2); constexpr int kBilinearSampleX = 16; constexpr int kBilinearSampleY = 3; const std::size_t bilinear_sample = std::size_t(kBilinearSampleY) * curved_shore.width + kBilinearSampleX; assert(curved_shore.mask[bilinear_sample] && reference_shore.mask[bilinear_sample]); const Rgba curved_colour = curved_shore.rgba[bilinear_sample]; const Rgba reference_colour = reference_shore.rgba[bilinear_sample]; assert(std::abs(int(curved_colour.r) - int(reference_colour.r)) <= 1); assert(std::abs(int(curved_colour.g) - int(reference_colour.g)) <= 1); assert(std::abs(int(curved_colour.b) - int(reference_colour.b)) <= 1); // Water depth selects one stable palette row. Sparse signed brightness runs provide the surface // texture without jumping independently through the complete gradient in rectangular cells. TerrainRenderTile flat_water; flat_water.corner_heights.fill(0); flat_water.base_height = 0; flat_water.surface_height = 4; flat_water.water_height = 4; flat_water.water = true; const Sprite& streaked_water = material->surface_sprite( flat_water, {4, 4, 4, 4}, {255, 255, 255, 255}, 0, 0, 2); std::unordered_set water_reds; int longest_horizontal_run = 0; long horizontal_change = 0; long vertical_change = 0; int horizontal_pairs = 0; int vertical_pairs = 0; for (int y = 0; y < 17; ++y) { int previous_red = -1; int run = 0; for (int x = 0; x < streaked_water.width; ++x) { const std::size_t index = std::size_t(y) * streaked_water.width + x; if (!streaked_water.mask[index]) continue; const int red = streaked_water.rgba[index].r; water_reds.insert(red); if (previous_red >= 0) { horizontal_change += std::abs(red - previous_red); ++horizontal_pairs; } if (red == previous_red) ++run; else { previous_red = red; run = 1; } longest_horizontal_run = std::max(longest_horizontal_run, run); if (y > 0 && streaked_water.mask[index - streaked_water.width]) { vertical_change += std::abs(red - int(streaked_water.rgba[index - streaked_water.width].r)); ++vertical_pairs; } } } assert(water_reds.size() >= 8); assert(*std::min_element(water_reds.begin(), water_reds.end()) >= 16); assert(*std::max_element(water_reds.begin(), water_reds.end()) <= 33); assert(longest_horizontal_run >= 2 && longest_horizontal_run <= 10); assert(horizontal_change * vertical_pairs < vertical_change * horizontal_pairs); // The skirt lies behind the separately rendered top surface. Its water edge must meet that // surface precisely rather than placing a 3x3 cap around every tile endpoint; similarly, water // must not extend sideways below its sloping bottom and notch the underlying earth. TerrainRenderTile flat_dry_edge = flat_water; flat_dry_edge.water = false; for (const int edge_zoom : {2, 3}) { const Sprite& flat_water_skirt = material->skirt_sprite(flat_water, true, false, edge_zoom); const Sprite& flat_dry_skirt = material->skirt_sprite(flat_dry_edge, true, false, edge_zoom); const int half_width = 4 << edge_zoom; const int half_height = 2 << edge_zoom; const int altitude_step = 3 << edge_zoom; const int water_top_y = half_height * 2; assert(flat_water_skirt.mask[std::size_t(water_top_y) * flat_water_skirt.width + half_width]); assert(!flat_water_skirt.mask[std::size_t(water_top_y - 1) * flat_water_skirt.width + half_width - 1]); const int water_bottom_y = flat_water.water_height * altitude_step + half_height * 2; const Rgba earth_beside_water = flat_water_skirt.rgba[std::size_t(water_bottom_y - 1) * flat_water_skirt.width + half_width - 1]; const Rgba unobscured_earth = flat_dry_skirt.rgba[std::size_t(half_height * 2 - 1) * flat_dry_skirt.width + half_width - 1]; assert(earth_beside_water.r == unobscured_earth.r); assert(earth_beside_water.g == unobscured_earth.g); assert(earth_beside_water.b == unobscured_earth.b); } // Deep imported water remains a flat sheet; its low bed only extends the bottom anchor. Water // side faces are emitted on shoreline edges, never between two wet tiles. TerrainRenderTile deep_water = shore; deep_water.corner_heights = {15, 15, 15, 14}; deep_water.base_height = 14; deep_water.surface_height = 18; deep_water.water_height = 18; const Sprite& deep_internal = material->surface_sprite( deep_water, {18, 18, 18, 18}, {255, 255, 255, 255}, 3, 5, 2); const Sprite& deep_boundary = material->surface_sprite( deep_water, {18, 18, 18, 18}, {255, 255, 255, 255}, 3, 5, 2, false, true, true); assert(deep_internal.anchor_adjust == 48); assert(deep_internal.height == 65); int internal_coverage = 0; int boundary_coverage = 0; for (const std::uint8_t covered : deep_internal.mask) internal_coverage += covered != 0; for (const std::uint8_t covered : deep_boundary.mask) boundary_coverage += covered != 0; assert(boundary_coverage > internal_coverage + 200); TerrainRenderTile dry_shore = shore; dry_shore.water = false; dry_shore.surface_height = 5; const Sprite& dry_blend = material->surface_sprite( dry_shore, {5, 5, 5, 5}, {192, 0, 0, 192}, 7, 11, 2); bool dry_saw_water = false; bool dry_saw_land = false; for (std::size_t i = 0; i < dry_blend.mask.size(); ++i) { if (!dry_blend.mask[i]) continue; const Rgba colour = dry_blend.rgba[i]; dry_saw_water |= colour.b > colour.r; dry_saw_land |= colour.r > colour.b; } assert(dry_saw_water); assert(dry_saw_land); // A dry tile may own a shoreline corner whose visible surface rises to the adjacent waterline. // Its exposed vertical water face follows geometry, not the tile-local wet bit. TerrainRenderTile dry_water_corner = land; dry_water_corner.corner_heights = {5, 5, 4, 4}; dry_water_corner.base_height = 4; dry_water_corner.surface_height = 4; dry_water_corner.water_height = 5; const Sprite& dry_corner_internal = material->surface_sprite( dry_water_corner, {5, 5, 5, 5}, {0, 0, 255, 255}, 1, 2, 2); const Sprite& dry_corner_exposed = material->surface_sprite( dry_water_corner, {5, 5, 5, 5}, {0, 0, 255, 255}, 1, 2, 2, false, true, true); int dry_internal_coverage = 0; int dry_exposed_coverage = 0; for (const std::uint8_t covered : dry_corner_internal.mask) dry_internal_coverage += covered != 0; for (const std::uint8_t covered : dry_corner_exposed.mask) dry_exposed_coverage += covered != 0; assert(dry_exposed_coverage > dry_internal_coverage); // Retained ramps/foundations use a presentation cap, but their walls descend to the repaired // canonical terrain. Explicit lower face corners must therefore add solid coverage. TerrainRenderTile cap = land; cap.corner_heights = {6, 6, 6, 6}; cap.base_height = 5; cap.surface_height = 5; const Sprite& cap_top = material->surface_sprite( cap, {0, 0, 0, 0}, {0, 0, 0, 0}, 4, 6, 2); const std::array cap_floor = {4, 4, 4, 4}; const Sprite& cap_with_walls = material->surface_sprite( cap, {0, 0, 0, 0}, {0, 0, 0, 0}, 4, 6, 2, false, false, false, &cap_floor); const Sprite& cap_with_rock_walls = material->surface_sprite( cap, {0, 0, 0, 0}, {0, 0, 0, 0}, 4, 6, 2, false, false, false, &cap_floor, true); const Sprite& boundary_cap_with_rock_walls = material->surface_sprite( cap, {0, 0, 0, 0}, {0, 0, 0, 0}, 4, 6, 2, false, false, false, &cap_floor, true, {true, false}); int cap_top_coverage = 0; int cap_wall_coverage = 0; for (const std::uint8_t covered : cap_top.mask) cap_top_coverage += covered != 0; for (const std::uint8_t covered : cap_with_walls.mask) cap_wall_coverage += covered != 0; assert(cap_wall_coverage > cap_top_coverage + 300); assert(cap_with_rock_walls.mask == cap_with_walls.mask); for (std::size_t pixel = 0; pixel < cap_with_rock_walls.mask.size(); ++pixel) assert(!cap_with_rock_walls.mask[pixel] || boundary_cap_with_rock_walls.mask[pixel]); bool rock_colour_differs = false; int rock_colour_changes = 0; for (std::size_t index = 0; index < cap_with_walls.rgba.size(); ++index) { const Rgba rock = cap_with_rock_walls.rgba[index]; const Rgba grass = cap_with_walls.rgba[index]; const bool differs = rock.r != grass.r || rock.g != grass.g || rock.b != grass.b; rock_colour_differs |= differs; rock_colour_changes += differs; } assert(rock_colour_differs); assert(rock_colour_changes > 200); TerrainRenderTile angled_cap = cap; angled_cap.corner_heights = {6, 7, 7, 6}; const Sprite& angled_grass = material->surface_sprite( angled_cap, {0, 0, 0, 0}, {0, 0, 0, 0}, 4, 6, 2, false, false, false, &cap_floor, false); const Sprite& angled_rock = material->surface_sprite( angled_cap, {0, 0, 0, 0}, {0, 0, 0, 0}, 4, 6, 2, false, false, false, &cap_floor, true); const auto is_rock_face = [&](int x, int y) { if (x < 0 || y < 0 || x >= angled_rock.width || y >= angled_rock.height) return false; const std::size_t index = std::size_t(y) * angled_rock.width + x; if (!angled_rock.mask[index] || !angled_grass.mask[index]) return false; const Rgba rock = angled_rock.rgba[index]; const Rgba grass = angled_grass.rgba[index]; return rock.r != grass.r || rock.g != grass.g || rock.b != grass.b; }; const auto packed_colour = [&](int x, int y) { const Rgba colour = angled_rock.rgba[std::size_t(y) * angled_rock.width + x]; return std::uint32_t(colour.r) << 16 | std::uint32_t(colour.g) << 8 | colour.b; }; std::unordered_set rock_colours; int along_face_changes = 0; int down_face_changes = 0; int longest_flat_run = 0; for (int y = 0; y < angled_rock.height; ++y) { int flat_run = 0; for (int x = 0; x < angled_rock.width; ++x) { if (!is_rock_face(x, y)) { flat_run = 0; continue; } rock_colours.insert(packed_colour(x, y)); if (is_rock_face(x - 1, y)) { if (packed_colour(x, y) != packed_colour(x - 1, y)) { ++along_face_changes; flat_run = 1; } else { ++flat_run; } longest_flat_run = std::max(longest_flat_run, flat_run); } if (is_rock_face(x, y - 1) && packed_colour(x, y) != packed_colour(x, y - 1)) ++down_face_changes; } } // Natural cliff material must have facets and fractures in both face directions. In particular, // it must not collapse back into long repeated horizontal masonry courses. assert(rock_colours.size() > 30); assert(along_face_changes > 100); assert(down_face_changes > 100); assert(longest_flat_run < 20); const Sprite& gridded = material->surface_sprite( land, {0, 0, 0, 0}, {0, 0, 0, 0}, 7, 11, 2, true); assert(&gridded != &flat); int grid_changes = 0; int brighter_grid_pixels = 0; for (std::size_t i = 0; i < flat.mask.size(); ++i) if (flat.mask[i] && (flat.rgba[i].r != gridded.rgba[i].r || flat.rgba[i].g != gridded.rgba[i].g || flat.rgba[i].b != gridded.rgba[i].b)) { ++grid_changes; brighter_grid_pixels += gridded.rgba[i].r > flat.rgba[i].r && gridded.rgba[i].g > flat.rgba[i].g && gridded.rgba[i].b > flat.rgba[i].b; } assert(grid_changes > 20); assert(brighter_grid_pixels == grid_changes); // The surface owns exactly the inside of its cutaway boundary. A square closure brush extends // one pixel onto the skirt and makes a repeating tooth at every boundary tile. const Sprite& boundary_surface = material->surface_sprite( land, {0, 0, 0, 0}, {0, 0, 0, 0}, 7, 11, 2, false, false, false, nullptr, false, {true, false}); assert(boundary_surface.mask[std::size_t(12) * boundary_surface.width + 24]); assert(!boundary_surface.mask[std::size_t(13) * boundary_surface.width + 24]); // Imported surface-water shape codes retain their classic topology as a mask, while the pixels // inside that mask come from the selected procedural material. std::optional shaped_material = TerrainMaterial::load(root.string(), "test-material"); assert(shaped_material); Sprite legacy_water_shape; legacy_water_shape.width = 32; legacy_water_shape.height = 17; legacy_water_shape.index.assign(32 * 17, 1); legacy_water_shape.mask.assign(32 * 17, 1); for (int y = 0; y < legacy_water_shape.height; ++y) for (int x = 0; x < legacy_water_shape.width / 2; ++x) legacy_water_shape.index[std::size_t(y) * legacy_water_shape.width + x] = 2; SpriteBank legacy_bank; legacy_bank.set(1275, legacy_water_shape); legacy_bank.set(1285, std::move(legacy_water_shape)); Palette legacy_palette; legacy_palette.colors[1] = {145, 120, 70, 0xFF}; legacy_palette.colors[2] = {30, 45, 220, 0xFF}; shaped_material->set_legacy_water_masks(legacy_bank, legacy_palette); TerrainRenderTile shaped_water = land; shaped_water.water = true; shaped_water.water_height = shaped_water.surface_height; shaped_water.surface_kind = TerrainSurfaceKind::LegacyWater; shaped_water.presentation_code = 0x40; shaped_water.legacy_water_topology = true; const Sprite& shaped_surface = shaped_material->surface_sprite( shaped_water, {4, 4, 4, 4}, {255, 255, 255, 255}, 0, 0, 2); bool shaped_saw_land = false; bool shaped_saw_water = false; for (std::size_t index = 0; index < shaped_surface.mask.size(); ++index) { if (!shaped_surface.mask[index]) continue; shaped_saw_land |= shaped_surface.rgba[index].r > shaped_surface.rgba[index].b; shaped_saw_water |= shaped_surface.rgba[index].b > shaped_surface.rgba[index].r; } assert(shaped_saw_land && shaped_saw_water); // Compatibility XTER codes are not shoreline authority for modern/editor water. Two otherwise // identical surfaces must therefore produce the same canonical neighbour-blended material even // when their legacy mirror carries different retiled masks. Imported classic water still takes // the explicitly provenance-gated mask path above. TerrainRenderTile modern_surface_water = shaped_water; modern_surface_water.legacy_water_topology = false; modern_surface_water.presentation_code = 0x40; const Sprite& modern_surface_a = shaped_material->surface_sprite( modern_surface_water, {4, 4, 4, 4}, {0, 96, 191, 255}, 0, 0, 2); modern_surface_water.presentation_code = 0x45; const Sprite& modern_surface_b = shaped_material->surface_sprite( modern_surface_water, {4, 4, 4, 4}, {0, 96, 191, 255}, 0, 0, 2); assert(std::equal(modern_surface_a.rgba.begin(), modern_surface_a.rgba.end(), modern_surface_b.rgba.begin(), [](const Rgba& left, const Rgba& right) { return left.r == right.r && left.g == right.g && left.b == right.b && left.a == right.a; })); // One modern editor-water tile must visibly own water at its centre even though each shared // corner sees only one wet tile out of four. A wet neighbour removes the fade at their shared // edge, so adjoining brush strokes form one continuous surface rather than pale seams. const Sprite& isolated_editor_water = shaped_material->surface_sprite( modern_surface_water, {4, 4, 4, 4}, {64, 64, 64, 64}, 0, 0, 2, false, false, false, nullptr, false, {}, false, 0x010); const Rgba isolated_centre = isolated_editor_water.rgba[std::size_t(isolated_editor_water.height / 2) * isolated_editor_water.width + isolated_editor_water.width / 2]; assert(isolated_centre.b > isolated_centre.r); const Sprite& joined_editor_water = shaped_material->surface_sprite( modern_surface_water, {4, 4, 4, 4}, {128, 128, 128, 128}, 0, 0, 2, false, false, false, nullptr, false, {}, false, 0x090); int isolated_blue = 0; int joined_blue = 0; for (const Rgba& pixel : isolated_editor_water.rgba) isolated_blue += pixel.b > pixel.r; for (const Rgba& pixel : joined_editor_water.rgba) joined_blue += pixel.b > pixel.r; assert(isolated_blue > 0); assert(joined_blue > isolated_blue); // An L bend must evaluate as one surface on both sides of its shared edge. For wet tiles A=(0,0), // B=(1,0), C=(0,1), C is cardinal to A but diagonal to B. Per-tile edge erosion therefore cut a // land wedge into B near their concave join; the tile-centred field remains blue there. const Sprite& bend_a = shaped_material->surface_sprite( modern_surface_water, {4, 4, 4, 4}, {64, 128, 191, 128}, 0, 0, 2, false, false, false, nullptr, false, {}, false, 0x0B0); const Sprite& bend_b = shaped_material->surface_sprite( modern_surface_water, {4, 4, 4, 4}, {128, 64, 64, 191}, 1, 0, 2, false, false, false, nullptr, false, {}, false, 0x016); const Rgba bend_a_join = bend_a.rgba[std::size_t(14) * bend_a.width + 18]; const Rgba bend_b_join = bend_b.rgba[std::size_t(7) * bend_b.width + 2]; assert(bend_a_join.b > bend_a_join.r); assert(bend_b_join.b > bend_b_join.r); // The dry owner across an editor-water boundary must evaluate the same tile-centred field. // Leaving it on vertex-average coverage makes its rasterized edge land-coloured while the wet // owner is water-coloured, producing pale wedges where painter ownership changes. TerrainRenderTile editor_water_bank = land; editor_water_bank.water_height = editor_water_bank.surface_height; editor_water_bank.surface_kind = TerrainSurfaceKind::Shore; const TerrainMaterial::SurfaceArtifacts& editor_water_bank_artifacts = shaped_material->surface_artifacts( editor_water_bank, {4, 4, 4, 4}, {64, 0, 0, 64}, 1, 0, 2, false, false, false, nullptr, false, {}, false, 0x002); const Sprite& editor_water_bank_surface = editor_water_bank_artifacts.surface; const Rgba bank_edge = editor_water_bank_surface.rgba[std::size_t(4) * editor_water_bank_surface.width + 8]; const Rgba bank_centre = editor_water_bank_surface.rgba[std::size_t(editor_water_bank_surface.height / 2) * editor_water_bank_surface.width + editor_water_bank_surface.width / 2]; assert(bank_edge.b > bank_centre.b); assert(editor_water_bank_artifacts.modern_water_hits[1].mask[std::size_t(4) * editor_water_bank_surface.width + 8]); // End-to-end picking must follow that same visible contour. On a native slope, the continuous // shoreline is partly rasterized by a neighbouring canonical dry tile; those pixels still own // the editor-water tile which the bulldozer can erase. { std::vector heights(129 * 129, 5 * terrain::kHeightQuantum); heights[std::size_t(40) * 129 + 40] += terrain::kHeightQuantum; const auto slope = terrain::Model::create({City::kDim, City::kDim}, std::move(heights)); assert(slope); City modern_city; modern_city.set_funds(1000); auto structures = structure::import_legacy_structures(modern_city, &*slope); auto hydrology = hydrology::build_components(slope->extent(), std::vector(City::kTiles), &*slope); assert(structures && hydrology); assert(hydrology::project_to_legacy(*hydrology, *slope, modern_city, &*structures).ok()); assert(hydrology::paint_surface_water( modern_city, *hydrology, *slope, &*structures, {40, 40})); const auto cache = TerrainRenderCache::create( *slope, modern_city, &*hydrology, &*structures, /*legacyWaterTopology=*/false); assert(cache); SpriteBank empty_bank; Palette palette; CityRenderer renderer(empty_bank, palette, &*shaped_material); const ScreenPos origin = origin_centering_tile(40, 40, 5, 2, 256, 192); const Camera camera{origin.x, origin.y}; Framebuffer rendered(512, 384); renderer.render(modern_city, rendered, camera, 0, 0, 2, {}, {}, {}, &*cache); int redirected_water_pixels = 0; int geometric_terrain_pixels = 0; int geometric_owner_mismatches = 0; for (int y = 0; y < rendered.height(); ++y) for (int x = 0; x < rendered.width(); ++x) { const ui::PickResult terrain_pick = ui::pick_tile( modern_city, camera, x, y, &*cache); if (!terrain_pick.hit || hydrology->wet({terrain_pick.row, terrain_pick.col})) continue; const CityRenderer::VisibleObjectHit visible = renderer.visible_object_at( x - camera.x + 0.5, y - camera.y + 0.5); geometric_terrain_pixels += visible.hit && visible.geometricTerrain && visible.ownerRow == terrain_pick.row && visible.ownerCol == terrain_pick.col; geometric_owner_mismatches += visible.hit && visible.geometricTerrain && (visible.ownerRow != terrain_pick.row || visible.ownerCol != terrain_pick.col); redirected_water_pixels += visible.hit && visible.ownerRow == 40 && visible.ownerCol == 40 && !visible.geometricTerrain; } assert(geometric_terrain_pixels > 0); assert(geometric_owner_mismatches > 0); assert(redirected_water_pixels > 0); } TerrainRenderTile masked_shore = shaped_water; masked_shore.surface_kind = TerrainSurfaceKind::Shore; masked_shore.presentation_code = 0x25; masked_shore.legacy_water_topology = true; const Sprite& masked_shore_surface = shaped_material->surface_sprite( masked_shore, {4, 4, 4, 4}, {255, 255, 255, 255}, 0, 0, 2); bool masked_shore_saw_land = false; bool masked_shore_saw_water = false; for (std::size_t index = 0; index < masked_shore_surface.mask.size(); ++index) { if (!masked_shore_surface.mask[index]) continue; masked_shore_saw_land |= masked_shore_surface.rgba[index].r > masked_shore_surface.rgba[index].b; masked_shore_saw_water |= masked_shore_surface.rgba[index].b > masked_shore_surface.rgba[index].r; } assert(masked_shore_saw_land && masked_shore_saw_water); TerrainRenderTile raised_bed_shore = masked_shore; raised_bed_shore.corner_heights = {5, 4, 4, 5}; raised_bed_shore.base_height = 4; raised_bed_shore.surface_height = 4; raised_bed_shore.water_height = 4; const Sprite& flat_legacy_shore = shaped_material->surface_sprite( raised_bed_shore, {4, 4, 4, 4}, {255, 255, 255, 255}, 0, 0, 2); assert(flat_legacy_shore.height == 17); TerrainRenderTile modern_shore = masked_shore; modern_shore.legacy_water_topology = false; const Sprite& modern_shore_surface = shaped_material->surface_sprite( modern_shore, {4, 4, 4, 4}, {255, 255, 255, 255}, 0, 0, 2); bool modern_shore_saw_land = false; for (std::size_t index = 0; index < modern_shore_surface.mask.size(); ++index) { if (!modern_shore_surface.mask[index]) continue; modern_shore_saw_land |= modern_shore_surface.rgba[index].r > modern_shore_surface.rgba[index].b; } assert(!modern_shore_saw_land); // The vertical water boundary is one semantic water-to-ground face. Legacy top-surface masks // must not subdivide it into renderer-invented notches. TerrainRenderTile masked_edge = masked_shore; masked_edge.corner_heights.fill(2); masked_edge.base_height = 2; masked_edge.surface_height = 4; masked_edge.water_height = 4; const Sprite& masked_skirt = shaped_material->skirt_sprite(masked_edge, true, false, 2); TerrainRenderTile semantic_edge = masked_edge; semantic_edge.legacy_water_topology = false; semantic_edge.presentation_code = 0; const Sprite& semantic_skirt = shaped_material->skirt_sprite(semantic_edge, true, false, 2); assert(&masked_skirt == &semantic_skirt); const Sprite& cliff = material->cliff_sprite(6, 3, 4, 2); assert(cliff.width == 32); assert(cliff.height == 53); assert(cliff.direct_colour()); assert(cliff.clamp_edges); int cliff_coverage = 0; for (const std::uint8_t covered : cliff.mask) cliff_coverage += covered != 0; assert(cliff_coverage > 500); // Texture phases repeat every eight tiles, bounding the procedural atlas while retaining // enough variation that neighbouring tiles do not repeat. const Sprite& repeated = material->surface_sprite(land, {0, 0, 0, 0}, {0, 0, 0, 0}, 15, 19, 2); assert(&repeated == &flat); TerrainRenderTile edge = shore; edge.corner_heights = {6, 5, 4, 5}; edge.water_height = 8; const Sprite& skirt = material->skirt_sprite(edge, true, true, 2); assert(skirt.width == 32); assert(skirt.height == 113 + TerrainMaterial::kCutawayVisualDepth * 12); assert(skirt.anchor_adjust == TerrainMaterial::kCutawayVisualDepth * 12); assert(skirt.direct_colour()); assert(skirt.clamp_edges); int skirt_coverage = 0; for (const std::uint8_t covered : skirt.mask) skirt_coverage += covered != 0; assert(skirt_coverage > 800); // The cutaway follows the authoritative boundary profile; flattening it to the tile base creates // block steps and vertical curtains where the top surface meets the side of the map. TerrainRenderTile flat_edge = edge; flat_edge.corner_heights.fill(edge.base_height); const Sprite& flat_skirt = material->skirt_sprite(flat_edge, true, true, 2); assert(!std::equal(skirt.rgba.begin(), skirt.rgba.end(), flat_skirt.rgba.begin(), [](const Rgba& left, const Rgba& right) { return left.r == right.r && left.g == right.g && left.b == right.b && left.a == right.a; })); // Cutaway material depth is a fixed imported-altitude projection, not a normalization that // compresses the complete palette into each map edge. Higher terrain therefore reaches farther // into the sediment palette at the common visual baseline. TerrainRenderTile low_column = edge; low_column.water = false; low_column.corner_heights.fill(6); TerrainRenderTile high_column = low_column; high_column.corner_heights.fill(14); const Sprite& low_skirt = material->skirt_sprite(low_column, true, false, 2); const Sprite& high_skirt = material->skirt_sprite(high_column, true, false, 2); const int low_bottom_y = low_skirt.height - 6; const int high_bottom_y = high_skirt.height - 6; const Rgba low_layer = low_skirt.rgba[std::size_t(low_bottom_y) * low_skirt.width + 24]; const Rgba high_layer = high_skirt.rgba[std::size_t(high_bottom_y) * high_skirt.width + 24]; assert(high_layer.g > low_layer.g); const Rgba shallow_layer = high_skirt.rgba[std::size_t(16) * high_skirt.width + 24]; const Rgba deep_layer = high_skirt.rgba[std::size_t(76) * high_skirt.width + 24]; const int layer_span = std::abs(int(shallow_layer.r) - int(deep_layer.r)) + std::abs(int(shallow_layer.g) - int(deep_layer.g)) + std::abs(int(shallow_layer.b) - int(deep_layer.b)); assert(layer_span > 0); // A sloped cutaway is not a planar texture quad: each screen column has a different top and // depth. Shade from those column endpoints so the two raster triangles cannot leave a diagonal // gradient crease that repeats once per boundary tile. TerrainRenderTile skewed_column = low_column; skewed_column.corner_heights = {6, 6, 7, 6}; const Sprite& skewed_skirt = material->skirt_sprite(skewed_column, true, false, 2); const Sprite& opposite_skirt = material->skirt_sprite(skewed_column, false, true, 2); // Adjacent skirt sprites overlap at these internal endpoint columns. Extend exactly one source // texel across each shared edge so fractional GPU filtering never blends the owner with alpha 0. assert(skewed_skirt.mask[std::size_t(100) * skewed_skirt.width + 15]); assert(!skewed_skirt.mask[std::size_t(100) * skewed_skirt.width + 14]); assert(opposite_skirt.mask[std::size_t(100) * opposite_skirt.width + 17]); assert(!opposite_skirt.mask[std::size_t(100) * opposite_skirt.width + 18]); constexpr float top_x0 = 32.0f, top_y0 = 20.0f; constexpr float top_x1 = 16.0f, top_y1 = 16.0f; constexpr float bottom_y0 = 308.0f, bottom_y1 = 316.0f; constexpr float top_row = 260.0f, bottom_row0 = 330.4f, bottom_row1 = 333.6f; int checked_skirt_pixels = 0; for (int y = 0; y < skewed_skirt.height; ++y) for (int x = 16; x < skewed_skirt.width; ++x) { const std::size_t index = std::size_t(y) * skewed_skirt.width + x; if (!skewed_skirt.mask[index]) continue; const float across = std::clamp((float(x) + 0.5f - top_x0) / (top_x1 - top_x0), 0.0f, 1.0f); const float top_y = top_y0 + (top_y1 - top_y0) * across; const float bottom_y = bottom_y0 + (bottom_y1 - bottom_y0) * across; const float depth = (float(y) + 0.5f - top_y) / (bottom_y - top_y); if (depth < 0.05f || depth > 0.95f) continue; const float bottom_row = bottom_row0 + (bottom_row1 - bottom_row0) * across; const float material_row = top_row + (bottom_row - top_row) * depth; const int row0 = int(material_row); const int row1 = row0 + 1; const float fraction = material_row - float(row0); const int green0 = std::min(255, 55 + row0 / 8); const int green1 = std::min(255, 55 + row1 / 8); const int neutral_green = int(std::lround(float(green0) + float(green1 - green0) * fraction)); const float light_strength = 10.0f - 5.0f * (material_row - 256.0f) / 255.0f; const int expected_red = int(std::lround(70.0f + light_strength)); const int expected_green = int(std::lround(float(neutral_green) + light_strength)); const int expected_blue = int(std::lround(45.0f + light_strength * 0.9f)); assert(skewed_skirt.rgba[index].r == expected_red); assert(skewed_skirt.rgba[index].g == expected_green); assert(skewed_skirt.rgba[index].b == expected_blue); ++checked_skirt_pixels; } assert(checked_skirt_pixels > 3000); // Water is a separate face above submerged ground. It must not restart the sediment material // below that ground, or the wet/dry boundary acquires a vertical strata seam. TerrainRenderTile dry_sediment = low_column; dry_sediment.water_height = 8; TerrainRenderTile wet_sediment = dry_sediment; wet_sediment.water = true; const Sprite& dry_sediment_skirt = material->skirt_sprite(dry_sediment, true, false, 2); const Sprite& wet_sediment_skirt = material->skirt_sprite(wet_sediment, true, false, 2); constexpr int kBelowGroundSampleHeight = 4; const int dry_sample_y = 6 * 12 + 12 - kBelowGroundSampleHeight * 12; const int wet_sample_y = 8 * 12 + 12 - kBelowGroundSampleHeight * 12; const Rgba dry_sediment_layer = dry_sediment_skirt.rgba[std::size_t(dry_sample_y) * dry_sediment_skirt.width + 24]; const Rgba wet_sediment_layer = wet_sediment_skirt.rgba[std::size_t(wet_sample_y) * wet_sediment_skirt.width + 24]; assert(dry_sediment_layer.r == wet_sediment_layer.r); assert(dry_sediment_layer.g == wet_sediment_layer.g); assert(dry_sediment_layer.b == wet_sediment_layer.b); // Real imported terrain is the adversarial case: repaired shared vertices coexist with retained // legacy grades and network supports. With every feature layer hidden, no background-coloured // component may remain enclosed inside the rendered map. const std::string city_path = first_present_city({"FOURCITI.SC2"}); if (!city_path.empty() && have_render_assets()) { City city; assert(io::load_sc2(city_path, city).ok); const auto imported = terrain::import_legacy_terrain(city); assert(imported.terrain); const auto structures = structure::import_legacy_structures(city, &*imported.terrain); assert(structures); const auto hydrology = hydrology::import_legacy_hydrology( city, &*imported.terrain, &*structures); assert(hydrology); const Assets assets = load_assets(assets_dir(), "grassland"); assert(assets.terrain_material); CityRenderer renderer(assets.bank, assets.palette, &*assets.terrain_material); ViewToggles terrain_only; terrain_only.transportation = false; terrain_only.powerLines = false; terrain_only.flora = false; terrain_only.zonedBuildings = false; terrain_only.otherBuildings = false; terrain_only.zones = false; terrain_only.signs = false; const auto render_holes = [&](const City& rendered_city, int rotation, const TerrainRenderCache* terrain_cache) { Framebuffer city_render(4200, 2600); renderer.render(rendered_city, city_render, {2100, 420}, rotation, 0, 2, terrain_only, {}, {}, terrain_cache); return enclosed_background(city_render); }; HoleStats maximum_holes; for (int rotation = 0; rotation < 4; ++rotation) { const City rotated = rotate_city(city, rotation); const auto rotated_terrain = terrain::rotate_clockwise(*imported.terrain, rotation); const auto rotated_structures = structure::rotate_clockwise(*structures, rotation); const auto rotated_hydrology = hydrology::rotate_clockwise(*hydrology, rotation); assert(rotated_terrain && rotated_structures && rotated_hydrology); const auto cache = TerrainRenderCache::create( *rotated_terrain, rotated, &*rotated_hydrology, &*rotated_structures, true); assert(cache); // Semantic opaque-geometry oracle: the cache emits exactly the owner-facing subset of // authoritative split edges. Back-facing positive-owner faces are occluded by their high // tile top in this rotation and must not become opaque wedges behind it. std::size_t cached_faces = 0; for (int row = 0; row < City::kDim; ++row) for (int column = 0; column < City::kDim; ++column) { const TerrainRenderTile* tile = cache->tile(row, column); cached_faces += tile->row_face.present; cached_faces += tile->column_face.present; } const std::size_t visible_faces = std::count_if(rotated_terrain->split_edges().begin(), rotated_terrain->split_edges().end(), [](const terrain::SplitEdge& edge) { return edge.owner == terrain::FaceOwner::Negative; }); assert(cached_faces == visible_faces); int ramp_count = 0; for (int row = 0; row < City::kDim; ++row) for (int column = 0; column < City::kDim; ++column) { if (rotated_structures->support({row, column}).kind != structure::SupportKind::Ramp) continue; ++ramp_count; TerrainRenderTile ramp = *cache->tile(row, column); const std::array floor = ramp.corner_heights; for (std::size_t corner = 0; corner < ramp.corner_heights.size(); ++corner) ramp.corner_heights[corner] = ramp.surface_height + ((ramp.raised_corner_mask & (1u << corner)) ? 1 : 0); const std::array water_heights = { ramp.corner_heights[0] - 4, ramp.corner_heights[1] - 4, ramp.corner_heights[2] - 4, ramp.corner_heights[3] - 4}; const Sprite& generated = assets.terrain_material->surface_sprite( ramp, water_heights, {0, 0, 0, 0}, row, column, 2, false, false, false, &floor, true); const Sprite* classic = assets.bank.get(terrain_sprite_id(ramp.presentation_code, false)); assert(classic); int missing = 0; for (int y = 0; y < classic->height; ++y) for (int x = 0; x < classic->width; ++x) { const std::size_t classic_index = std::size_t(y) * classic->width + x; if (!classic->mask[classic_index]) continue; const int screen_y = y - (classic->height - 17); const int generated_y = screen_y + generated.height - (17 + generated.anchor_adjust); if (x < 0 || x >= generated.width || generated_y < 0 || generated_y >= generated.height || !generated.mask[std::size_t(generated_y) * generated.width + x]) ++missing; } assert(missing == 0); } assert(ramp_count > 0); const HoleStats holes = render_holes(rotated, rotation, &*cache); maximum_holes.pixels = std::max(maximum_holes.pixels, holes.pixels); maximum_holes.largest_component = std::max( maximum_holes.largest_component, holes.largest_component); if (rotation == 0) { const TerrainRenderTile* reported = cache->tile(44, 92); assert(reported && !reported->column_face.present); const terrain::SplitEdge* semantic = rotated_terrain->split_edge( {terrain::EdgeAxis::Column, {44, 93}}); assert(semantic && semantic->owner == terrain::FaceOwner::Positive); assert((semantic->negative_profile == terrain::EdgeProfile{7 * terrain::kHeightQuantum, 6 * terrain::kHeightQuantum})); assert((semantic->positive_profile == terrain::EdgeProfile{8 * terrain::kHeightQuantum, 8 * terrain::kHeightQuantum})); } } std::printf("terrain material: explicit-face rotations enclosed-background max=%ld/%ld px/component\n", maximum_holes.pixels, maximum_holes.largest_component); assert(maximum_holes.largest_component <= 1); } fs::remove_all(root); std::puts("terrain material tests passed"); return 0; }