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.
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
// Pixel probes for the client e2e (TESTING 10.3). A probe is the colour the browser actually shows
|
||||
// at one CSS point: Playwright screenshots a 3x3 CSS-pixel clip and this module decodes the PNG and
|
||||
// averages it. No renderer internals, no WebGL readPixels, no preserveDrawingBuffer.
|
||||
//
|
||||
// The decoder is intentionally small: Chromium screenshots are non-interlaced 8-bit truecolour
|
||||
// (RGB or RGBA), which is all a screenshot can be. Anything else is a programmer error here, not a
|
||||
// browser case to support.
|
||||
import zlib from 'node:zlib'
|
||||
|
||||
const SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
|
||||
|
||||
function paeth(a, b, c) {
|
||||
const p = a + b - c
|
||||
const pa = Math.abs(p - a)
|
||||
const pb = Math.abs(p - b)
|
||||
const pc = Math.abs(p - c)
|
||||
if (pa <= pb && pa <= pc) return a
|
||||
if (pb <= pc) return b
|
||||
return c
|
||||
}
|
||||
|
||||
/** Decode an 8-bit, non-interlaced RGB/RGBA PNG into `{ width, height, data }` (RGBA bytes). */
|
||||
export function decodePng(buffer) {
|
||||
if (!Buffer.isBuffer(buffer) || !buffer.subarray(0, 8).equals(SIGNATURE)) {
|
||||
throw new Error('not a PNG')
|
||||
}
|
||||
let offset = 8
|
||||
let width = 0
|
||||
let height = 0
|
||||
let channels = 0
|
||||
const idat = []
|
||||
while (offset + 8 <= buffer.length) {
|
||||
const length = buffer.readUInt32BE(offset)
|
||||
const type = buffer.toString('latin1', offset + 4, offset + 8)
|
||||
const body = buffer.subarray(offset + 8, offset + 8 + length)
|
||||
offset += 12 + length
|
||||
if (type === 'IHDR') {
|
||||
width = body.readUInt32BE(0)
|
||||
height = body.readUInt32BE(4)
|
||||
const depth = body[8]
|
||||
const colorType = body[9]
|
||||
const interlace = body[12]
|
||||
if (depth !== 8 || interlace !== 0) throw new Error(`unsupported PNG depth/interlace ${depth}/${interlace}`)
|
||||
if (colorType === 2) channels = 3
|
||||
else if (colorType === 6) channels = 4
|
||||
else throw new Error(`unsupported PNG colour type ${colorType}`)
|
||||
} else if (type === 'IDAT') {
|
||||
idat.push(body)
|
||||
} else if (type === 'IEND') {
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!width || !height || !channels) throw new Error('PNG without IHDR/IDAT')
|
||||
|
||||
const raw = zlib.inflateSync(Buffer.concat(idat))
|
||||
const stride = width * channels
|
||||
const pixels = Buffer.alloc(stride * height)
|
||||
for (let y = 0; y < height; y++) {
|
||||
const filter = raw[y * (stride + 1)]
|
||||
const line = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1))
|
||||
const out = pixels.subarray(y * stride, (y + 1) * stride)
|
||||
const prev = y > 0 ? pixels.subarray((y - 1) * stride, y * stride) : null
|
||||
for (let x = 0; x < stride; x++) {
|
||||
const a = x >= channels ? out[x - channels] : 0
|
||||
const b = prev ? prev[x] : 0
|
||||
const c = prev && x >= channels ? prev[x - channels] : 0
|
||||
let value = line[x]
|
||||
switch (filter) {
|
||||
case 0:
|
||||
break
|
||||
case 1:
|
||||
value = (value + a) & 0xff
|
||||
break
|
||||
case 2:
|
||||
value = (value + b) & 0xff
|
||||
break
|
||||
case 3:
|
||||
value = (value + ((a + b) >> 1)) & 0xff
|
||||
break
|
||||
case 4:
|
||||
value = (value + paeth(a, b, c)) & 0xff
|
||||
break
|
||||
default:
|
||||
throw new Error(`unsupported PNG filter ${filter}`)
|
||||
}
|
||||
out[x] = value
|
||||
}
|
||||
}
|
||||
|
||||
if (channels === 4) return { width, height, data: pixels }
|
||||
const rgba = Buffer.alloc(width * height * 4)
|
||||
for (let i = 0, j = 0; i < pixels.length; i += 3, j += 4) {
|
||||
rgba[j] = pixels[i]
|
||||
rgba[j + 1] = pixels[i + 1]
|
||||
rgba[j + 2] = pixels[i + 2]
|
||||
rgba[j + 3] = 255
|
||||
}
|
||||
return { width, height, data: rgba }
|
||||
}
|
||||
|
||||
/** Average every pixel of a decoded image (the probe clip is small and uniform). */
|
||||
export function averageColour(image) {
|
||||
let r = 0
|
||||
let g = 0
|
||||
let b = 0
|
||||
let a = 0
|
||||
const count = image.width * image.height
|
||||
for (let i = 0; i < image.data.length; i += 4) {
|
||||
r += image.data[i]
|
||||
g += image.data[i + 1]
|
||||
b += image.data[i + 2]
|
||||
a += image.data[i + 3]
|
||||
}
|
||||
return { r: r / count, g: g / count, b: b / count, a: a / count }
|
||||
}
|
||||
|
||||
/**
|
||||
* The colour shown at CSS point `(x, y)`: a 3x3 CSS-pixel screenshot clip, decoded and averaged.
|
||||
* Screenshots are in device pixels, so DPR is handled by the browser, not by this helper.
|
||||
*/
|
||||
export async function probe(page, x, y) {
|
||||
const px = Math.max(0, Math.round(x) - 1)
|
||||
const py = Math.max(0, Math.round(y) - 1)
|
||||
const buffer = await page.screenshot({ clip: { x: px, y: py, width: 3, height: 3 } })
|
||||
return averageColour(decodePng(buffer))
|
||||
}
|
||||
|
||||
/** Mean absolute channel difference, for "did this area change" assertions. */
|
||||
export function differs(a, b, tolerance = 12) {
|
||||
return (
|
||||
Math.abs(a.r - b.r) > tolerance ||
|
||||
Math.abs(a.g - b.g) > tolerance ||
|
||||
Math.abs(a.b - b.b) > tolerance
|
||||
)
|
||||
}
|
||||
|
||||
export function luminance({ r, g, b }) {
|
||||
return 0.2126 * r + 0.7152 * g + 0.0722 * b
|
||||
}
|
||||
Reference in New Issue
Block a user