Files
FFF_vtt_frozen/client/test/fog.test.ts
T
SirStone 2d9548c941 feat(client): fog explored bitmap, vision cut-out and player markers (#70)
The fog layer (DESIGN 7.1 layer 4, spec #63 decision 9) keeps the explored
grid in one cell-resolution RGBA canvas: never-seen cells opaque black,
explored cells black at 0.6 alpha. A second canvas is the sprite texture:
the explored canvas is copied into it and the union of the server's vision
polygons is cut out with `destination-out`. A full explored snapshot
repaints; a cell diff mutates only the new bytes and uploads the dirty
rectangle. Player markers from the marker set/del frames are sprites above
the fog; the GM layer hides the fog.

The cut-out is baked instead of using a Graphics stencil mask: on the e2e's
software renderer the stencil state cost ~350 ms of the phone-class viewport
fill budget (412x869 dpr 2.625), while baking keeps the fill at the pre-fog
baseline. The cut-out is rasterized at cell resolution, the same grid the
server sends.

Integration fix in the tile layer: a tile denied with 403 because it was
unexplored is asked once more when the fog says its cells became explored;
unexplored denials still never retry (the map 403 test is unchanged).

Proven at the reducer seam (fog slice) and by screenshot pixel probes in the
browser e2e: black/dim/visible, window see-through, one-way blind side,
secret door, marker anchor and retraction, a drag that moves the fog with no
reload, and a reconnect resync. Fog update cost recorded under the 412x869
dpr 2.625 / 4x CPU emulation.
2026-11-21 10:44:32 +01:00

203 lines
7.2 KiB
TypeScript

// The fog slice (T7, #70; DESIGN 6.1 `explored`/`vision`/`marker`, 7.3). These are reducer-seam
// tests: server frames in, state out. The pixels themselves are proven in the browser e2e
// (`client/e2e/fog.test.mjs`), not here.
import { describe, expect, it } from 'vitest'
import { initialState, reduce } from '../src/reducer.ts'
import type { ReducerEvent, RootState } from '../src/reducer.ts'
import { parseServerFrame } from '../src/protocol.ts'
import { fogOf } from '../src/slices/fog.ts'
import type { FogState } from '../src/slices/fog.ts'
const scene = {
id: 1,
name: 'Cellar',
w: 4096,
h: 4096,
tile_size: 32,
cell: 32,
grid_size: 64,
vision_radius_default: 320,
token_radius_default: 20,
art_version: 1,
}
const frame = (body: Record<string, unknown>): ReducerEvent => {
const parsed = parseServerFrame(JSON.stringify(body))
if (!parsed) throw new Error(`bad test frame ${JSON.stringify(body)}`)
return { type: 'frame', frame: parsed }
}
const hello = (n: number): ReducerEvent =>
frame({ t: 'hello', n, proto: 2, role: 'player', me: { id: 2, name: 'Ann' }, scene })
const run = (...events: ReducerEvent[]): RootState =>
events.reduce((state, event) => reduce(state, event), initialState())
const fogAfter = (...events: ReducerEvent[]): FogState => fogOf(run(hello(1), ...events))
describe('the explored grid comes from hello.scene (6.1)', () => {
it('is empty until hello, then sized in cells', () => {
const before = fogOf(initialState())
expect(before.cols).toBe(0)
expect(before.sceneId).toBeNull()
const fog = fogAfter()
expect(fog.cols).toBe(128)
expect(fog.rows).toBe(128)
expect(fog.cell).toBe(32)
expect(fog.sceneId).toBe(1)
expect(fog.cells.size).toBe(0)
expect(fog.exploredVersion).toBe(0)
})
})
describe('explored full and diff frames (6.1)', () => {
it('a full RLE snapshot answers every explored cell and marks the update a snapshot', () => {
// Row cy=2: x=5..7 and x=10..11 (runs are [x0, len] pairs).
const fog = fogAfter(frame({ t: 'explored', n: 2, full: true, rle: [[2, 5, 3, 10, 2]] }))
const base = 2 * 128
expect([...fog.cells].sort((a, b) => a - b)).toEqual([
base + 5,
base + 6,
base + 7,
base + 10,
base + 11,
])
expect(fog.fresh).toHaveLength(5)
expect(fog.snapshot).toBe(true)
expect(fog.exploredVersion).toBe(1)
})
it('a diff adds only new cells and reports exactly those as fresh', () => {
const fog = fogAfter(
frame({ t: 'explored', n: 2, full: true, rle: [[2, 5, 2]] }),
frame({
t: 'explored',
n: 3,
full: false,
cells: [
[5, 2], // explored already: not fresh again
[6, 2], // explored already
[7, 2], // new
],
}),
)
expect(fog.cells.size).toBe(3)
expect(fog.fresh).toEqual([2 * 128 + 7])
expect(fog.snapshot).toBe(false)
expect(fog.exploredVersion).toBe(2)
})
it('a diff with nothing new changes no version and reports no fresh cells', () => {
const fog = fogAfter(
frame({ t: 'explored', n: 2, full: true, rle: [[2, 5, 2]] }),
frame({ t: 'explored', n: 3, full: false, cells: [[5, 2], [6, 2]] }),
)
expect(fog.cells.size).toBe(2)
expect(fog.fresh).toEqual([])
expect(fog.exploredVersion).toBe(1)
})
it('ignores cells outside the grid and malformed frames without half-applying them', () => {
const fog = fogAfter(
frame({ t: 'explored', n: 2, full: true, rle: [[2, 5, 2], [999, 0, 3], [3, -4, 2]] }),
frame({ t: 'explored', n: 3, full: false, cells: [[-1, 0], [128, 0], [0, 128], ['x', 0]] }),
frame({ t: 'explored', n: 4, full: false, cells: 'nope' }),
)
expect(fog.cells.size).toBe(2)
expect(fog.exploredVersion).toBe(1)
})
})
describe('vision polygons (6.1)', () => {
it('keeps this player\'s polygons and drops malformed ones whole', () => {
const fog = fogAfter(
frame({
t: 'vision',
n: 2,
polys: [
{ id: 'tok-1', pts: [[10, 10], [20, 10], [20, 20]] },
{ id: 'short', pts: [[0, 0], [1, 1]] },
{ id: 'bad-point', pts: [[0, 0], [1, 'x'], [2, 2]] },
{ pts: [[0, 0], [1, 1], [2, 0]] },
],
}),
)
expect(fog.polys).toEqual([{ id: 'tok-1', pts: [[10, 10], [20, 10], [20, 20]] }])
expect(fog.visionVersion).toBe(1)
})
it('a later vision frame replaces the union (a token leaving sight is gone)', () => {
const fog = fogAfter(
frame({ t: 'vision', n: 2, polys: [{ id: 'a', pts: [[0, 0], [9, 0], [9, 9]] }] }),
frame({ t: 'vision', n: 3, polys: [] }),
)
expect(fog.polys).toEqual([])
expect(fog.visionVersion).toBe(2)
})
})
describe('player markers (5.8, 6.1)', () => {
it('a full set replaces the whole memory', () => {
const fog = fogAfter(
frame({ t: 'marker', n: 2, full: true, set: [{ id: 'm1', pos: [100, 200] }] }),
frame({ t: 'marker', n: 3, full: false, set: [{ id: 'm2', pos: [300, 400] }] }),
frame({ t: 'marker', n: 4, full: true, set: [{ id: 'm3', pos: [500, 600] }] }),
)
expect(fog.markers).toEqual({ m3: [500, 600] })
})
it('a diff adds and retracts by id; an anchor change moves the same id', () => {
const fog = fogAfter(
frame({ t: 'marker', n: 2, full: true, set: [{ id: 'm1', pos: [100, 200] }] }),
frame({ t: 'marker', n: 3, full: false, set: [{ id: 'm2', pos: [300, 400] }] }),
frame({ t: 'marker', n: 4, full: false, set: [{ id: 'm1', pos: [110, 200] }] }),
frame({ t: 'marker', n: 5, full: false, set: [], del: ['m2', 'never-seen'] }),
)
expect(fog.markers).toEqual({ m1: [110, 200] })
expect(fog.markerVersion).toBe(4)
})
it('a diff that changes nothing does not bump the version', () => {
const fog = fogAfter(
frame({ t: 'marker', n: 2, full: false, set: [{ id: 'm1', pos: [100, 200] }] }),
frame({ t: 'marker', n: 3, full: false, set: [{ id: 'm1', pos: [100, 200] }] }),
frame({ t: 'marker', n: 4, full: false, set: [], del: [] }),
)
expect(fog.markerVersion).toBe(1)
})
})
describe('reset and the GM (6.3)', () => {
it('a second hello clears explored, vision and markers and rebuilds the grid', () => {
const reset = run(
hello(1),
frame({ t: 'explored', n: 2, full: true, rle: [[2, 5, 2]] }),
frame({ t: 'vision', n: 3, polys: [{ id: 'a', pts: [[0, 0], [9, 0], [9, 9]] }] }),
frame({ t: 'marker', n: 4, full: true, set: [{ id: 'm1', pos: [1, 2] }] }),
hello(1),
)
const fog = fogOf(reset)
expect(fog.cols).toBe(128)
expect(fog.cells.size).toBe(0)
expect(fog.fresh).toEqual([])
expect(fog.polys).toEqual([])
expect(fog.markers).toEqual({})
expect(fog.exploredVersion).toBe(0)
expect(fog.visionVersion).toBe(0)
expect(fog.markerVersion).toBe(0)
})
it('a GM connection receives no fog frames, so its fog state stays empty', () => {
const state = run(
frame({ t: 'hello', n: 1, proto: 2, role: 'gm', me: { id: 1, name: 'GM' }, scene }),
frame({ t: 'roster', n: 2, players: [] }),
)
const fog = fogOf(state)
expect(fog.cols).toBe(128)
expect(fog.cells.size).toBe(0)
expect(fog.polys).toEqual([])
expect(fog.markers).toEqual({})
})
})