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:
2026-11-21 10:44:32 +01:00
parent d225e956ca
commit 2d9548c941
8 changed files with 1423 additions and 6 deletions
+45 -1
View File
@@ -9,7 +9,12 @@
// - a `403`/`404` is a permanent `denied` answer: never retried in a loop (the server said no);
// - a transient `error` is retried only when a later viewport sync asks for the tile again;
// - queued tiles that leave the viewport before they start are dropped, so a fast pan never
// prefetches beyond the margin.
// prefetches beyond the margin;
// - a denied tile is asked again only when the fog says its cells just became explored (#70):
// the gate's answer changed, so the old "no" is stale.
import type { SceneInfo } from './protocol.ts'
import { tileUrl } from './viewport.ts'
export const TILE_CONCURRENCY = 8
@@ -29,6 +34,28 @@ function sameList(a: readonly string[], b: readonly string[]): boolean {
return true
}
/**
* The tile urls touched by a set of explored cells (fog slice indices, row-major). v1 has
* `cell == tile_size` (DESIGN 8), but the maths keeps a cell-to-tile mapping general.
*/
export function tilesForCells(
scene: Pick<SceneInfo, 'id' | 'art_version' | 'tile_size'>,
grid: { cols: number; cell: number },
cells: Iterable<number>,
): string[] {
const tileSize = scene.tile_size
if (!(tileSize > 0) || grid.cell <= 0 || grid.cols <= 0) return []
const urls = new Set<string>()
for (const index of cells) {
const cx = index % grid.cols
const cy = (index / grid.cols) | 0
const tx = Math.floor((cx * grid.cell) / tileSize)
const ty = Math.floor((cy * grid.cell) / tileSize)
urls.add(tileUrl(scene, tx, ty))
}
return [...urls]
}
export class TileScheduler {
readonly #load: (url: string) => Promise<TileLoadResult>
readonly #onSettled: ((url: string, result: TileLoadResult) => void) | undefined
@@ -96,6 +123,23 @@ export class TileScheduler {
this.#lastDesired = []
}
/**
* A `403` is permanent *for the explored set the server had then*. When the fog slice reports
* newly explored cells (#70), the tiles those cells touch may have become allowed, so those
* denied urls are forgotten and the next `setDesired` asks for them exactly once more. Nothing
* else is retried: an unexplored 403 stays denied (no retry loop).
*/
retryDenied(urls: Iterable<string>): void {
let changed = false
for (const url of urls) {
if (this.#states.get(url) !== 'denied') continue
this.#states.delete(url)
changed = true
}
// The next setDesired rebuilds the queue, which re-queues the forgotten urls as `queued`.
if (changed) this.#lastDesired = []
}
/** True when every wanted tile settled with an `ok` or `denied` answer. */
isComplete(urls: readonly string[]): boolean {
return urls.every(url => {