Files
SirStone 30b35592c7 fix: forward -- args through devbox scripts, fail serve loudly (#84)
devbox 0.17.2 runs .devbox/gen/scripts/<name>.sh with the args as positional
parameters but does not append "$@", so tools/serve.sh (and seed, bench,
mutation, import-scale, fogbench) saw an empty argv and a documented
`serve -- --image=...` silently served the default 4096 scene. End every
argument-taking script body with "$@".

serve: --dry-run parses and prints the options without building; unknown
options, non-numeric ports/tile sizes and --tile-size without --image fail
loudly; a no-image start prints the current map read from state.db; NixOS
gets security.pki.certificateFiles instead of the Debian trust steps.

Guard the faults import in worldmap/vision/markers with
`when defined(leakfault)` (it is only used inside those blocks), removing
the three UnusedImport warnings; leakfault builds still compile.

tools/devboxargs.test.mjs goes through `devbox run <script> -- <args>` for
serve (dry-run, unknown flag, tile-size without image) and seed (--help,
unknown flag), the seam the direct-tool tests missed.
2026-11-21 21:35:39 +01:00

98 lines
5.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
when defined(leakfault):
import faults # faultActive() is only referenced inside the leakfault blocks below (#84)
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 open = w.open
when defined(leakfault):
# Negative control for the door-equivalence group (#56): a secret door behaves like an open
# one, in a `-d:leakfault` build only. The shipped build has no fault path at all.
if forSight and w.secret and faultActive("secretgap"):
open = true
let wall = Wall(a: w.a, b: w.b, door: w.door, open: open, sightDir: dir,
sightBarrier: w.blocksSight, movementBarrier: w.blocksMovement)
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