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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379// The network-drawing COMMIT pipeline (sim/network_commit.hpp) — Phase 2. Drives the faithful per-tile place// (terrain adapter / road connect / autotiler) from a solved path. Numeric,// asset-free: pins the laid road codes, automatic crossings (road×rail), dezoning, slope-road shapes, the// terrain flatten, and the 25·terrace + perTile·total cost model.#include "sc2k/sim/network_commit.hpp"#include "sc2k/sim/network_path.hpp"#include "sc2k/sim/terrain_edit.hpp" // terrain_autotile_cell (natural slope from altitudes)#include "sc2k/ui/tools.hpp" // apply_tool (Bulldoze) for the terrain-restore check#include <cstdio>
using namespace sc2k;using sim::NetType;using sim::solve_network_path;using sim::commit_network_path;
#include "test_harness.hpp"
static bool is_road(int code) { return code >= 0x1D && code <= 0x2B; }
int main() { // ---- 1. A straight E-W road: every tile is the E-W straight 0x1D; cost = perTile·tiles. ---- { City c; c.set_funds(1000); auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Road); auto r = commit_network_path(c, p, NetType::Road, 10); CHECK(r.ok && r.tiles == 11 && r.spent == 110); CHECK(c.funds() == 890); bool allStraight = true; for (int col = 10; col <= 20; ++col) if (c.xbld.at(10, col) != 0x1D) allStraight = false; CHECK(allStraight); }
// ---- 2. An L-bend: every laid tile is a road; the turn produces a curve piece (0x23-0x26). ---- { City c; c.set_funds(100000); auto p = solve_network_path(c, 10, 10, 14, 16, NetType::Road); auto r = commit_network_path(c, p, NetType::Road, 10); CHECK(r.ok); bool allRoad = true, sawCurve = false; for (auto& s : p.steps) { const int code = c.xbld.at(s.row, s.col); if (!is_road(code)) allRoad = false; if (code >= 0x23 && code <= 0x26) sawCurve = true; // a corner piece somewhere on the staircase } CHECK(allRoad); CHECK(sawCurve); }
// ---- 3. Crossing a perpendicular RAIL straight makes the combined road-over-rail sprite (0x45). ---- { City c; c.set_funds(100000); c.xbld.at(10, 15) = 0x2D; // rail N-S straight auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Road); auto r = commit_network_path(c, p, NetType::Road, 10); CHECK(r.ok && p.endCol == 20); // the drag crosses and reaches the end CHECK(c.xbld.at(10, 15) == 0x45); // road-over-rail crossing tile CHECK(is_road(c.xbld.at(10, 14)) && is_road(c.xbld.at(10, 16))); }
// ---- 4. Drawing a road through an empty zoned tile DEZONES it (clears the XZON nibble). ---- { City c; c.set_funds(100000); c.xzon.at(10, 15) = 0x01; // zoned residential, undeveloped auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Road); commit_network_path(c, p, NetType::Road, 10); CHECK(is_road(c.xbld.at(10, 15))); CHECK((c.xzon.at(10, 15) & 0x0F) == 0); // dezoned }
// ---- 5. Terrain adapter: a "needs-terrace" slope (shapes 5-8) flattens to 0x0D and loses its water bit. ---- { City c; c.xter.at(5, 5) = 0x05; c.xbit.at(5, 5) = 0x04; sim::commitdetail::terrain_adapt(c, 5, 5, 2); CHECK(c.xter.at(5, 5) == 0x0D && (c.xbit.at(5, 5) & 0x04) == 0); // Cardinal slopes 1-4 are NOT terraced (a road rides them directly). c.xter.at(6, 6) = 0x02; sim::commitdetail::terrain_adapt(c, 6, 6, 2); CHECK(c.xter.at(6, 6) == 0x02); // Single-corner slope 9 → a direction-aligned cardinal ramp (E heading → CORNER_RAMP[2] = slope 2). c.xter.at(7, 7) = 0x09; sim::commitdetail::terrain_adapt(c, 7, 7, 2); CHECK(c.xter.at(7, 7) == 2); }
// ---- 5b. A road over a CORNER slope converts the tile to a cardinal ramp + lays a SLOPED-road sprite // (0x1F-0x22) that connects — not a flat 0x1D floating on the corner (the reported join bug). ---- { City c; c.xter.at(5, 5) = 0x09; // single-corner slope sim::commitdetail::place_one(c, 5, 5, NetType::Road, 2); // road heading East across it CHECK(c.xter.at(5, 5) == 2); // corner → E-W cardinal ramp CHECK(c.xbld.at(5, 5) >= 0x1F && c.xbld.at(5, 5) <= 0x22);// a sloped-road piece (road-family ⇒ connects) }
// ---- 6. Slope-road shape: a road on a cardinal slope takes the sloped-road code (0x1F-0x22). ---- { City c; c.xbld.at(5, 5) = 0x1D; c.xter.at(5, 5) = 0x01; // road on slope shape 1 sim::commitdetail::autotile_road(c, 5, 5); CHECK(c.xbld.at(5, 5) == 0x1F); // 29 + slope-variant 2 }
// ---- 7. Insufficient funds: nothing is laid and funds are untouched. ---- { City c; c.set_funds(50); // < 11·10 auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Road); auto r = commit_network_path(c, p, NetType::Road, 10); CHECK(!r.ok); CHECK(c.funds() == 50); CHECK(c.xbld.at(10, 10) == 0 && c.xbld.at(10, 15) == 0); // map unchanged }
// ---- 8. Rail: a straight run lays the E-W rail straight (0x2C). ---- { City c; c.set_funds(100000); auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Rail); auto r = commit_network_path(c, p, NetType::Rail, 25); CHECK(r.ok && r.spent == 25 * 11); bool ok = true; for (int col = 10; col <= 20; ++col) if (c.xbld.at(10, col) != 0x2C) ok = false; CHECK(ok); }
// ---- 9. Power line: straight run lays 0x0E and marks every tile conductive (XBIT 0x80). ---- { City c; c.set_funds(100000); auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Power); commit_network_path(c, p, NetType::Power, 2); bool ok = true; for (int col = 10; col <= 20; ++col) if (c.xbld.at(10, col) != 0x0E || !(c.xbit.at(10, col) & 0x80)) ok = false; CHECK(ok); }
// ---- 10. Power lines do NOT dezone (a power line can sit on a zone that still develops). ---- { City c; c.set_funds(100000); c.xzon.at(10, 15) = 0x01; auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Power); commit_network_path(c, p, NetType::Power, 2); CHECK(c.xbld.at(10, 15) == 0x0E); CHECK((c.xzon.at(10, 15) & 0x0F) == 1); // zone survives (unlike road/rail) }
// ---- 11. Power crossing a perpendicular road (N-S straight) → power-over-road sprite 0x44. ---- { City c; c.set_funds(100000); c.xbld.at(10, 15) = 0x1E; // road N-S straight (perpendicular to E-W power) auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Power); auto r = commit_network_path(c, p, NetType::Power, 2); CHECK(r.ok && p.endCol == 20); CHECK(c.xbld.at(10, 15) == 0x44); }
// ---- 12. Pipe: laid in the independent native main layer; XUND remains subway-only. ---- { City c; c.set_funds(100000); auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Pipe); commit_network_path(c, p, NetType::Pipe, 3); bool ok = true; for (int col = 10; col <= 20; ++col) { if (!sim::has_water_main(c, 10, col) || c.xund.at(10, col) != 0 || !(c.xbit.at(10, col) & 0x20)) ok = false; } CHECK(ok); CHECK(sim::water_main_presentation_code(c, 10, 15) == 16); // E-W straight pipe }
// ---- 12b. A native main uses the directional pipe sprite for the terrain slope beneath it. ---- { City c; sim::set_water_main(c, 10, 9, true); sim::set_water_main(c, 10, 10, true); sim::set_water_main(c, 10, 11, true); for (int shape = 1; shape <= 4; ++shape) { CHECK(sim::water_main_presentation_code(c, 10, 10, shape) == 0x11 + shape); } CHECK(sim::water_main_presentation_code(c, 10, 10, 2) == 0x13); // canonical terrain override }
// ---- 13. Subway: laid in the XUND layer (1-15), distinct from the pipe range. ---- { City c; c.set_funds(100000); auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Subway); commit_network_path(c, p, NetType::Subway, 100); bool ok = true; for (int col = 10; col <= 20; ++col) { const int u = c.xund.at(10, col); if (u == 0 || u >= 0x10) ok = false; } CHECK(ok); CHECK(c.xund.at(10, 15) == 1); // E-W straight subway }
// ---- 13b. Underground networks never terrace or otherwise mutate the surface above them. ---- // The subway uses shape 5, which the shared network adapter used to turn into XTER 0x0D. A newly // constructed main uses representable cardinal ramp 2; unsupported shapes are covered by network_path. for (const NetType net : {NetType::Pipe, NetType::Subway}) { City c; c.set_funds(100000); c.xter.at(10, 10) = std::uint8_t(net == NetType::Pipe ? 0x02 : 0x05); c.xbit.at(10, 10) = 0x04; c.altm.at(10, 10) = 0x1234; const auto beforeXter = c.xter.cells; const auto beforeAltm = c.altm.cells; const auto p = solve_network_path(c, 10, 10, 10, 10, net); const auto r = commit_network_path(c, p, net, net == NetType::Pipe ? 3 : 100); CHECK(r.ok); CHECK(r.spent == (net == NetType::Pipe ? 3 : 100)); CHECK(c.xter.cells == beforeXter); CHECK(c.altm.cells == beforeAltm); CHECK((c.xbit.at(10, 10) & 0x04) != 0); // surface water state survives }
// ---- 14. A no-op placement (the same network already on the tile) does NOT charge (subway-station spam). ---- { City c; c.set_funds(100000); auto r1 = commit_network_path(c, solve_network_path(c, 10, 10, 10, 10, NetType::Subway), NetType::Subway, 100); CHECK(r1.ok && c.funds() == 99900); // first subway tile charges $100 auto r2 = commit_network_path(c, solve_network_path(c, 10, 10, 10, 10, NetType::Subway), NetType::Subway, 100); CHECK(!r2.ok && c.funds() == 99900); // re-placing on the same tile: no change, no charge }
// ---- 15. Bulldozing a network RESTORES the natural ground slope from the (untouched) altitudes. ---- { City c; c.set_funds(100000); c.altm.at(4, 4) = 1; // NW neighbour higher → (5,5) is a corner slope (shape 9) sc2k::terrain_autotile_cell(c, 5, 5, 0); CHECK(c.xter.at(5, 5) == 9); // the natural terrain sim::commitdetail::place_one(c, 5, 5, NetType::Road, 2); CHECK(c.xter.at(5, 5) == 2); // road terraces it to a cardinal ramp CHECK(c.xbld.at(5, 5) >= 0x1F && c.xbld.at(5, 5) <= 0x22); ui::apply_tool(c, ui::Tool::Bulldoze, 5, 5); // delete the road… CHECK(!(c.xbld.at(5, 5) >= 0x1D && c.xbld.at(5, 5) <= 0x2B)); // …road gone CHECK(c.xter.at(5, 5) == 9); // …and the original corner slope is back }
// ---- 16. Repainting an existing pipe run is a free no-op; mixed runs charge only new mains. ---- { City c; c.set_funds(1000); const auto first = solve_network_path(c, 10, 10, 10, 15, NetType::Pipe); const auto firstResult = commit_network_path(c, first, NetType::Pipe, 3); CHECK(firstResult.ok && firstResult.spent == 18 && c.funds() == 982);
const auto repaint = solve_network_path(c, 10, 10, 10, 15, NetType::Pipe); CHECK(repaint.endCol == 15); CHECK(sim::network_chargeable_tiles(c, repaint, NetType::Pipe) == 0); CHECK(sim::network_path_cost(c, repaint, NetType::Pipe, 3) == 0); const auto xundBeforeRepaint = c.xund.cells; const auto xbitBeforeRepaint = c.xbit.cells; const auto mainsBeforeRepaint = c.waterMains.cells; const auto repaintResult = commit_network_path(c, repaint, NetType::Pipe, 3); CHECK(!repaintResult.ok && repaintResult.spent == 0 && c.funds() == 982); CHECK(c.xund.cells == xundBeforeRepaint && c.xbit.cells == xbitBeforeRepaint); CHECK(c.waterMains.cells == mainsBeforeRepaint);
const auto extension = solve_network_path(c, 10, 10, 10, 20, NetType::Pipe); CHECK(extension.endCol == 20); CHECK(sim::network_chargeable_tiles(c, extension, NetType::Pipe) == 5); CHECK(sim::network_path_cost(c, extension, NetType::Pipe, 3) == 15); const auto extensionResult = commit_network_path(c, extension, NetType::Pipe, 3); CHECK(extensionResult.ok && extensionResult.spent == 15 && c.funds() == 967); }
// ---- 16b. Removing a visible main does not erase an imported hidden compatibility link. ---- { City c; c.waterDistributionModel = WaterDistributionModel::ClassicLinks; sim::initialize_modern_water_network(c); c.legacyWaterLinks.at(12, 13) = 1; sim::sync_water_pipe_bit(c, c.index(12, 13)); sim::set_water_main(c, 12, 13, true); sim::set_water_main(c, 12, 13, false); CHECK(!sim::has_water_main(c, 12, 13)); CHECK(c.legacyWaterLinks.at(12, 13) == 1); CHECK(c.xbit.at(12, 13) & xbit::kPipe); }
// ---- 16c. A standalone imported visible main is not duplicated into the hidden layer. ---- { City c; c.waterDistributionModel = WaterDistributionModel::ClassicLinks; c.xund.at(12, 13) = xund::kPipeFirst; c.xbit.at(12, 13) |= xbit::kPipe; sim::initialize_modern_water_network(c); CHECK(sim::has_water_main(c, 12, 13)); CHECK(c.legacyWaterLinks.at(12, 13) == 0); sim::set_water_main(c, 12, 13, false); CHECK(!(c.xbit.at(12, 13) & xbit::kPipe)); }
// ---- 17. A genuine branch through an existing pipe is charged only for its new arms and makes a cross. ---- { City c; c.set_funds(1000); const auto horizontal = solve_network_path(c, 10, 10, 10, 20, NetType::Pipe); CHECK(commit_network_path(c, horizontal, NetType::Pipe, 3).ok); const int fundsBeforeBranch = c.funds();
const auto vertical = solve_network_path(c, 5, 15, 15, 15, NetType::Pipe); CHECK(vertical.endRow == 15 && vertical.endCol == 15); CHECK(sim::network_chargeable_tiles(c, vertical, NetType::Pipe) == 10); const auto branchResult = commit_network_path(c, vertical, NetType::Pipe, 3); CHECK(branchResult.ok && branchResult.spent == 30); CHECK(c.funds() == fundsBeforeBranch - 30); CHECK(sim::water_main_presentation_code(c, 10, 15) == 30); // four-way pipe intersection }
// ---- 18. Reused tiles do not participate in the funds gate; one affordable extension still commits. ---- { City c; c.set_funds(1000); CHECK(commit_network_path(c, solve_network_path(c, 10, 10, 10, 20, NetType::Pipe), NetType::Pipe, 3).ok); c.set_funds(3); const auto oneNewTile = solve_network_path(c, 10, 10, 10, 21, NetType::Pipe); CHECK(sim::network_path_cost(c, oneNewTile, NetType::Pipe, 3) == 3); const auto result = commit_network_path(c, oneNewTile, NetType::Pipe, 3); CHECK(result.ok && result.spent == 3 && c.funds() == 0); CHECK(sim::water_main_presentation_code(c, 10, 21) == 16); }
// ---- 19. A pipe can share any tile with a subway, and the layers persist independently. ---- { City c; c.set_funds(10000); CHECK(commit_network_path(c, solve_network_path(c, 5, 15, 15, 15, NetType::Subway), NetType::Subway, 100).ok); const auto pipe = solve_network_path(c, 10, 10, 10, 20, NetType::Pipe); CHECK(pipe.endRow == 10 && pipe.endCol == 20); const auto result = commit_network_path(c, pipe, NetType::Pipe, 3); CHECK(result.ok && result.spent == 33); CHECK(c.xund.at(10, 15) == 2); // subway N-S remains intact CHECK(sim::has_water_main(c, 10, 15)); // pipe E-W occupies its own layer
const auto repaint = solve_network_path(c, 10, 10, 10, 20, NetType::Pipe); CHECK(repaint.endCol == 20); CHECK(sim::network_path_cost(c, repaint, NetType::Pipe, 3) == 0); }
// ---- 20. A main can pass through a subway station; this was impossible in shared XUND. ---- { City c; c.set_funds(1000); c.xbld.at(51, 57) = xbld::kSubwayStation; c.xund.at(51, 57) = xund::kSubRail; const auto pipe = solve_network_path(c, 51, 54, 51, 60, NetType::Pipe); CHECK(pipe.valid && pipe.endRow == 51 && pipe.endCol == 60); CHECK(commit_network_path(c, pipe, NetType::Pipe, 3).ok); CHECK(c.xund.at(51, 57) == xund::kSubRail); CHECK(sim::has_water_main(c, 51, 57)); }
if (failures == 0) { std::printf("network_commit: all checks passed\n"); return 0; } std::printf("network_commit: %d failure(s)\n", failures); return 1;}