The GM survey view asked row-major, at most 8 at a time, rebuilt the whole wanted window on every settle, and paid two requests for every failed tile (image error, then a fetch to learn the status). A 404/5xx was stored as permanent `denied`, so a tile imported after page load stayed black until a page reload, and `viewport.setScene` swallowed a same-id art_version change so the old textures were never dropped. - TILE_CONCURRENCY 8 -> 32, the measured knee on a Full HD scene (min-of-3 headless: GM fit + player 0.5, tile 32/64; 64 is inside the noise). - orderTilesCentreOut: nearest the viewport centre first. - Settle path is incremental (one settle = one tile sprite/flag update); the window is rebuilt only on camera/scene changes. - fetch + createImageBitmap: status from the same response, decode off the main thread, so a failure costs one request. - 403 stays permanent (retried only on fog.fresh); 404/5xx/network retry with 250/500 ms backoff up to 3 loads, and `hello`/reconnect or a scene/art_version change resets the budget. A failure that lands after a reconnect is a fresh first attempt; a failure that leaves the window does not spend the budget. - setScene emits for a same-id scene; a new art_version drops the texture cache and reschedules. Tiles are `Cache-Control: private, max-age=1y, immutable` (still gated), so a reload reuses them; DESIGN 3.3/11.1 and client/README updated. Tests: vitest for ordering, incremental invariants, retry budget/backoff, reconnect epoch and art_version invalidation; e2e for a GM survey fill (one request per succeeded tile) and a 404 tile recovering after the file appears and a real reconnect. Before -> after (min-of-3, saturated host, Full HD): GM fit 32px 910->843 ms, player 0.5 32px 972->862 ms, GM fit 64px 320->249 ms, player 0.5 64px 332->368 ms (noise). Part of #63. Blocks #77.
230 lines
9.0 KiB
TypeScript
230 lines
9.0 KiB
TypeScript
// Camera and viewport maths (#69, DESIGN 7.1/7.2, ADR-0013). Pure: no Pixi, no DOM, so this is
|
|
// the same cheap seam as the reducer. The numbers here are the contract: players clamp at 0.5 and
|
|
// the GM has no low clamp; both stop at 4.0; a viewport asks for its tiles plus exactly one tile of
|
|
// margin, clamped to the scene.
|
|
import { describe, expect, it } from 'vitest'
|
|
import type { SceneInfo } from '../src/protocol.ts'
|
|
import {
|
|
GM_MIN_ZOOM,
|
|
MAX_ZOOM,
|
|
PLAYER_MIN_ZOOM,
|
|
VIEWPORT_MARGIN_TILES,
|
|
ViewportModel,
|
|
clampZoom,
|
|
centredCamera,
|
|
orderTilesCentreOut,
|
|
pinchCamera,
|
|
screenToWorld,
|
|
tileUrl,
|
|
viewportCentre,
|
|
visibleTiles,
|
|
worldToScreen,
|
|
zoomCamera,
|
|
} from '../src/viewport.ts'
|
|
|
|
const scene: SceneInfo = {
|
|
id: 1,
|
|
name: 'Scene',
|
|
w: 4096,
|
|
h: 4096,
|
|
tile_size: 32,
|
|
cell: 32,
|
|
grid_size: 32,
|
|
vision_radius_default: 320,
|
|
token_radius_default: 20,
|
|
art_version: 1,
|
|
}
|
|
|
|
const size = { width: 320, height: 320 }
|
|
|
|
describe('zoom clamps (ADR-0013, spec #63 decision 7)', () => {
|
|
it('a player stops exactly at 0.5 and 4.0', () => {
|
|
expect(PLAYER_MIN_ZOOM).toBe(0.5)
|
|
expect(MAX_ZOOM).toBe(4)
|
|
expect(clampZoom('player', 0.4)).toBe(0.5)
|
|
expect(clampZoom('player', 0.5)).toBe(0.5)
|
|
expect(clampZoom('player', 1)).toBe(1)
|
|
expect(clampZoom('player', 4)).toBe(4)
|
|
expect(clampZoom('player', 4.1)).toBe(4)
|
|
})
|
|
|
|
it('the GM has no player low clamp, but the same 4.0 ceiling', () => {
|
|
expect(GM_MIN_ZOOM).toBeLessThan(0.5)
|
|
expect(clampZoom('gm', 0.25)).toBe(0.25)
|
|
expect(clampZoom('gm', 0.1)).toBe(0.1)
|
|
expect(clampZoom('gm', GM_MIN_ZOOM)).toBe(GM_MIN_ZOOM)
|
|
expect(clampZoom('gm', 0.0001)).toBe(GM_MIN_ZOOM)
|
|
expect(clampZoom('gm', 4.1)).toBe(4)
|
|
})
|
|
|
|
it('an unknown or non-finite zoom falls back to 1', () => {
|
|
expect(clampZoom(null, Number.NaN)).toBe(1)
|
|
expect(clampZoom('player', Number.POSITIVE_INFINITY)).toBe(4)
|
|
})
|
|
})
|
|
|
|
describe('camera', () => {
|
|
it('centres on the scene at zoom 1', () => {
|
|
const camera = centredCamera(scene, size)
|
|
expect(camera.zoom).toBe(1)
|
|
expect(camera.x + (scene.w / 2) * camera.zoom).toBe(size.width / 2)
|
|
expect(camera.y + (scene.h / 2) * camera.zoom).toBe(size.height / 2)
|
|
})
|
|
|
|
it('screenToWorld and worldToScreen are inverses', () => {
|
|
const camera = { x: -100, y: -200, zoom: 2 }
|
|
const world = screenToWorld(camera, 40, 80)
|
|
expect(world).toEqual([70, 140])
|
|
const screen = worldToScreen(camera, world[0], world[1])
|
|
expect(screen[0]).toBeCloseTo(40, 10)
|
|
expect(screen[1]).toBeCloseTo(80, 10)
|
|
})
|
|
|
|
it('viewportCentre is the world point under the middle of the screen (#82)', () => {
|
|
expect(viewportCentre(centredCamera(scene, size), size)).toEqual([2048, 2048])
|
|
expect(viewportCentre({ x: -100, y: -200, zoom: 2 }, size)).toEqual([130, 180])
|
|
})
|
|
|
|
it('zoomCamera keeps the world point under the anchor fixed and clamps', () => {
|
|
const camera = { x: -1000, y: -800, zoom: 1 }
|
|
const anchor = { x: 120, y: 200 }
|
|
const before = screenToWorld(camera, anchor.x, anchor.y)
|
|
const zoomed = zoomCamera('player', camera, 0.1, anchor)
|
|
expect(zoomed.zoom).toBe(0.5)
|
|
const after = screenToWorld(zoomed, anchor.x, anchor.y)
|
|
expect(after[0]).toBeCloseTo(before[0], 10)
|
|
expect(after[1]).toBeCloseTo(before[1], 10)
|
|
expect(zoomCamera('player', camera, 100, anchor).zoom).toBe(4)
|
|
})
|
|
|
|
it('pinch zooms around the starting midpoint and follows it as it moves', () => {
|
|
const start = { x: -1000, y: -800, zoom: 1 }
|
|
const startMid = { x: 100, y: 100 }
|
|
const world = screenToWorld(start, startMid.x, startMid.y)
|
|
const movedMid = { x: 140, y: 120 }
|
|
const pinched = pinchCamera('player', start, 100, 200, startMid, movedMid)
|
|
expect(pinched.zoom).toBe(2)
|
|
const atMoved = screenToWorld(pinched, movedMid.x, movedMid.y)
|
|
expect(atMoved[0]).toBeCloseTo(world[0], 10)
|
|
expect(atMoved[1]).toBeCloseTo(world[1], 10)
|
|
expect(pinchCamera('player', start, 100, 1, startMid, startMid).zoom).toBe(0.5)
|
|
})
|
|
})
|
|
|
|
describe('ViewportModel (the render layer\'s UI state)', () => {
|
|
it('pans, toggles the grid and re-clamps when the role becomes a player', () => {
|
|
const model = new ViewportModel()
|
|
model.reset(scene, 'gm', size)
|
|
model.pan(10, -20)
|
|
expect(model.state.camera.x).toBe(centredCamera(scene, size).x + 10)
|
|
expect(model.state.camera.y).toBe(centredCamera(scene, size).y - 20)
|
|
expect(model.state.grid).toBe(true)
|
|
model.toggleGrid()
|
|
expect(model.state.grid).toBe(false)
|
|
// A GM zoomed to 0.25; the same view cannot stay below the player floor.
|
|
model.zoomAt(0.25, { x: 0, y: 0 })
|
|
expect(model.state.camera.zoom).toBe(0.25)
|
|
model.setRole('player')
|
|
expect(model.state.camera.zoom).toBe(0.5)
|
|
})
|
|
|
|
it('keeps the camera across a same-scene resync and resets for a new scene', () => {
|
|
const model = new ViewportModel()
|
|
model.reset(scene, 'player', size)
|
|
model.pan(-40, 0)
|
|
const panned = model.state.camera
|
|
model.setScene({ ...scene })
|
|
expect(model.state.camera).toBe(panned)
|
|
model.setScene({ ...scene, id: 2 })
|
|
expect(model.state.camera).toEqual(centredCamera({ ...scene, id: 2 }, size))
|
|
})
|
|
|
|
it('emits on a same-scene resync so a new art_version is not swallowed (#86)', () => {
|
|
const model = new ViewportModel()
|
|
let seen = 0
|
|
model.reset(scene, 'player', size)
|
|
model.subscribe(() => {
|
|
seen += 1
|
|
})
|
|
const panned = model.state.camera
|
|
model.setScene({ ...scene, art_version: 2 })
|
|
expect(seen).toBe(1)
|
|
expect(model.state.scene?.art_version).toBe(2)
|
|
expect(model.state.camera).toBe(panned)
|
|
// The same object again is not a change: no extra notification.
|
|
model.setScene(model.state.scene)
|
|
expect(seen).toBe(1)
|
|
})
|
|
})
|
|
|
|
describe('centre-out tile order (#86)', () => {
|
|
it('asks for the tile under the viewport centre first, growing outwards', () => {
|
|
const camera = centredCamera(scene, size)
|
|
const tiles = orderTilesCentreOut(visibleTiles(scene, camera, size), scene, camera, size)
|
|
// 320x320 viewport at zoom 1 centred on world (2048,2048): the four middle tiles tie.
|
|
expect(tiles.length).toBe(12 * 12)
|
|
const distance = (tile: { x: number; y: number }) =>
|
|
Math.hypot((tile.x + 0.5) * scene.tile_size - 2048, (tile.y + 0.5) * scene.tile_size - 2048)
|
|
const distances = tiles.map(distance)
|
|
expect(distances).toEqual([...distances].sort((a, b) => a - b))
|
|
expect(Math.abs((tiles[0]?.x ?? -1) - 63)).toBeLessThanOrEqual(1)
|
|
expect(Math.abs((tiles[0]?.y ?? -1) - 63)).toBeLessThanOrEqual(1)
|
|
// It is a permutation of the row-major window, not a different window.
|
|
expect(new Set(tiles.map(tile => tile.url)).size).toBe(12 * 12)
|
|
})
|
|
})
|
|
|
|
describe('visible tiles', () => {
|
|
it('returns the viewport plus one tile of margin, row-major, inside the scene', () => {
|
|
const camera = centredCamera(scene, size)
|
|
const tiles = visibleTiles(scene, camera, size)
|
|
// 320 px at zoom 1 is 10 tiles; one margin on each side is 12 x 12.
|
|
expect(tiles.length).toBe(12 * 12)
|
|
expect(tiles[0]).toMatchObject({ x: 58, y: 58 })
|
|
expect(tiles[tiles.length - 1]).toMatchObject({ x: 69, y: 69 })
|
|
expect(tiles[0]?.url).toBe('/tiles/1/v1/58_58.png')
|
|
expect(tiles.every(tile => tile.x >= 0 && tile.x < 128 && tile.y >= 0 && tile.y < 128)).toBe(true)
|
|
// row-major: every tile of a row comes before the next row.
|
|
const ys = tiles.map(tile => tile.y)
|
|
expect(ys).toEqual([...ys].sort((a, b) => a - b))
|
|
})
|
|
|
|
it('clamps the margin at the scene edges', () => {
|
|
const camera = { x: 0, y: 0, zoom: 1 }
|
|
const tiles = visibleTiles(scene, camera, size)
|
|
expect(tiles[0]).toMatchObject({ x: 0, y: 0 })
|
|
// 320 px + one tile margin to the right/down => 0..10 inclusive.
|
|
expect(Math.max(...tiles.map(tile => tile.x))).toBe(10)
|
|
expect(Math.max(...tiles.map(tile => tile.y))).toBe(10)
|
|
})
|
|
|
|
it('scales with zoom (out asks for more, in for fewer)', () => {
|
|
const out = visibleTiles(scene, centredCamera(scene, size, 0.5), size)
|
|
const inn = visibleTiles(scene, centredCamera(scene, size, 4), size)
|
|
expect(out.length).toBeGreaterThan(inn.length)
|
|
// At 0.5 a 320 px viewport is 640 world px: 20 tiles + 2 margin = 22 per axis.
|
|
expect(out.length).toBe(22 * 22)
|
|
})
|
|
|
|
it('never prefetches beyond the margin', () => {
|
|
const camera = centredCamera(scene, size)
|
|
const tiles = visibleTiles(scene, camera, size)
|
|
const maxX = Math.max(...tiles.map(tile => tile.x))
|
|
const minX = Math.min(...tiles.map(tile => tile.x))
|
|
const viewport = visibleTiles(scene, camera, { width: 320, height: 320 })
|
|
// Calling twice is deterministic; the margin is exactly one tile (VIEWPORT_MARGIN_TILES).
|
|
expect(VIEWPORT_MARGIN_TILES).toBe(1)
|
|
expect(maxX - minX).toBe(11)
|
|
expect(viewport.length).toBe(tiles.length)
|
|
})
|
|
|
|
it('returns nothing when the camera looks outside the map', () => {
|
|
expect(visibleTiles(scene, { x: 100000, y: 100000, zoom: 1 }, size)).toEqual([])
|
|
})
|
|
|
|
it('builds the canonical tile URL', () => {
|
|
expect(tileUrl(scene, 0, 0)).toBe('/tiles/1/v1/0_0.png')
|
|
expect(tileUrl(scene, 127, 3)).toBe('/tiles/1/v1/127_3.png')
|
|
})
|
|
})
|