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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339// tests/test_terrain_transaction.cpp — canonical terrain edits commit or roll back the whole live world.#include "sc2k/io/sc2_file.hpp"#include "sc2k/sim/generated_terrain.hpp"#include "sc2k/sim/terrain_transaction.hpp"#include "sc2k/terrain/legacy_projection.hpp"#include "sc2k/terrain/legacy_import.hpp"#include "sc2k/ui/tools.hpp"
#include <algorithm>#include <cstdio>#include <vector>
#include "test_harness.hpp"#include "test_paths.hpp"
using namespace sc2k;
namespace {
terrain::Model flat_model(int level) { std::vector<terrain::Height> heights(129 * 129, level * terrain::kHeightQuantum); auto model = terrain::Model::create({City::kDim, City::kDim}, std::move(heights)); CHECK(model.has_value()); return std::move(*model);}
structure::Model structures_from(const City& city) { auto model = structure::import_legacy_structures(city); CHECK(model.has_value()); return std::move(*model);}
bool same_terrain(const terrain::Model& left, const terrain::Model& right) { return left.extent() == right.extent() && std::equal(left.heights().begin(), left.heights().end(), right.heights().begin());}
bool same_structures(const structure::Model& left, const structure::Model& right) { return left.extent() == right.extent() && std::equal(left.supports().begin(), left.supports().end(), right.supports().begin());}
bool same_city(const City& left, const City& right) { return left.name == right.name && left.xter.cells == right.xter.cells && left.xbld.cells == right.xbld.cells && left.xzon.cells == right.xzon.cells && left.xund.cells == right.xund.cells && left.xtxt.cells == right.xtxt.cells && left.xbit.cells == right.xbit.cells && left.altm.cells == right.altm.cells && left.xtrf.cells == right.xtrf.cells && left.xplt.cells == right.xplt.cells && left.xval.cells == right.xval.cells && left.xcrm.cells == right.xcrm.cells && left.xplc.cells == right.xplc.cells && left.xfir.cells == right.xfir.cells && left.xpop.cells == right.xpop.cells && left.xrog.cells == right.xrog.cells && left.xlab == right.xlab && left.xmic == right.xmic && left.xthg == right.xthg && left.misc == right.misc && left.xgrp == right.xgrp && left.graph_max == right.graph_max && left.scen == right.scen && left.briefing == right.briefing && left.pict == right.pict && left.savedCompass == right.savedCompass;}
} // namespace
int main() { using terrain::EditCommand; using terrain::EditKind;
// The live tool writes canonical terrain first, derives the compatibility arrays, and charges once. { City city; city.set_funds(1000); terrain::Model terrain = flat_model(5); structure::Model structures = structures_from(city); CHECK(terrain::project_to_legacy(terrain, city, &structures).ok());
const ui::ToolResult result = ui::apply_tool(city, ui::Tool::RaiseTerrain, 40, 40, -1, &terrain, &structures); CHECK(result.ok); CHECK(result.spent == 25); CHECK(city.funds() == 975); CHECK(terrain.validate().valid()); CHECK(terrain::matches_legacy_projection(terrain, city, &structures)); const terrain::Corners corners = *terrain.corners({40, 40}); CHECK(std::all_of(corners.begin(), corners.end(), [](terrain::Height height) { return height == 6 * terrain::kHeightQuantum; })); }
// Editing an imported breakline footprint destroys the road and removes that local discontinuity // atomically, while an unrelated breakline and a real foundation survive unchanged. { City city; for (std::uint16_t& altitude : city.altm.cells) altitude = 5; city.xbld.at(45, 45) = xbld::kRoadFirst; city.xter.at(45, 45) = static_cast<std::uint8_t>(terrain::Shape::RightHigh); city.altm.at(45, 45) = 6; city.xter.at(70, 70) = static_cast<std::uint8_t>(terrain::Shape::LeftHigh); city.altm.at(70, 70) = 6; city.xbld.at(80, 80) = xbld::kRoadFirst; city.xter.at(80, 80) = xter::kCube; city.altm.at(80, 80) = 4; const auto importedTerrain = terrain::import_legacy_terrain(city); CHECK(importedTerrain.terrain.has_value()); terrain::Model terrain = *importedTerrain.terrain; auto importedStructures = structure::import_legacy_structures(city, &terrain); CHECK(importedStructures.has_value()); structure::Model structures = std::move(*importedStructures); CHECK(!structures.support({45, 45})); CHECK(!structures.support({70, 70})); CHECK(structures.support({80, 80}).kind == structure::SupportKind::Foundation); CHECK(structures.support({80, 80}).elevation_offset == -1); const std::size_t split_count = terrain.split_edges().size();
const sim::TerrainTransactionResult result = sim::apply_terrain_transaction(city, terrain, &structures, EditCommand{EditKind::Raise, {45, 45}}); CHECK(result.ok()); CHECK(city.xbld.at(45, 45) == xbld::kNone); CHECK(!structures.support({45, 45})); CHECK(terrain.corners({70, 70}) == importedTerrain.terrain->corners({70, 70})); CHECK(terrain.split_edges().size() < split_count); CHECK(structures.support({80, 80}).kind == structure::SupportKind::Foundation); CHECK(structures.support({80, 80}).elevation_offset == -1); CHECK(terrain::matches_legacy_projection(terrain, city, &structures)); }
// A foundation belongs to the structural layer. Editing its ground demolishes the network and its // support together, then projects the now-natural canonical surface back to the compatibility arrays. { City city; city.set_funds(1000); terrain::Model terrain = flat_model(5); city.xbld.at(40, 40) = xbld::kRoadFirst; city.xter.at(40, 40) = xter::kCube; structure::Model structures = structures_from(city); CHECK(structures.support({40, 40})); CHECK(terrain::project_to_legacy(terrain, city, &structures).ok());
const sim::TerrainTransactionResult result = sim::apply_terrain_transaction(city, terrain, &structures, EditCommand{EditKind::Raise, {40, 40}}); CHECK(result.ok()); CHECK(city.xbld.at(40, 40) == xbld::kNone); CHECK(!structures.support({40, 40})); CHECK(city.xter.at(40, 40) != xter::kCube); CHECK(terrain::matches_legacy_projection(terrain, city, &structures)); CHECK(structure::matches_legacy_structures(structures, city)); }
// A caller without a retained canonical hydrology model imports the compatibility water for the // transaction. Raising its bed through the fixed surface dries it instead of rejecting the edit. { City city; terrain::Model terrain = flat_model(5); structure::Model structures = structures_from(city); CHECK(terrain::project_to_legacy(terrain, city, &structures).ok()); city.set_misc_be32(misc::kWaterLevel, 6); city.altm.at(40, 40) = static_cast<std::uint16_t>(5 | (6 << 5)); city.xter.at(40, 40) = xter::kShore; city.xbit.at(40, 40) |= xbit::kWater;
const sim::TerrainTransactionResult result = sim::apply_terrain_transaction(city, terrain, &structures, EditCommand{EditKind::Raise, {40, 40}}); CHECK(result.ok()); CHECK((city.xbit.at(40, 40) & xbit::kWater) == 0); CHECK(terrain::classify(*terrain.corners({40, 40})).base_height == 6 * terrain::kHeightQuantum); }
// Shallow editor water may share the global surface with a generated body. Editing unrelated dry // land must neither reclassify that flush water as an invalid sea bed nor drain generated shores. { terrain::GenerationSpec spec; spec.extent = {City::kDim, City::kDim}; spec.centre = terrain::CentreFeature::RiverNorthSouth; spec.north = terrain::EdgeFeature::Sea; spec.south = terrain::EdgeFeature::River; spec.east = terrain::EdgeFeature::Land; spec.west = terrain::EdgeFeature::Sea; spec.mountainousness = 42; spec.wateriness = 58; spec.seed = 0xC17A11u;
City city; sim::GeneratedTerrainInstallation installed = sim::install_generated_terrain(city, spec); CHECK(installed.ok());
terrain::TileCoord painted_tile{-1, -1}; for (int row = 8; row < City::kDim - 8 && painted_tile.row < 0; ++row) { for (int column = 8; column < City::kDim - 8; ++column) { if (!installed.hydrology->wet({row, column})) { painted_tile = {row, column}; break; } } } CHECK(painted_tile.row >= 0); CHECK(hydrology::paint_surface_water(city, *installed.hydrology, *installed.terrain, &*installed.structures, painted_tile)); const std::vector<hydrology::Sample> water_before = hydrology::samples(*installed.hydrology);
terrain::TileCoord dry_tile{-1, -1}; for (int row = 8; row < City::kDim - 8 && dry_tile.row < 0; ++row) { for (int column = 8; column < City::kDim - 8; ++column) { const terrain::EditPlan plan = terrain::plan_edit(*installed.terrain, {EditKind::Raise, {row, column}}); const bool affects_water = plan.ok() && std::any_of(plan.affected_tiles.begin(), plan.affected_tiles.end(), [&](terrain::TileCoord tile) { return installed.hydrology->wet(tile); }); if (plan.ok() && !affects_water) { dry_tile = {row, column}; break; } } } CHECK(dry_tile.row >= 0); const sim::TerrainTransactionResult result = sim::apply_terrain_transaction(city, *installed.terrain, &*installed.structures, {EditKind::Raise, dry_tile}, &*installed.hydrology); CHECK(result.ok()); const std::vector<hydrology::Sample> water_after_raise = hydrology::samples(*installed.hydrology); for (std::size_t index = 0; index < water_before.size(); ++index) if (water_before[index]) CHECK(water_after_raise[index]); CHECK(installed.hydrology->wet(painted_tile));
const sim::TerrainTransactionResult lowered = sim::apply_terrain_transaction(city, *installed.terrain, &*installed.structures, {EditKind::Lower, dry_tile}, &*installed.hydrology); CHECK(lowered.ok()); CHECK(installed.hydrology->wet(painted_tile)); }
// The canonical model can represent heights beyond the temporary five-bit mirror. Projection failure // is nevertheless atomic: no demolition, zoning, underground state, funds, or canonical state leaks. { City city; city.set_funds(4321); terrain::Model terrain = flat_model(31); city.xbld.at(40, 40) = xbld::kRoadFirst; city.xter.at(40, 40) = xter::kCube; city.xzon.at(40, 40) = 1; city.xund.at(40, 40) = 0x10; structure::Model structures = structures_from(city); CHECK(terrain::project_to_legacy(terrain, city, &structures).ok()); const City beforeCity = city; const terrain::Model beforeTerrain = terrain; const structure::Model beforeStructures = structures;
const sim::TerrainTransactionResult result = sim::apply_terrain_transaction(city, terrain, &structures, EditCommand{EditKind::Raise, {40, 40}}); CHECK(result.error == sim::TerrainTransactionError::ProjectionFailed); CHECK(same_city(city, beforeCity)); CHECK(same_terrain(terrain, beforeTerrain)); CHECK(same_structures(structures, beforeStructures)); }
// A converted classic map uses the same canonical terrain-tool path as a new city. Cape Quest // contains the joined shallow/deep water topology which previously made hydrology reconciliation // reject every terrain edit, even on dry land. { City imported_city; if (io::load_sc2(city_file("CAPEQUES.SC2"), imported_city).ok) { const terrain::LegacyImportResult imported_terrain = terrain::import_legacy_terrain(imported_city); CHECK(imported_terrain.terrain.has_value()); const auto imported_structures = imported_terrain.terrain ? structure::import_legacy_structures(imported_city, &*imported_terrain.terrain) : std::nullopt; const auto imported_hydrology = imported_terrain.terrain && imported_structures ? hydrology::import_legacy_hydrology( imported_city, &*imported_terrain.terrain, &*imported_structures) : std::nullopt; CHECK(imported_structures.has_value()); CHECK(imported_hydrology.has_value()); if (imported_terrain.terrain && imported_structures && imported_hydrology) { const terrain::TileCoord tile{40, 40}; const terrain::Query query(imported_city, &*imported_terrain.terrain, &*imported_hydrology); const int base_level = query.base_level(tile); const int level_target = base_level > 0 ? base_level - 1 : base_level + 1; for (const auto [tool, argument] : {std::pair{ui::Tool::RaiseTerrain, -1}, std::pair{ui::Tool::LowerTerrain, -1}, std::pair{ui::Tool::LevelTerrain, level_target}}) { City city = imported_city; city.set_funds(1000000); terrain::Model terrain = *imported_terrain.terrain; structure::Model structures = *imported_structures; hydrology::Model hydrology = *imported_hydrology; const ui::ToolResult result = ui::apply_tool(city, tool, tile.row, tile.column, argument, &terrain, &structures, &hydrology); CHECK(result.ok); CHECK(terrain.validate().valid()); CHECK(hydrology.validate(&terrain).valid()); }
// Raised shallow compatibility-water cells at these near-edge tiles used to freeze at // their imported levels while the bed dropped, sometimes exposing patch boundaries that // rejected later edits. It now follows the edited bed until the global sea subsumes it. const int sea_level = imported_city.water_level(); for (const terrain::TileCoord shallow : {terrain::TileCoord{126, 82}, {122, 86}, {122, 87}, {122, 88}, {124, 86}, {124, 87}}) { City city = imported_city; terrain::Model terrain = *imported_terrain.terrain; structure::Model structures = *imported_structures; hydrology::Model water = *imported_hydrology; CHECK(water.body_at(shallow) != nullptr); CHECK(water.body_at(shallow)->kind == hydrology::BodyKind::SurfaceWater); for (int step = 0; step < 16; ++step) { if (water.surface_height(shallow) <= sea_level * terrain::kHeightQuantum) break; const sim::TerrainTransactionResult lowered = sim::apply_terrain_transaction(city, terrain, &structures, {terrain::EditKind::Lower, shallow}, &water); CHECK(lowered.ok()); if (!lowered.ok()) break; const terrain::ShapeClassification ground = terrain::classify(*terrain.corners(shallow)); CHECK(ground); if (ground) { const terrain::Height expected_surface = std::max( ground.base_height, sea_level * terrain::kHeightQuantum); CHECK(water.surface_height(shallow) == expected_surface); } CHECK(terrain.validate().valid()); CHECK(water.validate(&terrain).valid()); } CHECK(water.surface_height(shallow) == sea_level * terrain::kHeightQuantum); } } } else { std::puts("terrain transaction: CAPEQUES.SC2 unavailable; classic-map edit regression skipped"); } }
if (failures == 0) { std::printf("terrain transaction: canonical terrain/water commit and foundation cleanup\n"); return 0; } std::printf("terrain transaction: %d failure(s)\n", failures); return 1;}