feat(walls): proto 2 typed wall record, inert geometry, derived kind (#54)

wall.add/wall.update take thickness, position, height and the eight raw
switches; kind is output-only and the GM wall frames echo the full typed
record. Geometry is stored but inert (0/full/centre behaviour), and the
legacy kind/open wire adapter is gone. proto is bumped to 2 with the
harness default, and BannedAlways gains every wall field name with a
WALLDATA positive control.

Tests: schema-7 migrated scene vs the same walls authored via raw switches
gives byte-identical player frames, refusals and per-tile statuses.
Nim 382 / Node 113 green.
This commit is contained in:
2026-11-19 22:21:47 +01:00
parent c1b1e9cf91
commit 77fa2dfe4c
23 changed files with 852 additions and 318 deletions
+90 -50
View File
@@ -4,9 +4,10 @@
##
## Walls (DESIGN 8, FIXED #3): per-scene ids from `scene.next_wall_id`, monotonic and never reused
## (a removal leaves the counter alone). Identity is `(scene, id)` in the DB, in memory and on the
## wire. Doors are walls with `kind = door`; `blocks = not (kind == door and open)` comes from
## geometry.Wall when slice 5 feeds the vision code. `flags` is a free JSON object the server never
## interprets (M6/Beyond: window, one-way, secret door live there).
## wire. Today a wall's sight/movement behaviour is still `blocks = not (door and open)` in
## geometry.Wall; the stored switches (`blocks_sight`, `blocks_movement`, `sight_dir`) gain their
## behaviour in the window/one-way rounds (#57/#58). `flags` is a free JSON object the server never
## interprets.
##
## Wire payload shapes are DESIGN 6.1; the parts it leaves open are fixed here (recorded FIXED
## owner-delegated, #19, in DESIGN 6.1/14):
@@ -15,11 +16,11 @@
## - `scene.set` (slice 10, #25): the patch rules of 6.1 ("Scene payload rules"); `w`, `h`,
## `tile_size`, `cell` and `art_version` are not patch fields. `patched`/`writeSceneRow` are pure
## and transaction-free so main.nim can commit the scene row with the tokens it resized.
## - `wall.add`: `open` omitted = false; `open` on a `kind: "wall"` is `invalid` (a wall is never
## open), and the stored row normalizes it to false regardless.
## - `wall.update` patches `a`, `b`, `kind`, `flags` only (`open` belongs to `door.set`; an `open`
## field here is an unknown field and is ignored); at least one patch field is required, else
## `invalid`. Changing `kind` door -> wall closes the door.
## - `wall.add`: the geometry fields and the eight raw switches of 6.1; an omitted switch keeps
## the `wall` default, `open` omitted = false, and a non-door never stores door-only settings.
## - `wall.update` patches only what it names (geometry, the switches except `open`, `label`,
## `flags`); `open` belongs to `door.set`, so an `open` field here is an unknown field and is
## ignored, and at least one patch field is required, else `invalid`.
## - `a != b` (a zero-length wall is refused) and both endpoints inside `[0,w] x [0,h]`.
## - `flags` must be a JSON object; omitted = `{}`. Coordinates must be finite numbers.
##
@@ -27,15 +28,11 @@
## around it (Q12 FIXED: GM authoring is the expensive-to-lose data) and the in-memory state is
## updated only after COMMIT, so a failing write leaves memory and DB identical. This module does
## no locking: the caller holds the global lock (4.3).
import std/[json, math, options, strutils]
import std/[json, math, options, strutils, unicode]
import db_connector/db_sqlite
import geometry, text
type
WallKind* = enum
wkWall = "wall"
wkDoor = "door"
WallPosition* = enum
wpCenter = "center"
wpLeft = "left"
@@ -47,8 +44,8 @@ type
sdRight = "right"
WallRec* = object
## The typed wall record of DESIGN 8. The wire still carries the legacy `kind`/`open` pair in
## this round (#53); `kind` is derived from `door` and nothing else is exposed there yet (#54).
## The typed wall record of DESIGN 8. Every field is a column; `kind` is never stored and is
## derived from the switches (`derivedKind`) for the GM's frames (#54).
id*: int
a*, b*: Vec
thickness*: float
@@ -83,8 +80,19 @@ type
WallPatch* = object
## The updatable fields of `wall.update` (DESIGN 6.1); none = the field keeps its value.
## `open` is not here: it belongs to `door.set` and is an unknown field in a wall update (#54).
## `height`/`label` use a separate `clear*` flag because an explicit JSON null is a real patch
## ("clear it"), while an absent field keeps the stored value.
a*, b*: Option[Vec]
kind*: Option[WallKind]
thickness*: Option[float]
position*: Option[WallPosition]
height*: Option[float]
clearHeight*: bool
label*: Option[string]
clearLabel*: bool
blocksSight*, blocksMovement*: Option[bool]
sightDir*: Option[SightDir]
playerMarker*, door*, secret*, locked*: Option[bool]
flags*: Option[JsonNode]
ScenePatch* = object
@@ -104,36 +112,41 @@ type
const
DefaultSceneId* = 1
## The seeded v1 scene (migration step 3). Exactly one scene exists in v1 (Q3).
WallKinds* = [wkWall, wkDoor]
WallPositions* = [wpCenter, wpLeft, wpRight]
SightDirs* = [sdBoth, sdLeft, sdRight]
SceneNameMaxLen* = 64
## `scene.set.name`: the 6.5 text rule, same 64-code-point bound as character names (#25).
proc defaultWall*(a, b: Vec): WallRec =
## The `wall` preset's switches: both blocks on, both-way sight, no marker, no door settings.
WallRec(a: a, b: b, blocksSight: true, blocksMovement: true, flags: newJObject())
## The `wall` preset's switches: both blocks on, both-way sight, no marker, no door settings,
## zero thickness, centre placement and full height (DESIGN 5.8, 6.1).
WallRec(a: a, b: b, position: wpCenter, blocksSight: true, blocksMovement: true,
sightDir: sdBoth, flags: newJObject())
proc kind*(w: WallRec): WallKind =
## The legacy wire label for this round: only the door axis exists on it (#53). #54 replaces
## this with the full derived shape label.
if w.door: wkDoor else: wkWall
proc `kind=`*(w: var WallRec, k: WallKind) =
## Legacy update path (`wall.update {kind}`): a wall is never open; clearing the door switch
## also drops the door-only settings so the DB CHECK cannot see an impossible row.
if k == wkDoor:
w.door = true
proc derivedKind*(w: WallRec): string =
## The GM-only shape label of DESIGN 5.8, derived in this order and never stored or accepted as
## input: window, one_way, door, wall, else custom. `secret`/`locked` never change it.
if not w.blocksSight and w.blocksMovement:
"window"
elif w.blocksSight and w.sightDir != sdBoth:
"one_way"
elif w.blocksSight and w.blocksMovement and w.door:
"door"
elif w.blocksSight and w.blocksMovement and not w.door and w.sightDir == sdBoth:
"wall"
else:
w.door = false
w.open = false
w.secret = false
w.locked = false
"custom"
# ---------------------------------------------------------------------------------------------------
# Field parsing (DESIGN 6.1 field notes: px floats, missing or wrongly typed -> `invalid`)
proc parseKind*(raw: string): Option[WallKind] =
for k in WallKinds:
if $k == raw: return some(k)
proc parsePosition*(raw: string): Option[WallPosition] =
for p in WallPositions:
if $p == raw: return some(p)
proc parseSightDir*(raw: string): Option[SightDir] =
for d in SightDirs:
if $d == raw: return some(d)
proc parseVec*(node: JsonNode): Option[Vec] =
## `a:[x,y]` / `b:[x,y]`: exactly two finite JSON numbers.
@@ -155,19 +168,13 @@ proc parseFlags*(node: JsonNode): Option[JsonNode] =
if node.kind != JObject: return none(JsonNode)
some(node)
proc parseOpen*(node: JsonNode): Option[bool] =
## `open?`: a boolean or absent (= false).
if node.isNil: return some(false)
if node.kind != JBool: return none(bool)
some(node.getBool)
proc fieldOrNull*(env: JsonNode, key: string): JsonNode =
## `undefined` and `null` are both "field absent" for the wall payloads.
if env.isNil: return nil
result = env{key}
if not result.isNil and result.kind == JNull: result = nil
proc parseFinite(node: JsonNode): Option[float] =
proc parseFinite*(node: JsonNode): Option[float] =
## One finite JSON number (integers accepted; strings, booleans and null are not numbers).
if node.isNil: return none(float)
case node.kind
@@ -233,13 +240,32 @@ proc patched*(sc: SceneRec, patch: ScenePatch): SceneRec =
proc inBounds(sc: SceneRec, p: Vec): bool =
p.x >= 0 and p.y >= 0 and p.x <= sc.w and p.y <= sc.h # written so NaN fails
proc storable*(rec: WallRec): bool =
## The subset of the switch-set invariants the database CHECKs already enforce (db.nim step 8).
## #54 accepts and stores the raw switches; this test keeps a payload the schema cannot store from
## reaching SQLite and raising. The full expand-and-validate path, presets and the remaining
## switch-set rules are #55.
if rec.open and not rec.door: return false
if rec.secret and not rec.door: return false
if rec.secret and (rec.open or not rec.blocksSight or not rec.blocksMovement or
rec.sightDir != sdBoth): return false
if rec.locked and not rec.door: return false
if rec.sightDir != sdBoth and not rec.blocksSight: return false
true
proc validWall*(sc: SceneRec, rec: WallRec): bool =
## DESIGN 6.1 `wall.*` validation: `a != b`, both endpoints in bounds. `kind` and `flags` are
## checked by the parsers, so a `WallRec` can only carry valid ones.
(rec.a.x != rec.b.x or rec.a.y != rec.b.y) and inBounds(sc, rec.a) and inBounds(sc, rec.b)
## DESIGN 6.1 `wall.*` validation: `a != b`, both endpoints in bounds, the geometry ranges and a
## switch set the schema can store. `flags` is checked by the parser, so a `WallRec` can only
## carry a valid one.
(rec.a.x != rec.b.x or rec.a.y != rec.b.y) and inBounds(sc, rec.a) and inBounds(sc, rec.b) and
rec.thickness >= 0 and rec.thickness <= 256 and
(rec.height.isNone or (rec.height.get > 0 and rec.height.get <= 100)) and
(rec.label.isNone or (rec.label.get.runeLen >= 1 and rec.label.get.runeLen <= 64)) and
storable(rec)
proc normalize*(rec: WallRec): WallRec =
## A wall is never open; only a door can be, and a non-door stores no door-only settings.
## A non-door stores no door-only settings (clearing `door` closes and de-secrets), and a missing
## flags object becomes `{}` (DESIGN 6.1).
result = rec
if not result.door:
result.open = false
@@ -256,11 +282,25 @@ proc wallById*(sc: SceneRec, id: int): Option[WallRec] =
if i.isSome: return some(sc.walls[i.get])
proc patched*(w: WallRec, patch: WallPatch): WallRec =
## `wall.update` candidate: the stored row with the patch applied (open untouched).
## `wall.update` candidate: the stored row with the patch applied. A patch is absolute and only
## touches what it names; `open` is never here (door.set owns it). `clearHeight`/`clearLabel` are
## an explicit null, distinct from an absent field.
result = w
if patch.a.isSome: result.a = patch.a.get
if patch.b.isSome: result.b = patch.b.get
if patch.kind.isSome: result.kind = patch.kind.get
if patch.thickness.isSome: result.thickness = patch.thickness.get
if patch.position.isSome: result.position = patch.position.get
if patch.clearHeight: result.height = none(float)
elif patch.height.isSome: result.height = patch.height
if patch.clearLabel: result.label = none(string)
elif patch.label.isSome: result.label = patch.label
if patch.blocksSight.isSome: result.blocksSight = patch.blocksSight.get
if patch.blocksMovement.isSome: result.blocksMovement = patch.blocksMovement.get
if patch.sightDir.isSome: result.sightDir = patch.sightDir.get
if patch.playerMarker.isSome: result.playerMarker = patch.playerMarker.get
if patch.door.isSome: result.door = patch.door.get
if patch.secret.isSome: result.secret = patch.secret.get
if patch.locked.isSome: result.locked = patch.locked.get
if patch.flags.isSome: result.flags = patch.flags.get
result = normalize(result)
@@ -355,7 +395,7 @@ proc addWall*(db: DbConn, sc: var SceneRec, rec: WallRec): Option[int] =
proc updateWall*(db: DbConn, sc: var SceneRec, rec: WallRec): WallApply =
## `wall.update` = full replacement of the patched row (`remove + add` for the 5.5 status rules,
## which slice 5 re-evaluates). A kind change door -> wall closes the door.
## which slice 5 re-evaluates). Clearing the door switch closes and de-secrets the wall.
let idx = wallIndex(sc, rec.id)
if idx.isNone: return waNotFound
let stored = normalize(rec)