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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122// The drag-stroke model (ui/drag.hpp + apply_tool): a FAST drag must lay a CONTINUOUS network (no skipped// tiles) and a zone drag must fill the whole RECTANGLE — the original draws gaplessly (tight poll loop /// Bresenham line / rect fill); an un-interpolated SDL drag would leave a dotted, disconnected mess (the// user-reported "can't properly draw networks or zones"). Verified numerically, no assets needed.#include "sc2k/ui/drag.hpp"#include "sc2k/ui/tools.hpp"#include <cstdio>#include <vector>
using namespace sc2k;
#include "test_harness.hpp"
// Replay a drag as the app does: apply_tool at the first sample, then walk_line to each next sample// (skipping the already-applied head), exactly like apply_at's linear-tool branch.static void replay_linear(City& c, ui::Tool tool, const std::vector<std::pair<int, int>>& samples) { bool first = true; int pr = -1, pc = -1; for (auto [r, col] : samples) { if (first) { ui::apply_tool(c, tool, r, col); first = false; } else ui::walk_line(pr, pc, r, col, /*includeStart=*/false, [&](int rr, int cc) { ui::apply_tool(c, tool, rr, cc); }); pr = r; pc = col; }}
int main() { // Raw motion events may arrive much faster than frames. Every terrain sample remains ordered, but // their expensive utility/reprojection tail runs once when the event batch is flushed. { ui::DragRefreshBatch batch; int refreshes = 0; for (int event = 0; event < 100; ++event) batch.record(event % 3 == 0); CHECK(batch.pending()); batch.flush([&] { ++refreshes; }); CHECK(refreshes == 1); CHECK(!batch.pending()); batch.flush([&] { ++refreshes; }); CHECK(refreshes == 1); batch.record(true); batch.flush([&] { ++refreshes; }); CHECK(refreshes == 2); }
// ---- walk_line is gapless: consecutive tiles never jump more than one step in either axis. ---- { // clang-format off const int ends[][4] = {{0,0,0,12},{0,0,12,0},{3,4,9,17},{20,20,5,9},{7,7,7,7},{2,30,29,1}}; // clang-format on for (auto& e : ends) { int lr = e[0], lc = e[1]; bool firstTile = true, ok = true; int n = 0; ui::walk_line(e[0], e[1], e[2], e[3], true, [&](int r, int c) { if (!firstTile) { if (std::abs(r - lr) > 1 || std::abs(c - lc) > 1 || (r == lr && c == lc)) ok = false; } lr = r; lc = c; firstTile = false; ++n; }); CHECK(ok); // every step is to an 8-neighbour, no repeats CHECK(lr == e[2] && lc == e[3]); // …and it reaches the endpoint const int span = 1 + std::max(std::abs(e[2] - e[0]), std::abs(e[3] - e[1])); CHECK(n == span); // exactly Chebyshev-length tiles (gapless, no overshoot) } }
// ---- A FAST road drag (sparse samples that jump 10+ tiles) lays a CONTINUOUS road, not dots. ---- { City c; c.set_funds(1000000); // Three widely-spaced samples — an L: (10,10) → (10,30) → (25,30). A raw SDL drag would only touch // those 3 tiles; interpolation must fill the whole L. // clang-format off replay_linear(c, ui::Tool::Road, {{10,10},{10,30},{25,30}}); auto isRoad = [&](int r, int col){ return ui::detail::is_road(c.xbld.at(r, col)); }; int gaps = 0; for (int col = 10; col <= 30; ++col) if (!isRoad(10, col)) ++gaps; // horizontal leg complete? for (int r = 10; r <= 25; ++r) if (!isRoad(r, 30)) ++gaps; // vertical leg complete? CHECK(gaps == 0); // no holes anywhere on the L // Contrast: applying ONLY at the 3 sample tiles (no interpolation) leaves big gaps — proves the // interpolation is load-bearing, not incidental. City d; d.set_funds(1000000); for (auto [r, col] : {std::pair{10,10},{10,30},{25,30}}) ui::apply_tool(d, ui::Tool::Road, r, col); // clang-format on int dotted = 0; for (int col = 10; col <= 30; ++col) if (!ui::detail::is_road(d.xbld.at(10, col))) ++dotted; for (int r = 10; r <= 25; ++r) if (!ui::detail::is_road(d.xbld.at(r, 30))) ++dotted; CHECK(dotted > 25); // the un-interpolated drag is mostly holes // Continuity also shows in autotiling: the corner (10,30) becomes a real junction (tee/curve), not a // lone straight — only possible if both legs actually meet there. CHECK(c.xbld.at(10, 30) >= 0x23 && c.xbld.at(10, 30) <= 0x2b); }
// ---- A zone drag fills the whole RECTANGLE between the anchor and the cursor. ---- { City c; c.set_funds(1000000); int zoned = 0; ui::walk_rect(12, 8, 17, 20, [&](int r, int col) { if (ui::apply_tool(c, ui::Tool::ZoneR, r, col).ok) ++zoned; }); CHECK(zoned == 6 * 13); // every tile in [12..17]x[8..20] for (int r = 12; r <= 17; ++r) for (int col = 8; col <= 20; ++col) CHECK((c.xzon.at(r, col) & 0x0F) == 1); // Outside the rectangle stays untouched. CHECK((c.xzon.at(11, 8) & 0x0F) == 0 && (c.xzon.at(18, 20) & 0x0F) == 0 && (c.xzon.at(12, 21) & 0x0F) == 0); }
if (failures == 0) { std::printf("drag: all checks passed\n"); return 0; } std::printf("drag: %d failure(s)\n", failures); return 1;}