Three scouts with lanterns explore a dark cave, and the map remembers what they saw. The toolbox's new world/visibility answers the question behind every fog of war: which cells can be seen from here, and what has anyone seen so far.
Play it · Source: collection-spielwerk/36-laterne · Toolbox 6.2.0 or later
The game
The cave is black. Ada, Bo and Cleo stand at the entrance, each with a lantern. Pick a scout and click a place: it walks there and lights what it sees. Only what a lantern sees right now is in colour; everything any scout has seen stays on the map in grey. Crystals and old bones show up the moment a light first falls on them. A scout left alone walks to the nearest edge of the unknown by itself, so the cave gets explored even if you only watch. "Achtel" shows how the chosen scout's view is cast. There are no monsters and nothing to lose; the round ends when every corner is known.
- Mouse or touch: a scout picks it, a place in the cave sends it there; the buttons pick a scout, show the octants or dig a new cave.
- Keyboard: 1 to 3 or Tab picks a scout, the arrows move a cursor, Space or Enter sends the scout, O shows the octants, N digs a new cave.
New in the toolbox: visibility
The problem. Step 31 brought has_line_of_sight: one line between two cells. A lantern needs the whole view, about 140 cells for a radius of 6, every time a scout steps into a new cell. A line to every cell repeats the same work over and over and looks wrong: a wall seen at a slant along a corridor drops out after two cells, because the line to it brushes the wall before it. And the fog must remember: three scouts, cells seen before, cells seen now.
The idea. Shadowcasting: split the view into four quadrants (each two octants, the switch "Achtel" shows them) and walk each quadrant row by row away from the viewer. A row's light is a range of slopes. Where a wall stands, the range is cut: the part before it goes on to the next row, the wall itself is seen, the part behind it is shadow. Each cell is looked at about once. I use Albert Ford's symmetric version: slopes are whole-number fractions, and an open cell B is seen from A exactly when A is seen from B. A scout who sees a monster is seen by it. One rule is my own: light never squeezes between two walls that touch only at a corner, so a diagonal line of rock is a wall from either side.
field_of_view(grid, origin, radius, { blocks_sight, shape? }): GridPosition[]
visit_field_of_view(grid, origin, radius, options, (x, y) => ...): number // no list
const fog = new FogOfWar(grid, { blocks_sight });
fog.update(viewers); // [{ id, x, y, radius }]: casts only who moved
fog.state_at(x, y); // "unseen" | "seen" | "visible"
fog.events.attach(FogSeenEvent, (event) => event.cells); // seen for the first time
fog.changed(x, y); // a door opened
draw_fog(context, fog, cell_size, { soft: true, grey_seen: true });
The only thing a game must say is which cells stop sight. The fog counts per cell how many viewers see it; update() casts the view again only for a viewer that stepped into another cell, and takes back the view of one that is gone. A unit standing still costs nothing. draw_fog keeps one picture of one pixel per cell, makes it again only when the fog changed, and draws it scaled in one call; soft blurs the edges over one cell, grey_seen drains the colour from what was seen before (the canvas blend mode saturation).
In the game, the scouts are the viewers, and the finds hang on the event:
this.fog = new FogOfWar(this.cave.cells, { blocks_sight: (cell) => cell === 'rock' });
this.fog.events.attach(FogSeenEvent, (event) => this.on_seen(event.cells));
// every update, after the scouts walked:
this.fog.update(this.scouts.map((scout) => ({ id: scout.index, ...cell_at(scout.x, scout.y), radius: 6 })));
What it costs, measured on an Intel i7-11700F in Node 22 and in Playwright's headless Chromium, on a 200 × 200 map with 12 % scattered rock: one view of radius 8 (141 cells) takes 9 to 11 µs, about 14 000 cells per millisecond; radius 16 (321 cells) 21 to 25 µs. A full fog update where every viewer moved: 10 viewers 0.12 to 0.25 ms, 100 viewers 1.1 ms (Chromium) to 2.2 ms (Node), with radius 16 2.4 to 3.0 ms. A frame is 16.7 ms. This game's own fog, three lanterns on a 48 × 36 cave, takes about 0.015 ms an update; the card "Sicht" shows it live.
Tests. The toolbox checks symmetry on twelve random maps (every pair of open cells, both ways), that a pillar casts a shadow, that a room's walls and corners are seen and nothing behind them, that a diagonal wall never leaks, and that the fog never forgets and always equals a fresh view of all viewers. The game checks that its fog is exactly the lanterns' views and that idle scouts see every floor cell within 150 seconds. The trap I met: my first test called a cell "seen through a wall" whenever no straight line reached it without touching a wall. Shadowcasting looks at the walls at each row's middle, so a sightline may graze a corner by a few tenths of a cell; the test now checks what the method promises.
The module is src/world/visibility/ (FieldOfView.ts, FogOfWar.ts, DrawFog.ts).
From earlier steps
Grid2Dfrom step 03: the cave.SeededRandomfrom step 01: the same seed digs the same cave and lays the same crystals.find_path,flow_fieldandsmooth_pathfrom step 31: the scouts' walks, and the nearest edge of the unknown for an idle scout.- Noise recipes from step 34: the cave is noise cut at about half floor, its pockets joined by tunnels.
load_animationfrom step 32: the scouts are the cabbage kid from the library, walking and idling.- The
ActionMapfrom step 30: keys and a gamepad pick scouts and steer the cursor.
The scaffold, start screen, intro and scaling are explained in Spielwerk 00: The workbench.
Embed it
<script src="https://www.game-object.de/tce-elements/tobidot/dll-tce-assets"></script>
<script src="https://www.game-object.de/tce-elements/tobidot/tce-laterne"></script>
<tce-laterne init-seed="4711"></tce-laterne>
| Attribute | Values | Default |
|---|---|---|
init-seed |
whole number: the first cave | random |
init-intro |
true / false: play the GO logo intro |
true |
Next
Previous: Spielwerk 35: Spielstand · Next: Spielwerk 37: Gewimmel · All steps
Comments