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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269// Numeric verification of the screen->tile pick (ui/pick.hpp): the forward projection and the inverse// must round-trip. For every tile we project its diamond centre to screen, pick it back, and require// the same tile — across flat ground, hills, and water (the altitude-aware path). Plus a painter's-order// ground truth on a synthetic steep cone: every pixel must pick the tile whose drawn surface is VISIBLE// there (the volcano-cone "highlight lands on the opposite side of the ridge" bug). Not eyeballed.#include "sc2k/io/sc2_file.hpp"#include "sc2k/sim/terrain_edit.hpp"#include "sc2k/ui/pick.hpp"#include <cstdio>#include <string>#include <vector>#include "test_paths.hpp"
using namespace sc2k;
#include "test_harness.hpp"
// The renderer's tile altitude (city_renderer.hpp tile_alt, the original's model): LAND at ground, WATER at the// per-tile water-level field. picked_altitude MUST equal this or the hover highlight / tools land on the// wrong tile (the map-editor "misaligned cursor" bug).static int render_alt(const City& c, int r, int col) { const std::uint16_t a = c.altm.at(r, col); return (c.xter.at(r, col) < 0x10) ? (int(a) & 0x1F) : ((int(a) >> 5) & 0x1F);}
// Painter's-order ground truth: paint every tile's drawn surface quad (ui::surface_quad, the renderer's// terrain-face geometry) into a visible-tile buffer back-to-front, then sweep pixels (2 px stride) and// count where pick_tile disagrees with the tile actually visible there. Returns wrong count; total out.static long painters_truth_sweep(const City& city, const render::Camera& cam, int W, int H, long& total, bool underground_floor = false) { using namespace sc2k; std::vector<int> vis(std::size_t(W) * H, -1); for (int s = 0; s <= 254; ++s) // painter: r+c ascending for (int r = std::max(0, s - 127); r <= std::min(127, s); ++r) { const int c = s - r; render::ScreenPos v[4]; ui::surface_quad(city, cam, r, c, v, 2, true, 1.f, nullptr, underground_floor); int minY = v[0].y, maxY = v[0].y; for (int i = 1; i < 4; ++i) { minY = std::min(minY, v[i].y); maxY = std::max(maxY, v[i].y); } for (int y = std::max(0, minY); y <= std::min(H - 1, maxY); ++y) { // scanline extents — the same edge rule as quad_contains/blend_quad int xl = 1 << 30, xr = -(1 << 30); for (int i = 0; i < 4; ++i) { const render::ScreenPos A = v[i], B = v[(i + 1) & 3]; if (y < std::min(A.y, B.y) || y > std::max(A.y, B.y)) continue; if (A.y == B.y) { xl = std::min({xl, A.x, B.x}); xr = std::max({xr, A.x, B.x}); continue; } const int ex = A.x + (B.x - A.x) * (y - A.y) / (B.y - A.y); xl = std::min(xl, ex); xr = std::max(xr, ex); } for (int x = std::max(0, xl); x <= std::min(W - 1, xr); ++x) vis[std::size_t(y) * W + x] = r * 128 + c; } } long wrong = 0; total = 0; for (int y = 0; y < H; y += 2) for (int x = 0; x < W; x += 2) { const int t = vis[std::size_t(y) * W + x]; if (t < 0) continue; const ui::PickResult pr = ui::pick_tile(city, cam, x, y, nullptr, underground_floor); ++total; if (!pr.hit || pr.row * 128 + pr.col != t) ++wrong; } return wrong;}
int main() { // Underground interaction geometry belongs to the renderer's lowered dirt floor, not the hidden // surface terrain. At zoom 2 one altitude unit is exactly 12 screen pixels, so this catches the // former generic-hover fallback even when the selected tile and camera remain unchanged. { City city; constexpr int row = 20, col = 30, baseHeight = 5; for (std::uint16_t& altitude : city.altm.cells) altitude = baseHeight; render::ScreenPos quad[4]; const render::Camera camera{100, 80}; ui::surface_quad(city, camera, row, col, quad, 2, /*slope=*/false, 1.f, nullptr, /*underground_floor=*/true); const render::ScreenPos floor = render::tile_to_screen(row, col, baseHeight - 1, 2, camera.x, camera.y); CHECK(quad[0].x == floor.x + 16); CHECK(quad[0].y == floor.y); CHECK(quad[2].x == floor.x + 16); CHECK(quad[2].y == floor.y + 16); const ui::PickResult picked = ui::pick_tile(city, camera, (quad[0].x + quad[2].x) / 2, (quad[0].y + quad[2].y) / 2, nullptr, /*underground_floor=*/true); CHECK(picked.hit && picked.row == row && picked.col == col); }
// ---- picked_altitude tracks the renderer, NOT a global-sea clamp (synthetic, no assets) ---- { // Dry land whose GROUND is below the sea table: renderer draws it at ground 2, NOT clamped up to sea 8. City land; land.set_misc_be32(sc2k::misc::kWaterLevel, 8); for (int i = 0; i < City::kTiles; ++i) { land.xter.cells[i] = 0x00; land.altm.cells[i] = 2; } CHECK(ui::picked_altitude(land, 40, 40) == 2); // was max(2,8)=8 under the old clamp → misaligned CHECK(ui::picked_altitude(land, 40, 40) == render_alt(land, 40, 40));
// A WATER plateau whose SURFACE sits well above its sea floor: renderer uses the water-level field (bits 5-9). City sea; for (int i = 0; i < City::kTiles; ++i) { sea.xter.cells[i] = 0x10; sea.altm.cells[i] = std::uint16_t(2 | (12 << 5)); } CHECK(ui::picked_altitude(sea, 40, 40) == 12); CHECK(ui::picked_altitude(sea, 40, 40) == render_alt(sea, 40, 40));
// Clicking a tile's RENDERED screen centre resolves to that tile — proving pick and render agree. const render::Camera scam{640, 400}; for (const City* cp : {&land, &sea}) for (auto [r, col] : {std::pair{40, 40}, std::pair{63, 63}, std::pair{50, 70}}) { const render::ScreenPos p = render::tile_to_screen(r, col, render_alt(*cp, r, col), 2, scam.x, scam.y); const ui::PickResult pr = ui::pick_tile(*cp, scam, p.x + 16, p.y + 8); // + diamond-centre offset CHECK(pr.hit && pr.row == r && pr.col == col); } }
// ---- explicit discontinuity: upper top, lower top and the owned vertical face ---- { City stepped; for (std::uint16_t& altitude : stepped.altm.cells) altitude = 3; std::vector<terrain::Height> fabric(129 * 129, 3 * terrain::kHeightQuantum); const terrain::Corners upper = {6 * terrain::kHeightQuantum, 6 * terrain::kHeightQuantum, 6 * terrain::kHeightQuantum, 6 * terrain::kHeightQuantum}; const terrain::EdgeProfile high = {6 * terrain::kHeightQuantum, 6 * terrain::kHeightQuantum}; const terrain::EdgeProfile low = {3 * terrain::kHeightQuantum, 3 * terrain::kHeightQuantum}; std::vector<terrain::SurfacePatch> patches = {{{20, 20}, upper}}; std::vector<terrain::SplitEdge> edges = { {{terrain::EdgeAxis::Row, {20, 20}}, low, high, terrain::FaceMaterial::NaturalRock, terrain::FaceOwner::Positive}, {{terrain::EdgeAxis::Row, {21, 20}}, high, low, terrain::FaceMaterial::NaturalRock, terrain::FaceOwner::Negative}, {{terrain::EdgeAxis::Column, {20, 20}}, low, high, terrain::FaceMaterial::NaturalRock, terrain::FaceOwner::Positive}, {{terrain::EdgeAxis::Column, {20, 21}}, high, low, terrain::FaceMaterial::NaturalRock, terrain::FaceOwner::Negative}, }; const auto model = terrain::Model::create({128, 128}, std::move(fabric), std::move(patches), std::move(edges)); CHECK(model.has_value()); const auto cache = render::TerrainRenderCache::create(*model, stepped); CHECK(cache.has_value()); const render::Camera cam{900, 500}; const render::ScreenPos upper_centre = ui::tile_centre(stepped, cam, 20, 20, &*cache); const ui::PickResult upper_pick = ui::pick_tile( stepped, cam, upper_centre.x, upper_centre.y, &*cache); CHECK(upper_pick.hit && upper_pick.row == 20 && upper_pick.col == 20 && upper_pick.kind == ui::TerrainHitKind::Top); const render::ScreenPos lower_centre = ui::tile_centre(stepped, cam, 20, 21, &*cache); const ui::PickResult lower_pick = ui::pick_tile( stepped, cam, lower_centre.x, lower_centre.y, &*cache); CHECK(lower_pick.hit && lower_pick.row == 20 && lower_pick.col == 21 && lower_pick.kind == ui::TerrainHitKind::Top);
render::ScreenPos underground_quad[4]; ui::surface_quad(stepped, cam, 20, 20, underground_quad, 2, true, 1.f, &*cache, /*underground_floor=*/true); const ui::PickResult underground_pick = ui::pick_tile(stepped, cam, (underground_quad[0].x + underground_quad[2].x) / 2, (underground_quad[0].y + underground_quad[2].y) / 2, &*cache, /*underground_floor=*/true); CHECK(underground_pick.hit && underground_pick.row == 20 && underground_pick.col == 20 && underground_pick.kind == ui::TerrainHitKind::Top);
render::ScreenPos face[4]; CHECK(ui::terrain_face_quad(cam, 20, 20, cache->tile(20, 20)->column_face, false, face)); const int face_x = (3 * face[0].x + 3 * face[1].x + face[2].x + face[3].x) / 8; const int face_y = (3 * face[0].y + 3 * face[1].y + face[2].y + face[3].y) / 8; const ui::PickResult face_pick = ui::pick_tile(stepped, cam, face_x, face_y, &*cache); CHECK(face_pick.hit && face_pick.kind == ui::TerrainHitKind::ColumnFace); CHECK((face_pick.edge == terrain::EdgeCoord{terrain::EdgeAxis::Column, {20, 21}})); }
// ---- painter's-truth sweep on a synthetic steep cone (synthetic, no assets) ---- // Build a volcano-steep pyramid (1 level per tile, apex alt 16) through the game's own slope // autotiler, paint every tile's surface quad back-to-front into a visible-tile buffer (the exact // draw order + quad the renderer uses), then require pick_tile to return the visible tile for EVERY // pixel. Nearest-centre picking failed this on ridges — a hidden tile behind the ridge projects its // centre nearer than the visible face's centre ("highlight on the opposite side", user-reported). { City cone; for (int r = 0; r < City::kDim; ++r) for (int c = 0; c < City::kDim; ++c) { const int d = std::max(std::abs(r - 64), std::abs(c - 64)); cone.altm.at(r, c) = std::uint16_t(std::max(0, 16 - d)); // pyramid, 1 level/tile } detail::reslope_box(cone, 40, 40, 88, 88, 2); // derive XTER from ALTM long total = 0; const long wrong = painters_truth_sweep(cone, render::Camera{1200, 800}, 4200, 2200, total); std::printf("pick: cone painter's-truth sweep %ld pixels, %ld wrong\n", total, wrong); CHECK(total > 100000); // the sweep actually covered the cone CHECK(wrong == 0); // every pixel picks the VISIBLE tile long undergroundTotal = 0; const long undergroundWrong = painters_truth_sweep(cone, render::Camera{1200, 800}, 4200, 2200, undergroundTotal, /*underground_floor=*/true); CHECK(undergroundTotal > 100000); CHECK(undergroundWrong == 0); // lowered floor draw and inverse pick use the same geometry }
// Hilliest available city first (CAPE has terrain); fall back to any. const std::string cityPath = first_present_city({"CAPE.SC2", "TOKYO.SC2", "LONDON.SC2", "BAYVIEW.SC2"}); if (cityPath.empty()) { std::printf("pick: SKIP (no game data)\n"); return 0; }
City city; if (!io::load_sc2(cityPath, city).ok) { std::printf("pick: load failed\n"); return 1; }
// Painter's-truth sweep on real terrain: every visible pixel of the whole city — coasts, hills, // water, the map's corner tiles — must pick the tile whose drawn face is actually there. (This // subsumes the old projected-centre round-trip: a slope tile's flat centre can sit a pixel inside // the FRONT neighbour's diamond tip, where the neighbour is genuinely the visible surface.) { const render::Camera cam{2100, 420}; long total = 0; const long wrong = painters_truth_sweep(city, cam, 4400, 2400, total); std::printf("pick: %s painter's-truth sweep %ld pixels, %ld wrong\n", cityPath.c_str(), total, wrong); CHECK(total > 500000); // the sweep covered the whole city CHECK(wrong == 0); // every pixel picks the VISIBLE tile
// A pixel deep in the background (far from any tile) is a miss. const ui::PickResult off = ui::pick_tile(city, cam, cam.x + 100000, cam.y - 100000); CHECK(!off.hit); }
// ---- cube-top surface under a 2x2 elevated deck (user-reported, FOURCITI (66,24)) ---- // A highway ramp terrace alternates 0x0d cubes and slopes under the climbing deck. The cube's terrain // sprite always draws its top face one level up; the deck floats above on pillars with the cube fully // visible, so the hover surface is the cube TOP. Gating the lift on 1x1 features (the deck-sprite // rule) sank the highlight to the cube base — "acting as if the underlying tile was flat". if (exists(city_file("FOURCITI.SC2"))) { City fc; if (io::load_sc2(city_file("FOURCITI.SC2"), fc).ok) { CHECK(fc.xter.at(66, 24) == 0x0d && fc.xbld.at(66, 24) == 0x62); // guard: the reported terrain CHECK(ui::render_altitude(fc, 66, 24) == (fc.altm.at(66, 24) & 0x1F) + 1); // surface = cube TOP // …and the pick agrees: the centroid of the lifted quad resolves to this tile. const render::Camera cam{2100, 420}; render::ScreenPos v[4]; ui::surface_quad(fc, cam, 66, 24, v); const ui::PickResult pr = ui::pick_tile(fc, cam, (v[0].x + v[2].x) / 2, (v[0].y + v[2].y) / 2); CHECK(pr.hit && pr.row == 66 && pr.col == 24); std::printf("pick: FOURCITI (66,24) cube-under-deck surface at cube top, self-picks\n"); } }
if (failures == 0) { std::printf("pick: all checks passed\n"); return 0; } std::printf("pick: %d failure(s)\n", failures); return 1;}