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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108// tests/test_terrain_repair.cpp — exact monotone slope closure, saddle removal, and deterministic// projection of arbitrary vertex targets into the canonical terrain domain.#include "sc2k/terrain/repair.hpp"
#include <algorithm>#include <cstdint>#include <cstdio>#include <vector>
#include "test_harness.hpp"
int main() { 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()); };
// An isolated three-level peak is repaired by lowering only its centre to the highest legal value. std::vector<Height> peak(9, 0); peak[4] = 3 * kHeightQuantum; RepairResult repaired_peak = repair({2, 2}, peak); CHECK(repaired_peak.terrain.has_value()); CHECK(repaired_peak.terrain->validate().valid()); CHECK(repaired_peak.stats.direction == RepairDirection::Lower); CHECK(repaired_peak.stats.changed_vertices == 1); CHECK(repaired_peak.terrain->vertex({1, 1}) == kHeightQuantum);
// The symmetric pit chooses the upward closure and likewise changes only its centre. std::vector<Height> pit(9, 0); pit[4] = -3 * kHeightQuantum; RepairResult repaired_pit = repair({2, 2}, pit); CHECK(repaired_pit.terrain.has_value()); CHECK(repaired_pit.terrain->validate().valid()); CHECK(repaired_pit.stats.direction == RepairDirection::Raise); CHECK(repaired_pit.stats.changed_vertices == 1); CHECK(repaired_pit.terrain->vertex({1, 1}) == -kHeightQuantum);
// The forbidden alternating saddle is resolved monotonically and cannot survive construction. std::vector<Height> saddle = { kHeightQuantum, 0, 0, kHeightQuantum, }; RepairResult repaired_saddle = repair({1, 1}, saddle); CHECK(repaired_saddle.terrain.has_value()); CHECK(repaired_saddle.terrain->validate().valid()); CHECK(repaired_saddle.stats.saddle_adjustments == 1); CHECK(repaired_saddle.stats.changed_vertices == 1);
// Candidate values are safely clamped and quantized before repair. std::vector<Height> fractional = {1, 127, 128, -129}; RepairResult repaired_fractional = repair({1, 1}, fractional); CHECK(repaired_fractional.terrain.has_value()); CHECK(repaired_fractional.terrain->validate().valid()); for (const Height height : repaired_fractional.terrain->heights()) CHECK(height_is_quantized(height));
CHECK(!repair({2, 2}, std::vector<Height>(8)).terrain.has_value()); CHECK(!repair({2, 2}, std::vector<Height>(9), std::vector<std::uint16_t>(8)).terrain.has_value());
// A hard ceiling is an invariant, not a preference: repairs may lower neighbouring vertices to // meet the slope bound but may never solve a contradiction by raising the constrained vertex. { std::vector<Height> targets(9, 5 * kHeightQuantum); targets[4] = 2 * kHeightQuantum; std::vector<Height> ceilings(9, kMaximumHeight); ceilings[4] = 3 * kHeightQuantum; RepairResult bounded = repair({2, 2}, targets, {}, ceilings); CHECK(bounded.terrain.has_value()); CHECK(bounded.terrain->vertex({1, 1}) <= 3 * kHeightQuantum); CHECK(bounded.terrain->validate().valid()); CHECK(!repair({2, 2}, targets, {}, std::vector<Height>(8)).terrain.has_value()); }
// Property sweep: hostile random targets of widely varying heights always produce valid, // deterministic terrain. This covers mixed cliffs and interacting saddle corrections. std::uint32_t random = 0xC0FFEEu; auto next_random = [&] { random = random * 1664525u + 1013904223u; return random; }; for (int trial = 0; trial < 100; ++trial) { std::vector<Height> targets(9 * 8); std::vector<std::uint16_t> weights(targets.size()); for (std::size_t i = 0; i < targets.size(); ++i) { targets[i] = (static_cast<int>(next_random() % 25) - 12) * kHeightQuantum; weights[i] = static_cast<std::uint16_t>(1 + next_random() % 16); } RepairResult first = repair({8, 7}, targets, weights); RepairResult second = repair({8, 7}, targets, weights); 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.stats.weighted_error == second.stats.weighted_error); CHECK(first.stats.direction == second.stats.direction); }
if (failures == 0) { std::printf("terrain repair: exact peak/pit closure, saddle removal, 100 random fields\n"); return 0; } std::printf("terrain repair: %d failure(s)\n", failures); return 1;}