- door.set: opening a secret or locked door is invalid; closing either is allowed; a fixed window/non-door still has no open state. - new tools/doors.test.mjs: two fresh servers, one identical script, three equivalence pairs (secret/wall, secret+locked/wall, locked/closed door), canonicalised frame + full per-player tile sweeps, an open-door positive control and a pinned zero-thickness close-push. - secretgap fault hook (sight-only) makes the pair fail as exactly SECRETGAP; the clean pair passes and the shipped binary carries no fault hook. - docs/TESTING.md T2/T17 and the door-equivalence/10.3 sections: built. Ref #56
82 lines
4.5 KiB
Nim
82 lines
4.5 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).
|
|
## `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:
|
|
var wall = Wall(a: w.a, b: w.b, door: w.door, open: w.open)
|
|
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).
|
|
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
|
|
|