| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { spriteCenterFromPivot, tilePivot, ySortLayer, flightSortLayer, Y_SORT_BASE } from "../../shared/render/pivot.js"; |
| 4 | import { CHUNK_SIZE, SPRITE_SIZE_PX, STATIC_CHUNK_RADIUS } from "../../shared/protocol/constants.js"; |
| 5 | |
| 6 | test("a 16×16 sprite pivot sits on the tile centre", () => |
| 7 | { |
| 8 | const pivot = tilePivot(0, 0, 16); |
| 9 | const center = spriteCenterFromPivot(pivot.x, pivot.y, 16, 1); |
| 10 | assert.equal(pivot.x, 8); |
| 11 | assert.equal(pivot.y, 8); |
| 12 | assert.equal(center.x, 8); |
| 13 | assert.equal(center.y, 8); |
| 14 | }); |
| 15 | |
| 16 | test("a taller sprite keeps its feet 8px from the bottom on the tile centre", () => |
| 17 | { |
| 18 | const pivot = tilePivot(0, 0, 16); |
| 19 | const center = spriteCenterFromPivot(pivot.x, pivot.y, 32, 1); |
| 20 | assert.equal(center.x, 8); |
| 21 | assert.equal(center.y, 0); |
| 22 | }); |
| 23 | |
| 24 | test("a 48px-tall tree sprite pivots 8px from its bottom on the tile centre", () => |
| 25 | { |
| 26 | const pivot = tilePivot(0, 0, 16); |
| 27 | const center = spriteCenterFromPivot(pivot.x, pivot.y, 48, 1); |
| 28 | assert.equal(center.x, 8); |
| 29 | assert.equal(center.y, -8); |
| 30 | }); |
| 31 | |
| 32 | test("Y-sort puts a southern tile in front of a northern tile", () => |
| 33 | { |
| 34 | assert.ok(ySortLayer(16, 1) > ySortLayer(0, 1)); |
| 35 | }); |
| 36 | |
| 37 | test("viewport-relative Y-sort stays below UI edge band at high world Y", () => |
| 38 | { |
| 39 | const layerEdge = 900_000; |
| 40 | const focusY = 50_000; |
| 41 | const zoomedTile = 64; |
| 42 | // Player around tileY ≈ 780 at zoom 4 — absolute sort used to exceed LAYER_EDGE. |
| 43 | const worldY = focusY + zoomedTile * 2; |
| 44 | const relative = worldY - focusY; |
| 45 | const layer = ySortLayer(relative, 3); |
| 46 | assert.ok(layer > Y_SORT_BASE); |
| 47 | assert.ok(layer < layerEdge, `expected ${layer} < ${layerEdge}`); |
| 48 | const absoluteWouldOverflow = Y_SORT_BASE + Math.round(worldY) * 16 + 3; |
| 49 | assert.ok(absoluteWouldOverflow > layerEdge); |
| 50 | }); |
| 51 | |
| 52 | test("chunk-quantised sort origin keeps the whole loaded ring inside the sort band", () => |
| 53 | { |
| 54 | // The client pins the sort origin to the top of the chunk row under the |
| 55 | // viewport centre, so it only moves when the view crosses a chunk border. |
| 56 | // Loaded tiles then span centre − radius … centre + radius + 1 chunk. |
| 57 | const layerEdge = 900_000; |
| 58 | const maxZoom = 4; |
| 59 | const chunkPx = CHUNK_SIZE * SPRITE_SIZE_PX * maxZoom; |
| 60 | const northMost = -STATIC_CHUNK_RADIUS * chunkPx; |
| 61 | const southMost = (STATIC_CHUNK_RADIUS + 1) * chunkPx; |
| 62 | const northLayer = ySortLayer(northMost, 0); |
| 63 | const southLayer = ySortLayer(southMost, 15); |
| 64 | assert.ok(northLayer > 0, `expected ${northLayer} > 0`); |
| 65 | assert.ok(southLayer < layerEdge, `expected ${southLayer} < ${layerEdge}`); |
| 66 | assert.ok(flightSortLayer(northMost, 0) > southLayer); |
| 67 | }); |
| 68 | |
| 69 | test("S-closed door stays in front of entity equipment overlays on the same tile", () => |
| 70 | { |
| 71 | const sortY = 128; |
| 72 | const entityBody = ySortLayer(sortY, 3); |
| 73 | const entityOverlay = ySortLayer(sortY, 3 + 1 + 5); |
| 74 | const doorFront = ySortLayer(sortY, 12); |
| 75 | const doorBehind = ySortLayer(sortY, 1); |
| 76 | assert.ok(doorFront > entityOverlay); |
| 77 | assert.ok(doorFront > entityBody); |
| 78 | assert.ok(entityBody > doorBehind); |
| 79 | assert.ok(ySortLayer(sortY, 14) > doorFront); |
| 80 | }); |
| 81 | |
| 82 | test("flight sort stays above any ground y-sort in view", () => |
| 83 | { |
| 84 | // In-view relative sortY stays within roughly ±screen height in world px. |
| 85 | const farInView = ySortLayer(2_000, 3); |
| 86 | assert.ok(flightSortLayer(0, 0) > farInView); |
| 87 | assert.ok(flightSortLayer(16, 1) > flightSortLayer(0, 1)); |
| 88 | assert.ok(farInView < 900_000); |
| 89 | }); |