// Numeric verification of sprite-mask world picking: transparent pixels pass through, opaque pixels // claim in reverse painter order, flips/scales map back to the right source texel, and an unowned visual // blocks structures behind it. #include "sc2k/render/world_hit.hpp" #include using namespace sc2k::render; #include "test_harness.hpp" int main() { Sprite back; back.width = 3; back.height = 2; back.index.assign(6, 1); back.mask = {1, 1, 1, 1, 1, 1}; Sprite front; front.width = 2; front.height = 2; front.index.assign(4, 2); front.mask = {1, 0, 1, 1}; WorldHitIndex hits; hits.add(back, 10, 20, false, 1, 4, 5); hits.add(front, 11, 20, false, 1, 8, 9); // later painter command overlaps the back sprite WorldHit hit = hits.hit_test(11.25, 20.25); CHECK(hit.hit && hit.row == 8 && hit.col == 9); // opaque front pixel wins hit = hits.hit_test(12.25, 20.25); CHECK(hit.hit && hit.row == 4 && hit.col == 5); // transparent front pixel passes through hit = hits.hit_test_with_hysteresis(12.25, 20.25, 8, 9, 0.5); CHECK(hit.hit && hit.row == 8 && hit.col == 9); // current front owner remains inside its edge halo hit = hits.hit_test_with_hysteresis(12.25, 20.25, 8, 9, 0.2); CHECK(hit.hit && hit.row == 4 && hit.col == 5); // outside the halo it releases to the exact back pixel CHECK(!hits.hit_test(9.99, 20.0).hit); // half-open left bound CHECK(!hits.hit_test(13.0, 20.0).hit); // half-open right bound Sprite blocker; blocker.width = 1; blocker.height = 1; blocker.index = {3}; blocker.mask = {1}; hits.add(blocker, 12, 20, false, 1, -1, -1); // genuinely later opaque pixels still occlude hit = hits.hit_test_with_hysteresis(12.25, 20.25, 8, 9, 0.5); CHECK(hit.hit && hit.row == -1 && hit.col == -1); hits.clear(); hits.add(back, 10, 20, false, 1, 4, 5); hits.add(front, 11, 20, false, 1, -1, -1); // a Thing/effect is a non-selectable blocker hit = hits.hit_test(11.25, 20.25); CHECK(hit.hit && hit.row == -1 && hit.col == -1); // do not select the structure behind it hits.clear(); hits.add(back, 10, 20, false, 1, 4, 5); hits.add(front, 11, 20, false, 1, 8, 9, /*resolvesObject=*/false); hit = hits.hit_test_with_hysteresis(11.25, 20.25, 8, 9, 0.5); CHECK(hit.hit && hit.row == 8 && hit.col == 9 && !hit.resolves_object); // report the blocker tile without selecting its buried object hits.clear(); hits.add(front, 11, 20, false, 1, 8, 9, /*resolvesObject=*/true, /*geometricTerrain=*/true); hit = hits.hit_test(11.25, 20.25); CHECK(hit.hit && hit.row == 8 && hit.col == 9 && hit.geometric_terrain); // caller can defer ordinary ground to its geometric picker hits.clear(); hits.add(front, 30, 40, true, 2, 7, 6); hit = hits.hit_test(30.25, 40.25); // flipped destination x=0 samples source x=1 (transparent) CHECK(!hit.hit); hit = hits.hit_test(32.25, 40.25); // destination x=1 samples opaque source x=0 CHECK(hit.hit && hit.row == 7 && hit.col == 6); Sprite rgba; rgba.width = 1; rgba.height = 1; rgba.mask = {1}; rgba.rgba = {Rgba{255, 255, 255, 0}}; hits.clear(); hits.add(rgba, 0, 0, false, 1, 1, 1); CHECK(!hits.hit_test(0.5, 0.5).hit); // authored zero-alpha pixels do not claim if (failures == 0) { std::printf("world_hit: all checks passed\n"); return 0; } std::printf("world_hit: %d failure(s)\n", failures); return 1; }