Files
FFF_vtt_frozen/server/worldmap.nim
T
SirStone 38d8962f84 server: the status model, per-token radius and the grow push (#22)
moverules.nim replaces the M2 `blocked` bool with the 5.5 status model: the
rules own the `walls` source (`stuckWalls`, `squeezedWalls`) and take
`locked` (any entry, any source) as input, so a GM-sourced lock refuses a
player move without the rules ever knowing what caused it. Identity is kept
on KEEP, cleared on CLEAR, and only tokens the event touches change.

Every geometry path now uses the token's own radius (5.6): the overlap test,
the escape check, the capsule, the door push and the step cap's neighbour
rules. `resizeToken` implements 5.6: a shrink never pushes and clears the
walls entries only when the overlap ends, a grow that overlaps is displaced
by a deterministic greedy relaxation (radial off the first overlapped wall,
scene order, at most 16 rounds, one flipped-side pass) and a grow that cannot
be placed stays put and becomes squeezed(walls). A step whose centre path
would cross a wall ends the pass, so the relaxation never carries a token
through a wall.

worldmap.nim is the bridge: `ruleToken`/`ruleWall`/`moveScene` build the
rules' input from the records, `applyOutcome` copies the position, the
door-push history and the walls-sourced entries back, and `wallsStatuses`
does the same for a caller that writes its own row. All of them return fresh
sequences instead of mutating a shared one.
2026-11-15 11:02:29 +01:00

77 lines
4.2 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
proc geometryWalls*(sc: SceneRec): seq[Wall] =
## The scene's walls as the vision code sees them: an open door does not block (5.1).
result = newSeqOfCap[Wall](sc.walls.len)
for w in sc.walls:
result.add Wall(a: w.a, b: w.b, door: w.kind == wkDoor, open: w.open)
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.kind == wkDoor, 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