Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181// tests/test_terrain_model.cpp — canonical shared-vertex terrain shapes, validation, and immutable model.#include "sc2k/terrain/model.hpp"#include "sc2k/terrain/transform.hpp"
#include <array>#include <cstdint>#include <cstdio>#include <vector>
#include "test_harness.hpp"
int main() { using namespace sc2k::terrain;
// Exhaust the complete binary corner vocabulary. Masks 5 and 10 are saddles; an all-high mask // normalizes to a flat tile at the higher base. All other masks are the thirteen natural shapes. // clang-format off constexpr std::array<int, 16> expected_shapes = { 0, 9,10, 2,11,-1, 3, 6,12, 1,-1, 5, 4, 8, 7, 0, }; // clang-format on for (std::uint8_t mask = 0; mask < 16; ++mask) { Corners corners{}; for (unsigned corner = 0; corner < 4; ++corner) corners[corner] = (mask & (1u << corner)) ? kHeightQuantum : 0;
const ShapeClassification result = classify(corners); if (expected_shapes[mask] < 0) { CHECK(!result); CHECK(result.error == ShapeError::Saddle); } else { CHECK(result); CHECK(static_cast<int>(result.shape) == expected_shapes[mask]); if (mask != 15) CHECK(result.raised_corner_mask == mask); if (mask == 15) CHECK(result.base_height == kHeightQuantum); } }
// Shape masks are exact inverses, and four view rotations are identity. for (int code = 0; code <= 12; ++code) { const Shape shape = static_cast<Shape>(code); const std::uint8_t mask = raised_corner_mask(shape); CHECK(shape_from_raised_corner_mask(mask) == shape); CHECK(rotate_shape_clockwise(shape, 4) == shape); CHECK(rotate_shape_clockwise(rotate_shape_clockwise(shape, 1), 3) == shape); } CHECK(rotate_shape_clockwise(Shape::TopHigh, 1) == Shape::RightHigh); CHECK(rotate_shape_clockwise(Shape::TopLeftHigh, 2) == Shape::RightBottomHigh);
CHECK(classify({0, kHeightQuantum * 2, 0, 0}).error == ShapeError::TooSteep); CHECK(classify({0, 1, 0, 0}).error == ShapeError::HeightNotQuantized); CHECK(classify({-kHeightQuantum, 0, 0, -kHeightQuantum}).shape == Shape::RightBottomHigh);
// A 2x2-tile model with one raised centre vertex is valid: every tile shares that exact vertex // and independently derives the appropriate one-high corner shape. const Extent extent{2, 2}; std::vector<Height> centre_peak(9, 0); centre_peak[4] = kHeightQuantum; std::optional<Model> model = Model::create(extent, centre_peak); CHECK(model.has_value()); CHECK(model->validate().valid()); CHECK(model->extent() == extent); CHECK(model->vertex({1, 1}) == kHeightQuantum); CHECK(model->shape({0, 0}) == Shape::BottomHigh); CHECK(model->shape({0, 1}) == Shape::RightHigh); CHECK(model->shape({1, 0}) == Shape::LeftHigh); CHECK(model->shape({1, 1}) == Shape::TopHigh); CHECK(!model->vertex({-1, 0}).has_value()); CHECK(!model->vertex({3, 0}).has_value()); CHECK(!model->shape({2, 0}).has_value());
// Dynamic, non-square extents use a (width+1) by (height+1) row-major lattice. std::vector<Height> rectangle(12, -kHeightQuantum); std::optional<Model> rectangular_model = Model::create({3, 2}, rectangle); CHECK(rectangular_model.has_value()); CHECK(rectangular_model->heights().size() == 12); CHECK(rectangular_model->vertex({2, 3}) == -kHeightQuantum);
// Bad dimensions and incomplete lattices fail before any tile indexing is attempted. CHECK(!vertex_count({0, 2}).has_value()); CHECK(!vertex_count({2, -1}).has_value()); CHECK(validate({2, 2}, std::vector<Height>(8)).issues[0].code == ValidationCode::VertexCountMismatch); CHECK(!Model::create({2, 2}, std::vector<Height>(8)).has_value());
// A shared grid can still be a mathematically invalid candidate, but it cannot become a Model. std::vector<Height> saddle = { kHeightQuantum, 0, 0, kHeightQuantum, }; ValidationReport saddle_report = validate({1, 1}, saddle); CHECK(!saddle_report.valid()); CHECK(saddle_report.issue_count == 1); CHECK(saddle_report.issues[0].code == ValidationCode::SaddleTile); CHECK(!Model::create({1, 1}, saddle).has_value());
std::vector<Height> cliff = { 0, kHeightQuantum * 2, 0, 0, }; ValidationReport cliff_report = validate({1, 1}, cliff); CHECK(!cliff_report.valid()); CHECK(cliff_report.issues[0].code == ValidationCode::TileTooSteep);
std::vector<Height> unquantized(4, 0); unquantized[3] = 17; ValidationReport unquantized_report = validate({1, 1}, unquantized); CHECK(!unquantized_report.valid()); CHECK(unquantized_report.issues[0].code == ValidationCode::HeightNotQuantized); CHECK(!Model::create({1, 1}, unquantized).has_value());
// Sparse surface patches preserve the welded fabric as the default. Every unequal shared profile // requires exactly one canonical edge record, which owns one semantic connecting face. const std::vector<Height> flat_fabric(9, 0); const Corners level_two = {2 * kHeightQuantum, 2 * kHeightQuantum, 2 * kHeightQuantum, 2 * kHeightQuantum}; const std::vector<SurfacePatch> one_patch = {{{0, 0}, level_two}}; const std::vector<SplitEdge> turning_breakline = { {{EdgeAxis::Row, {1, 0}}, {2 * kHeightQuantum, 2 * kHeightQuantum}, {0, 0}, FaceMaterial::NaturalRock, FaceOwner::Negative}, {{EdgeAxis::Column, {0, 1}}, {2 * kHeightQuantum, 2 * kHeightQuantum}, {0, 0}, FaceMaterial::Earth, FaceOwner::Negative}, }; const auto split_model = Model::create({2, 2}, flat_fabric, one_patch, turning_breakline); CHECK(split_model.has_value()); CHECK(split_model->validate().valid()); CHECK(split_model->split_edges().size() == 2); CHECK(split_model->split_edge({EdgeAxis::Column, {0, 1}})->material == FaceMaterial::Earth); CHECK((split_model->corners({0, 0}) == level_two)); CHECK((split_model->fabric_corners({0, 0}) == Corners{0, 0, 0, 0}));
ValidationReport missing_face = validate({2, 2}, flat_fabric, one_patch, {}); CHECK(!missing_face.valid()); CHECK(std::any_of(missing_face.issues.begin(), missing_face.issues.end(), [](const ValidationIssue& issue) { return issue.code == ValidationCode::MissingSplitEdge; })); std::vector<SplitEdge> wrong_profile = turning_breakline; wrong_profile[0].positive_profile[0] = kHeightQuantum; ValidationReport mismatched_face = validate({2, 2}, flat_fabric, one_patch, wrong_profile); CHECK(!mismatched_face.valid()); CHECK(std::any_of(mismatched_face.issues.begin(), mismatched_face.issues.end(), [](const ValidationIssue& issue) { return issue.code == ValidationCode::SplitProfileMismatch; }));
// Three incident split edges form a T-junction at the centre vertex; the fourth edge remains // welded because its two profiles are equal. const Corners level_one = {kHeightQuantum, kHeightQuantum, kHeightQuantum, kHeightQuantum}; const std::vector<SurfacePatch> junction_patches = { {{0, 0}, level_two}, {{0, 1}, level_one}}; const std::vector<SplitEdge> junction_edges = { {{EdgeAxis::Row, {1, 0}}, {2 * kHeightQuantum, 2 * kHeightQuantum}, {0, 0}, FaceMaterial::RetainingWall, FaceOwner::Negative}, {{EdgeAxis::Row, {1, 1}}, {kHeightQuantum, kHeightQuantum}, {0, 0}, FaceMaterial::Quay, FaceOwner::Negative}, {{EdgeAxis::Column, {0, 1}}, {2 * kHeightQuantum, 2 * kHeightQuantum}, {kHeightQuantum, kHeightQuantum}, FaceMaterial::Earth, FaceOwner::Negative}, }; const auto junction = Model::create({2, 2}, flat_fabric, junction_patches, junction_edges); CHECK(junction.has_value()); CHECK(junction->split_edges().size() == 3);
const auto split_once = rotate_clockwise(*split_model, 1); CHECK(split_once.has_value()); CHECK(split_once->validate().valid()); CHECK(split_once->split_edge({EdgeAxis::Row, {1, 0}}) != nullptr); const auto split_four = rotate_clockwise(*split_model, 4); CHECK(split_four.has_value()); CHECK(std::equal(split_four->surface_patches().begin(), split_four->surface_patches().end(), split_model->surface_patches().begin())); CHECK(std::equal(split_four->split_edges().begin(), split_four->split_edges().end(), split_model->split_edges().begin()));
if (failures == 0) { std::printf("terrain model: 13 shapes, shared vertices, strict validation\n"); return 0; } std::printf("terrain model: %d failure(s)\n", failures); return 1;}