## 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 and move code see them. Each wall carries its stored switches: ## the sight fan asks `blocksSight` (and the one-way `sightDir`, 5.8/#58), the move rules and the ## statuses ask `blocks`, so a window is see-through but still a barrier to movement (#57). Every ## wall stays in the list, in scene order: `moverules` addresses walls by that index. `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, sightBarrier: w.blocksSight, movementBarrier: w.blocksMovement) 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 the movement switch and `door`/`open`; 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, sightBarrier: w.blocksSight, movementBarrier: w.blocksMovement) 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