Files
FFF_vtt_frozen/server/worldmap.nim
T
SirStone 9fcbe885c1 feat(walls): one-way sight per-viewer filter and ONEWAY fault (#58)
The vision fan blocks a sight-blocking one-way wall only for a viewer on
its blind side (sight_dir names it; left is the (-dy,dx) normal of a->b),
one sign test inline after the radius cull, so the hoisted wall list stays
viewer-independent. Movement never reads the direction: one-way walls block
both ways. Explored and tiles therefore differ by side and only ever grow.

- geometry: WallDir + Wall.sightDir + blindTo, filter in visibilityPolygon.
- worldmap: sight_dir mapping (movement mapping unchanged).
- vision: the ONEWAY leak-fault build drops sight_dir player-side.
- leakcheck: own side test for the fan and the clear-segment oracle; ONEWAY
  class detected against the sight_dir-dropped fan.
- leak scenario gains a one-way wall the subject sees across; fault test
  asserts exactly ONEWAY.
- oneway.test.mjs: both sides, tile sweep with its own oracle, flip with
  fixed endpoints, monotone explored/tiles, restart, two-way movement.
- docs: DESIGN 5.8 mechanism sentence corrected; TESTING #58 markers built.
- nginx harness: temp dirs created recursively (restart on one state dir).
2026-11-19 22:57:39 +01:00

89 lines
4.8 KiB
Nim

## Adapters between the authoritative records (`world.SceneRec`, `tokens.TokenRec`) and the pure
## inputs of the vision code (`geometry.nim`) and the move rules (`moverules.nim`). No IO, no locks,
## no wire shapes: main.nim builds these per mutation and copies the outcome back.
##
## The two modules below exist because the walls and tokens are stored as records with wire fields
## (kind, open, flags, owner) while geometry and moverules take narrow value types. Keeping the
## translation in one place is what makes "the rules never see the wire" checkable.
##
## The status bridge (DESIGN 5.5, slice 7): the move rules own the `walls` source of `stuck` and
## `squeezed`, the token record owns the whole entry set (the `GM` source too). `ruleToken` copies
## the record into the rules' shape - including `locked`, the one bit a player move needs - and
## `applyOutcome` copies the rules' two booleans back into the record's `walls`-sourced entries.
##
## Every function here returns a *new* status sequence instead of mutating one in place: `main.nim`
## copies a token record before it applies an outcome, and a Nim seq assignment shares its buffer, so
## in-place edits would reach the authoritative list before the transaction commits (4.2 step 6).
import std/options
import geometry, moverules, tokens, world, faults
proc geometryWalls*(sc: SceneRec, forSight = false): seq[Wall] =
## The scene's walls as the vision code sees them: an open door does not block (5.1), and a
## one-way wall carries the side that is blind (5.8, #58). Movement gets the same mapping and
## ignores `sightDir` (two-way).
## `forSight` is only the sight computation; `LEAK_FAULT=secretgap` (a -d:leakfault build only)
## opens a secret door there, the negative control for the door-equivalence group (#56).
result = newSeqOfCap[Wall](sc.walls.len)
for w in sc.walls:
let dir = case w.sightDir
of sdBoth: wdBoth
of sdLeft: wdLeft
of sdRight: wdRight
var wall = Wall(a: w.a, b: w.b, door: w.door, open: w.open, sightDir: dir)
if forSight and w.secret and faultActive("secretgap"):
wall.open = true
result.add wall
proc ruleWall*(w: WallRec): Wall =
## The same wall as the move rules see it (they need `door`/`open` to know what blocks; the
## one-way direction is sight-only and never read by the move rules, so it is not mapped).
Wall(a: w.a, b: w.b, door: w.door, open: w.open)
proc ruleToken*(sc: SceneRec, t: TokenRec): moverules.Token =
moverules.Token(pos: t.pos, owner: t.owner,
radius: effectiveRadius(sc, t),
hasVision: t.hasVision,
visionRadius: (if t.visionRadius.isSome: t.visionRadius.get else: 0.0),
locked: t.isLocked,
stuckWalls: t.hasStatus(stStuck, ssWalls),
squeezedWalls: t.hasStatus(stSqueezed, ssWalls),
prev: t.prev, hasPrev: t.hasPrev)
proc moveScene*(sc: SceneRec, tokens: seq[TokenRec]): moverules.Scene =
## The scene as the move rules see it: per-token radius (5.6) and per-token lock state (5.5).
## `tokens` may be one longer than the caller's list, for the placement of a token about to be
## created (the new one is the last element, as `moverules.addToken` would append it).
result = moverules.Scene(w: sc.w, h: sc.h, visionDefault: sc.visionRadiusDefault,
walls: geometryWalls(sc))
result.tokens = newSeqOfCap[moverules.Token](tokens.len)
for t in tokens:
result.tokens.add ruleToken(sc, t)
proc wallsStatuses*(entries: seq[StatusEntry], mr: moverules.Token): seq[StatusEntry] =
## The entry set with its `walls`-sourced half replaced by what the rules computed; the `GM`
## entries are untouched (5.5: they clear only by GM action).
result = entries
if mr.stuckWalls: result = withStatus(result, stStuck, ssWalls)
else: result = withoutStatus(result, stStuck, ssWalls)
if mr.squeezedWalls: result = withStatus(result, stSqueezed, ssWalls)
else: result = withoutStatus(result, stSqueezed, ssWalls)
type
Outcome* = object
## What a rule changed, so the caller can persist only that (and only then).
moved*: bool ## the position (or the door-push history) changed
statuses*: bool ## a `walls`-sourced status entry was added or removed
proc applyOutcome*(t: var TokenRec, mr: moverules.Token): Outcome =
## Copies one rule result into a token record: position, door-push history and `walls`-sourced
## statuses. The record may be a copy of the authoritative one (main.nim writes the DB first and
## assigns it afterwards); nothing here touches another record.
result.moved = t.pos != mr.pos or (t.prev, t.hasPrev) != (mr.prev, mr.hasPrev)
let before = t.statuses
t.statuses = wallsStatuses(before, mr)
result.statuses = t.statuses != before
t.pos = mr.pos
t.prev = mr.prev
t.hasPrev = mr.hasPrev