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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198// Construction grading plans and commits are deterministic, permanent, and atomic.#include "sc2k/sim/construction_terrain.hpp"#include "sc2k/sim/growth.hpp"#include "sc2k/sim/network_commit.hpp"
#include <algorithm>#include <cstdio>#include <vector>
#include "test_harness.hpp"
using namespace sc2k;
namespace {
terrain::Model model_with_slope() { constexpr int dimension = map_dimension(MapSize::Tiny); std::vector<terrain::Height> heights( static_cast<std::size_t>(dimension + 1) * (dimension + 1), 5 * terrain::kHeightQuantum); for (int row = 0; row <= dimension; ++row) for (int column = 9; column <= dimension; ++column) heights[static_cast<std::size_t>(row) * (dimension + 1) + column] = 6 * terrain::kHeightQuantum; auto model = terrain::Model::create({dimension, dimension}, std::move(heights)); CHECK(model.has_value()); return std::move(*model);}
structure::Model empty_structures() { constexpr int dimension = map_dimension(MapSize::Tiny); auto model = structure::Model::create({dimension, dimension}, std::vector<structure::Support>(dimension * dimension)); CHECK(model.has_value()); return std::move(*model);}
hydrology::Model dry_hydrology(const terrain::Model& terrain) { constexpr int dimension = map_dimension(MapSize::Tiny); auto model = hydrology::Model::create({dimension, dimension}, {}, std::vector<hydrology::Cell>(dimension * dimension), &terrain); CHECK(model.has_value()); return std::move(*model);}
} // namespace
int main() { // A road crossing the ridge is flattened only across its width. Reversing the same geometric path // produces the same canonical terrain, and an L-turn becomes a flat junction tile. { const terrain::Model source = model_with_slope(); City pathCity(MapSize::Tiny); const sim::NetworkPath legacyLimited = sim::solve_network_path( pathCity, 5, 8, 12, 8, sim::NetType::Road, &source); const sim::NetworkPath preparedRoute = sim::solve_construction_network_path( pathCity, 5, 8, 12, 8, sim::NetType::Road, &source); CHECK(legacyLimited.endRow != 12 || legacyLimited.endCol != 8); CHECK(preparedRoute.endRow == 12 && preparedRoute.endCol == 8);
sim::NetworkPath east; east.valid = true; for (int column = 5; column <= 11; ++column) east.steps.push_back({10, column, 2}); east.endRow = 10; east.endCol = 11; const sim::ConstructionTerrainPlan eastGrade = sim::plan_road_construction_grade(source, east); CHECK(eastGrade.ok()); for (int column = 5; column <= 11; ++column) { const terrain::Corners corners = *eastGrade.terrain.result->corners({10, column}); CHECK(corners[0] == corners[1]); CHECK(corners[3] == corners[2]); CHECK(std::llabs(corners[3] - corners[0]) <= terrain::kHeightQuantum); }
sim::NetworkPath west; west.valid = true; for (int column = 11; column >= 5; --column) west.steps.push_back({10, column, 0}); west.endRow = 10; west.endCol = 5; const sim::ConstructionTerrainPlan westGrade = sim::plan_road_construction_grade(source, west); CHECK(westGrade.ok()); CHECK(std::equal(eastGrade.terrain.result->heights().begin(), eastGrade.terrain.result->heights().end(), westGrade.terrain.result->heights().begin()));
sim::NetworkPath bend; bend.valid = true; for (int column = 5; column <= 8; ++column) bend.steps.push_back({10, column, 2}); for (int row = 11; row <= 13; ++row) bend.steps.push_back({row, 8, 1}); bend.endRow = 13; bend.endCol = 8; const sim::ConstructionTerrainPlan bendGrade = sim::plan_road_construction_grade(source, bend); CHECK(bendGrade.ok()); const terrain::Corners turn = *bendGrade.terrain.result->corners({10, 8}); CHECK(std::all_of(turn.begin(), turn.end(), [&](terrain::Height height) { return height == turn.front(); }));
terrain::Model committedTerrain = source; structure::Model committedStructures = empty_structures(); hydrology::Model committedHydrology = dry_hydrology(committedTerrain); City committedCity(MapSize::Tiny); committedCity.set_funds(10'000); const int baseCost = static_cast<int>(east.steps.size()) * 10; const auto roadCommit = sim::commit_construction_with_terrain(committedCity, committedTerrain, committedStructures, committedHydrology, eastGrade, baseCost, 25, [&](City& candidate, const terrain::Model*) { return sim::place_network_path(candidate, east, sim::NetType::Road, /*adaptCompatibilityTerrain=*/false); }); CHECK(roadCommit.ok()); CHECK(committedCity.funds() == 10'000 - roadCommit.spent); for (const sim::PathStep& step : east.steps) CHECK(xbld::is_road(committedCity.xbld.at(step.row, step.col))); CHECK(terrain::matches_legacy_projection( committedTerrain, committedCity, &committedStructures)); }
terrain::Model terrain = model_with_slope(); structure::Model structures = empty_structures(); hydrology::Model hydrology = dry_hydrology(terrain); City city(MapSize::Tiny); city.set_funds(10'000);
// A 3x3 footprint crossing the ridge becomes one flat pad and commits the building on that prepared // model. The earthworks remain after the building is removed. const sim::ConstructionTerrainPlan pad = sim::plan_flat_construction_pad(terrain, {5, 7}, 3); CHECK(pad.ok()); CHECK(!pad.terrain.vertices.empty()); const int earthwork = sim::construction_earthwork_cost(pad, 25); CHECK(earthwork > 0); const auto placed = sim::commit_construction_with_terrain(city, terrain, structures, hydrology, pad, 500, 25, [](City& candidate, const terrain::Model* prepared) { return construct_building(candidate, 5, 7, 210, 3, 0, prepared); }); CHECK(placed.ok()); CHECK(placed.spent == 500 + earthwork); CHECK(city.funds() == 10'000 - placed.spent); for (int row = 5; row < 8; ++row) for (int column = 7; column < 10; ++column) { CHECK(terrain::classify(*terrain.corners({row, column})).shape == terrain::Shape::Flat); city.xbld.at(row, column) = xbld::kNone; } for (int row = 5; row < 8; ++row) for (int column = 7; column < 10; ++column) CHECK(terrain::classify(*terrain.corners({row, column})).shape == terrain::Shape::Flat);
// An occupied shoulder vetoes the complete transaction: no terrain, funds, or placement leaks. terrain::Model blockedTerrain = model_with_slope(); structure::Model blockedStructures = empty_structures(); hydrology::Model blockedHydrology = dry_hydrology(blockedTerrain); City blockedCity(MapSize::Tiny); blockedCity.set_funds(10'000); const sim::ConstructionTerrainPlan blockedPad = sim::plan_flat_construction_pad(blockedTerrain, {5, 7}, 3); CHECK(blockedPad.ok()); const terrain::TileCoord shoulder = blockedPad.terrain.affected_tiles.front(); blockedCity.xbld.at(shoulder.row, shoulder.column) = xbld::kRoadFirst; const std::vector<terrain::Height> beforeHeights( blockedTerrain.heights().begin(), blockedTerrain.heights().end()); const auto blocked = sim::commit_construction_with_terrain(blockedCity, blockedTerrain, blockedStructures, blockedHydrology, blockedPad, 500, 25, [](City& candidate, const terrain::Model* prepared) { return construct_building(candidate, 5, 7, 210, 3, 0, prepared); }); CHECK(blocked.error == sim::ConstructionTerrainError::Occupied); CHECK(blockedCity.funds() == 10'000); CHECK(std::equal(beforeHeights.begin(), beforeHeights.end(), blockedTerrain.heights().begin())); CHECK(blockedCity.xbld.at(5, 7) != 210);
// Placement failure after a valid terrain proposal also rolls the prepared model back. terrain::Model refusedTerrain = model_with_slope(); structure::Model refusedStructures = empty_structures(); hydrology::Model refusedHydrology = dry_hydrology(refusedTerrain); City refusedCity(MapSize::Tiny); refusedCity.set_funds(10'000); const auto refusedPad = sim::plan_flat_construction_pad(refusedTerrain, {5, 7}, 3); const std::vector<terrain::Height> refusedBefore( refusedTerrain.heights().begin(), refusedTerrain.heights().end()); const auto refused = sim::commit_construction_with_terrain(refusedCity, refusedTerrain, refusedStructures, refusedHydrology, refusedPad, 500, 25, [](City&, const terrain::Model*) { return false; }); CHECK(refused.error == sim::ConstructionTerrainError::PlacementFailed); CHECK(refusedCity.funds() == 10'000); CHECK(std::equal(refusedBefore.begin(), refusedBefore.end(), refusedTerrain.heights().begin()));
if (failures == 0) { std::printf("construction terrain: flat pads, occupancy vetoes, and atomic commits\n"); return 0; } std::printf("construction terrain: %d failure(s)\n", failures); return 1;}