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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326#include "sc2k/render/terrain_render_cache.hpp"#include "sc2k/hydrology/legacy.hpp"#include "sc2k/terrain/legacy_import.hpp"#include "sc2k/terrain/legacy_projection.hpp"#include "sc2k/terrain/transform.hpp"#include "sc2k/ui/pick.hpp"
#include <cassert>#include <cstdint>#include <vector>
using namespace sc2k;
namespace {
terrain::Model flat_model(int levels = 3) { std::vector<terrain::Height> heights(129 * 129, levels * terrain::kHeightQuantum); auto model = terrain::Model::create({128, 128}, std::move(heights)); assert(model); return std::move(*model);}
} // namespace
int main() { City city; for (int row = 0; row < City::kDim; ++row) for (int column = 0; column < City::kDim; ++column) city.altm.at(row, column) = 3;
// Canonical geometry, not the compatibility bytes, determines the natural shape and base. std::vector<terrain::Height> heights(129 * 129, 3 * terrain::kHeightQuantum); heights[0] = 4 * terrain::kHeightQuantum; // top corner of tile 0,0 auto model = terrain::Model::create({128, 128}, heights); assert(model); auto cache = render::TerrainRenderCache::create(*model, city); assert(cache); assert(!cache->legacy_presentation()); assert(render::TerrainRenderView(city, &*cache).uses_modern_presentation()); assert(!render::TerrainRenderView(city).uses_modern_presentation()); const auto legacy_cache = render::TerrainRenderCache::create(*model, city, nullptr, nullptr, true); assert(legacy_cache && legacy_cache->legacy_presentation()); assert(!render::TerrainRenderView(city, &*legacy_cache).uses_modern_presentation()); const auto* tile = cache->tile(0, 0); assert(tile); assert(tile->shape == terrain::Shape::TopHigh); assert(tile->raised_corner_mask == 1); assert(tile->base_height == 3); assert(tile->surface_height == 3); assert(tile->corner_heights[0] == 4); assert(!tile->buildable);
// Modern shallow water is an explicit material mask over unchanged terrain. Its rendered water // height follows each shared terrain vertex instead of collapsing a sloped tile to one plane. City slopedWaterCity; std::vector<terrain::Height> slopedWaterHeights(129 * 129, 3 * terrain::kHeightQuantum); slopedWaterHeights[40 * 129 + 40] = 4 * terrain::kHeightQuantum; const auto slopedWaterTerrain = terrain::Model::create( {City::kDim, City::kDim}, std::move(slopedWaterHeights)); assert(slopedWaterTerrain); std::vector<hydrology::Sample> slopedWaterSamples(City::kTiles); slopedWaterSamples[40 * City::kDim + 40] = { hydrology::BodyKind::SurfaceWater, 3 * terrain::kHeightQuantum, hydrology::FlowDirection::None, true}; const auto slopedWaterHydrology = hydrology::build_components( slopedWaterTerrain->extent(), slopedWaterSamples, &*slopedWaterTerrain); assert(slopedWaterHydrology); const auto slopedWaterCache = render::TerrainRenderCache::create( *slopedWaterTerrain, slopedWaterCity, &*slopedWaterHydrology); assert(slopedWaterCache); const render::TerrainRenderView slopedWaterView(slopedWaterCity, &*slopedWaterCache); const render::TerrainRenderTile* slopedWaterTile = slopedWaterCache->tile(40, 40); assert(slopedWaterTile && slopedWaterTile->water_follows_terrain); assert(slopedWaterView.water_height_at_vertex(40, 40) == 4); assert(slopedWaterView.water_height_at_vertex(41, 40) == 3);
const auto classicSlopedWaterCache = render::TerrainRenderCache::create( *slopedWaterTerrain, slopedWaterCity, &*slopedWaterHydrology, nullptr, true); assert(classicSlopedWaterCache); const render::TerrainRenderView classicSlopedWaterView( slopedWaterCity, &*classicSlopedWaterCache); assert(!classicSlopedWaterCache->tile(40, 40)->water_follows_terrain); assert(classicSlopedWaterView.water_height_at_vertex(40, 40) == 3);
render::ScreenPos legacy_quad[4], canonical_quad[4]; const render::Camera camera{100, 100}; ui::surface_quad(city, camera, 0, 0, legacy_quad); ui::surface_quad(city, camera, 0, 0, canonical_quad, 2, true, 1.f, &*cache); assert(canonical_quad[0].y == legacy_quad[0].y - 12); assert(canonical_quad[1].y == legacy_quad[1].y);
// Water remains a presentation layer for now, but its height does not replace canonical ground. city.xbit.at(2, 3) |= xbit::kWater; city.altm.at(2, 3) = static_cast<std::uint16_t>(3 | (4 << 5)); cache = render::TerrainRenderCache::create(flat_model(), city); assert(cache); tile = cache->tile(2, 3); assert(tile->surface_kind == render::TerrainSurfaceKind::Shore); assert(tile->presentation_code == xter::kShore); assert(tile->base_height == 3); assert(tile->surface_height == 4); assert(!tile->buildable);
// An imported shoreline keeps its saved sprite presentation while canonical geometry is repaired // underneath it. This includes submerged orphan cubes recovered as water by hydrology. city.xter.at(2, 3) = static_cast<std::uint8_t>(xter::kSubmerged + static_cast<std::uint8_t>(terrain::Shape::RightHigh)); cache = render::TerrainRenderCache::create(flat_model(), city); assert(cache); tile = cache->tile(2, 3); assert(tile->presentation_code == city.xter.at(2, 3)); assert(tile->surface_kind == render::TerrainSurfaceKind::Submerged); city.xter.at(2, 3) = xter::kFlat;
// A legacy raised cube under a network is represented as a constructed support, not a natural // terrain shape. This compatibility marker disappears when the structure layer lands. city.xter.at(8, 9) = xter::kCube; city.xbld.at(8, 9) = xbld::kRoadFirst; cache = render::TerrainRenderCache::create(flat_model(), city); assert(cache); tile = cache->tile(8, 9); assert(tile->shape == terrain::Shape::Flat); assert(tile->constructed_support); assert(tile->feature_lift); assert(tile->presentation_code == xter::kCube);
// A foundation retains its saved anchor even when canonical repair moved the shared ground beneath // it. The feature receives the separate one-level cube-top lift at draw time. City offsetFoundation; offsetFoundation.xter.at(8, 9) = xter::kCube; offsetFoundation.xbld.at(8, 9) = xbld::kRoadFirst; offsetFoundation.altm.at(8, 9) = 2; const terrain::Model repairedFoundationGround = flat_model(3); const auto offsetFoundationStructures = structure::import_legacy_structures( offsetFoundation, &repairedFoundationGround); assert(offsetFoundationStructures); assert(offsetFoundationStructures->support({8, 9}).elevation_offset == -1); cache = render::TerrainRenderCache::create(repairedFoundationGround, offsetFoundation, nullptr, &*offsetFoundationStructures); assert(cache); tile = cache->tile(8, 9); assert(tile->surface_height == 2); assert(tile->feature_lift);
// A submerged cube is anchored at the saved water presentation, not at its repaired bed. City submergedFoundation; submergedFoundation.xter.at(8, 9) = static_cast<std::uint8_t>(xter::kSubmerged + xter::kCube); submergedFoundation.xbld.at(8, 9) = xbld::kHighwayTB; submergedFoundation.xbit.at(8, 9) |= xbit::kWater; submergedFoundation.altm.at(8, 9) = static_cast<std::uint16_t>(2 | (4 << 5)); const terrain::Model submergedGround = flat_model(2); const auto submergedStructures = structure::import_legacy_structures( submergedFoundation, &submergedGround); assert(submergedStructures); cache = render::TerrainRenderCache::create(submergedGround, submergedFoundation, nullptr, &*submergedStructures); assert(cache); tile = cache->tile(8, 9); assert(tile->surface_height == 4); assert(!tile->feature_lift); const render::TerrainRenderTile uncached_submerged = render::TerrainRenderView(submergedFoundation).tile(8, 9); assert(uncached_submerged.constructed_support); assert(!uncached_submerged.feature_lift);
// Legacy rendering recovers an otherwise empty submerged cube whose water bit was lost, but // does not spread that inference onto an adjacent dry network approach. City legacyPresentation; legacyPresentation.xter.at(30, 30) = static_cast<std::uint8_t>(xter::kSubmerged + xter::kCube); legacyPresentation.altm.at(30, 30) = static_cast<std::uint16_t>(2 | (4 << 5)); const render::TerrainRenderTile recoveredWater = render::TerrainRenderView(legacyPresentation).tile(30, 30); assert(recoveredWater.water); assert(recoveredWater.surface_height == 4); assert(recoveredWater.surface_kind == render::TerrainSurfaceKind::Submerged);
// The 0x30..0x45 bank selects irregular surface-water artwork. Its low nibble is not a slope // shape: decoding 0x35 as shape 5 creates the raised blue prisms seen around Four Cities 75,90. City surfaceWater; const render::TerrainRenderView surfaceWaterView(surfaceWater); for (int code = xter::kSurfaceWaterFirst; code <= xter::kSurfaceWaterLast; ++code) { surfaceWater.xter.at(32, code) = static_cast<std::uint8_t>(code); surfaceWater.xbit.at(32, code) = xbit::kWater; surfaceWater.altm.at(32, code) = static_cast<std::uint16_t>(4 | (4 << 5)); const render::TerrainRenderTile waterTile = surfaceWaterView.tile(32, code); assert(waterTile.shape == terrain::Shape::Flat); assert(waterTile.maximum_grade_steps == 0); assert((waterTile.corner_heights == std::array<int, 4>{4, 4, 4, 4})); assert(waterTile.surface_height == 4); assert(waterTile.surface_kind == render::TerrainSurfaceKind::LegacyWater); assert(waterTile.legacy_water_topology); }
// A canonical cache for a classic import must retain the saved shoreline-mask provenance. // Otherwise the material path combines that mask with modern neighbour blending and water // climbs dry slopes in triangular lips. Modern generated water deliberately has no such flag. City cachedShore; cachedShore.xter.at(32, 32) = xter::kShore; cachedShore.xbit.at(32, 32) = xbit::kWater; cachedShore.altm.at(32, 32) = static_cast<std::uint16_t>(3 | (4 << 5)); const terrain::Model cachedShoreTerrain = flat_model(3); const auto cachedShoreHydrology = hydrology::import_legacy_hydrology( cachedShore, &cachedShoreTerrain); assert(cachedShoreHydrology); const auto classicShoreCache = render::TerrainRenderCache::create( cachedShoreTerrain, cachedShore, &*cachedShoreHydrology, nullptr, true); assert(classicShoreCache); assert(classicShoreCache->tile(32, 32)->legacy_water_topology); const auto modernShoreCache = render::TerrainRenderCache::create( cachedShoreTerrain, cachedShore, &*cachedShoreHydrology); assert(modernShoreCache); assert(!modernShoreCache->tile(32, 32)->legacy_water_topology);
legacyPresentation.xter.at(40, 40) = xter::kSlopeCardLast; legacyPresentation.xbld.at(40, 40) = xbld::kPowerFirst + 5; legacyPresentation.altm.at(40, 40) = static_cast<std::uint16_t>(4 | (4 << 5)); legacyPresentation.xbld.at(40, 41) = xbld::kRaisedPower; legacyPresentation.xbit.at(40, 41) = xbit::kWater; const render::TerrainRenderTile dryRamp = render::TerrainRenderView(legacyPresentation).tile(40, 40); assert(dryRamp.constructed_support); assert(!dryRamp.feature_lift); assert(!dryRamp.water); assert(dryRamp.surface_height == 4); assert(dryRamp.surface_kind == render::TerrainSurfaceKind::Natural);
std::vector<structure::Support> rampSupports(City::kTiles); rampSupports[12 * City::kDim + 13] = { structure::SupportKind::Ramp, xter::kSlopeCardFirst, 1, true}; const auto rampStructures = structure::Model::create({City::kDim, City::kDim}, rampSupports); assert(rampStructures); cache = render::TerrainRenderCache::create(flat_model(), city, nullptr, &*rampStructures); assert(cache); tile = cache->tile(12, 13); assert(tile->constructed_support); assert(!tile->feature_lift); assert(tile->base_height == 3); assert(tile->surface_height == 4); assert(tile->presentation_code == xter::kSlopeCardFirst); assert(tile->shape == static_cast<terrain::Shape>(xter::kSlopeCardFirst));
// Hydrology remains authoritative underneath a bridge approach, while the structural support // explicitly owns the visible cap and prevents the renderer from colouring it as water. const terrain::Model rampTerrain = flat_model(); std::vector<hydrology::Sample> rampWaterSamples(City::kTiles); rampWaterSamples[12 * City::kDim + 13] = {hydrology::BodyKind::SurfaceWater, 4 * terrain::kHeightQuantum, hydrology::FlowDirection::None, true}; const auto rampWater = hydrology::build_components( rampTerrain.extent(), rampWaterSamples, &rampTerrain); assert(rampWater && rampWater->wet({12, 13})); cache = render::TerrainRenderCache::create( rampTerrain, city, &*rampWater, &*rampStructures); assert(cache); tile = cache->tile(12, 13); assert(tile->constructed_support); assert(!tile->water);
// An irreconcilable imported route keeps its saved tile-local grade as authoritative terrain // with edge-owned closure faces, without turning it into a structural support. City gradedCity = city; gradedCity.xbld.at(20, 21) = xbld::kRoadFirst; gradedCity.xter.at(20, 21) = static_cast<std::uint8_t>(terrain::Shape::TopHigh); gradedCity.altm.at(20, 21) = 5; const auto importedGrade = terrain::import_legacy_terrain(gradedCity); assert(importedGrade.terrain); terrain::Model gradedTerrain = *importedGrade.terrain; const auto gradedStructures = structure::import_legacy_structures(gradedCity, &gradedTerrain); assert(gradedStructures); assert(!gradedStructures->support({20, 21})); cache = render::TerrainRenderCache::create(gradedTerrain, gradedCity, nullptr, &*gradedStructures); assert(cache); tile = cache->tile(20, 21); assert(!tile->constructed_support); assert(!tile->feature_lift); assert(tile->presentation_code == static_cast<std::uint8_t>(terrain::Shape::TopHigh)); assert(tile->shape == terrain::Shape::TopHigh); assert(tile->raised_corner_mask == terrain::raised_corner_mask(terrain::Shape::TopHigh)); assert(tile->surface_height == 5);
assert(!gradedTerrain.split_edges().empty());
gradedCity.xbld.at(20, 21) = xbld::kNone; const auto bareGradeStructures = structure::import_legacy_structures(gradedCity, &gradedTerrain); assert(bareGradeStructures); assert(!bareGradeStructures->support({20, 21})); cache = render::TerrainRenderCache::create( gradedTerrain, gradedCity, nullptr, &*bareGradeStructures); assert(cache); tile = cache->tile(20, 21); assert(tile->surface_height == 5);
cache = render::TerrainRenderCache::create(flat_model(), city); assert(cache);
City projected = city; assert(terrain::project_to_legacy(flat_model(), projected).ok()); const render::TerrainRenderView projected_view(projected); for (int row = 0; row < City::kDim; ++row) { for (int column = 0; column < City::kDim; ++column) { const render::TerrainRenderTile semantic = cache->tile(row, column) ? *cache->tile(row, column) : render::TerrainRenderTile{}; const render::TerrainRenderTile compatibility = projected_view.tile(row, column); assert(semantic.base_height == compatibility.base_height); assert(semantic.surface_height == compatibility.surface_height); assert(semantic.presentation_code == compatibility.presentation_code); assert(semantic.constructed_support == compatibility.constructed_support); assert(semantic.feature_lift == compatibility.feature_lift); } }
// Rotation happens on the shared vertex lattice. Four turns are exact, and one turn maps the // old top vertex to the new right vertex of the rotated tile. model = terrain::Model::create({128, 128}, std::move(heights)); assert(model); auto once = terrain::rotate_clockwise(*model, 1); assert(once); const auto rotated_corners = once->corners({127, 0}); assert(rotated_corners); assert((*rotated_corners)[1] == 4 * terrain::kHeightQuantum); assert(once->shape({127, 0}) == terrain::Shape::RightHigh); auto four = terrain::rotate_clockwise(*model, 4); assert(four); assert(std::vector<terrain::Height>(four->heights().begin(), four->heights().end()) == std::vector<terrain::Height>(model->heights().begin(), model->heights().end()));
return 0;}