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
+164 -58
View File
@@ -137,7 +137,15 @@ proc closeGone(conn: Conn, reason: string, code: int) =
# views.egressFrames and never enter a PlayerView at all.
proc wallObjectOf(w: WallRec): JsonNode =
wallObject(w.id, w.a, w.b, $w.kind, w.open, w.flags)
## The GM's typed wall (DESIGN 6.1 proto 2): every stored field plus the derived shape label.
wallObject(WallView(id: w.id, a: w.a, b: w.b, thickness: w.thickness, position: $w.position,
blocksSight: w.blocksSight, blocksMovement: w.blocksMovement,
sightDir: $w.sightDir, playerMarker: w.playerMarker, door: w.door,
open: w.open, secret: w.secret, locked: w.locked,
height: (if w.height.isSome: w.height.get else: 0.0),
hasHeight: w.height.isSome,
label: (if w.label.isSome: w.label.get else: ""),
hasLabel: w.label.isSome, kind: derivedKind(w), flags: w.flags))
proc wallsSnapshot(sc: SceneRec): JsonNode =
var walls: seq[JsonNode]
@@ -484,6 +492,144 @@ proc declared(env: Envelope, key: string): bool =
## `vision_radius: null` means "the scene default" (6.1), so both are real patch fields.
not env.node.isNil and env.node.hasKey(key)
# Wall payload fields (DESIGN 6.1, proto 2, #54). `kind` and (in an update) `open` are never read:
# they are unknown fields and ignored (6.2). Absent and JSON null are the same field-absent for
# every wall field except `height` and `label`, where an explicit null means "clear it".
proc parseWallGeometry(env: Envelope, rec: var WallRec): bool =
## thickness (px), position and height (metres); the ranges are checked by `validWall`.
if present(env, "thickness"):
let v = parseFinite(fieldOrNull(env.node, "thickness"))
if v.isNone: return false
rec.thickness = v.get
if present(env, "position"):
let node = env.node{"position"}
if node.kind != JString: return false
let p = parsePosition(node.getStr)
if p.isNone: return false
rec.position = p.get
if declared(env, "height"):
let node = env.node{"height"}
if node.kind == JNull:
rec.height = none(float)
else:
let v = parseFinite(node)
if v.isNone: return false
rec.height = v
true
proc parseWallLabel(env: Envelope, rec: var WallRec): bool =
## `label` absent leaves it alone on an update (the caller seeds `rec` from the stored row);
## an explicit null clears it; anything but a string is `invalid`. The 6.5 text rule is #55,
## but the schema's 1..64 bound is enforced by `validWall` so SQLite cannot raise.
if not declared(env, "label"): return true
let node = env.node{"label"}
if node.kind == JNull:
rec.label = none(string)
elif node.kind == JString:
rec.label = some(node.getStr)
else:
return false
true
proc parseWallSwitch(env: Envelope, key: string, dest: var bool): bool =
## One boolean switch: absent = leave `dest`; a non-boolean is `invalid`.
if not present(env, key): return true
boolField(env, key, dest)
proc parseWallSwitches(env: Envelope, rec: var WallRec, allowOpen: bool): bool =
## The raw 5.8 switches on a create: omitted switches keep the candidate's value (the `wall`
## defaults), `open` is accepted here. A `sight_dir` outside the three words is `invalid`.
if not parseWallSwitch(env, "blocks_sight", rec.blocksSight): return false
if not parseWallSwitch(env, "blocks_movement", rec.blocksMovement): return false
if not parseWallSwitch(env, "player_marker", rec.playerMarker): return false
if not parseWallSwitch(env, "door", rec.door): return false
if allowOpen and not parseWallSwitch(env, "open", rec.open): return false
if not parseWallSwitch(env, "secret", rec.secret): return false
if not parseWallSwitch(env, "locked", rec.locked): return false
if present(env, "sight_dir"):
let node = env.node{"sight_dir"}
if node.kind != JString: return false
let d = parseSightDir(node.getStr)
if d.isNone: return false
rec.sightDir = d.get
true
proc parseWallPatch(env: Envelope, patch: var WallPatch): bool =
## `wall.update` names only the fields it patches. `open` and `kind` are unknown here and are
## ignored: a door's open state changes only through `door.set` (DESIGN 6.1, #54).
if present(env, "a"):
let v = parseVec(env.node{"a"})
if v.isNone: return false
patch.a = v
if present(env, "b"):
let v = parseVec(env.node{"b"})
if v.isNone: return false
patch.b = v
if present(env, "thickness"):
let v = parseFinite(fieldOrNull(env.node, "thickness"))
if v.isNone: return false
patch.thickness = v
if present(env, "position"):
let node = env.node{"position"}
if node.kind != JString: return false
let p = parsePosition(node.getStr)
if p.isNone: return false
patch.position = p
if declared(env, "height"):
let node = env.node{"height"}
if node.kind == JNull:
patch.clearHeight = true
else:
let v = parseFinite(node)
if v.isNone: return false
patch.height = v
if declared(env, "label"):
let node = env.node{"label"}
if node.kind == JNull:
patch.clearLabel = true
elif node.kind == JString:
patch.label = some(node.getStr)
else:
return false
var bit: bool
if present(env, "blocks_sight"):
if not boolField(env, "blocks_sight", bit): return false
patch.blocksSight = some(bit)
if present(env, "blocks_movement"):
if not boolField(env, "blocks_movement", bit): return false
patch.blocksMovement = some(bit)
if present(env, "player_marker"):
if not boolField(env, "player_marker", bit): return false
patch.playerMarker = some(bit)
if present(env, "door"):
if not boolField(env, "door", bit): return false
patch.door = some(bit)
if present(env, "secret"):
if not boolField(env, "secret", bit): return false
patch.secret = some(bit)
if present(env, "locked"):
if not boolField(env, "locked", bit): return false
patch.locked = some(bit)
if present(env, "sight_dir"):
let node = env.node{"sight_dir"}
if node.kind != JString: return false
let d = parseSightDir(node.getStr)
if d.isNone: return false
patch.sightDir = d
if present(env, "flags"):
let f = parseFlags(env.node{"flags"})
if f.isNone: return false
patch.flags = f
true
proc wallPatchHasField(patch: WallPatch): bool =
patch.a.isSome or patch.b.isSome or patch.thickness.isSome or patch.position.isSome or
patch.height.isSome or patch.clearHeight or patch.label.isSome or patch.clearLabel or
patch.blocksSight.isSome or patch.blocksMovement.isSome or patch.sightDir.isSome or
patch.playerMarker.isSome or patch.door.isSome or patch.secret.isSome or patch.locked.isSome or
patch.flags.isSome
type
ControllerCheck = enum
ccOk
@@ -1897,20 +2043,10 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
if a.isNone or b.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
let kindNode = env.node{"kind"}
if kindNode.isNil or kindNode.kind != JString:
ackErr(conn, env.reqId, CodeInvalid)
return
let kind = parseKind(kindNode.getStr)
if kind.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
let open = parseOpen(fieldOrNull(env.node, "open"))
if open.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
if kind.get == wkWall and open.get:
# A wall is never open; only a door has an open state (DESIGN 6.1, 8).
# The raw-switch create: omitted switches keep the `wall` preset's defaults (DESIGN 6.1, #54).
var candidate = defaultWall(a.get, b.get)
if not parseWallGeometry(env, candidate) or not parseWallSwitches(env, candidate, true) or
not parseWallLabel(env, candidate):
ackErr(conn, env.reqId, CodeInvalid)
return
let flags = parseFlags(fieldOrNull(env.node, "flags"))
@@ -1923,16 +2059,15 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
var ok = false
withLock app.dbLock:
sceneId = app.scene.id # the request's one read (#31)
# `candidate` is built here so its `flags` copy and its record copy both die under the lock;
# `detachedJson` keeps the stored object independent of `env` (ANALYSIS 4.1).
let candidate = WallRec(a: a.get, b: b.get, door: kind.get == wkDoor,
open: open.get, blocksSight: true, blocksMovement: true,
flags: detachedJson(flags.get))
# The record is copied here so its `flags` object and the record copy both die under the
# lock; `detachedJson` keeps the stored object independent of `env` (ANALYSIS 4.1).
var stored = candidate
stored.flags = detachedJson(flags.get)
# 5.5 SET: the new wall sticks every token its circle overlaps. The rules run on a snapshot
# taken before the write (the id is assigned by the write), and their outcome is applied only
# after it committed, so a failed write leaves every token untouched.
var mr = moveScene(app.scene, app.tokens)
let id = addWall(app.db, app.scene, candidate)
let id = addWall(app.db, app.scene, stored)
if id.isSome:
let added = wallById(app.scene, id.get)
discard moverules.addWall(mr, ruleWall(added.get))
@@ -1940,7 +2075,7 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
refreshAll() # 4.2 step 5: a wall change moves every player's sight
persistAllExplored() # step 6: the chunks the new wall revealed, before the ack
addedId = added.get.id
addedKind = $added.get.kind
addedKind = derivedKind(added.get)
# The wall frame references `added.flags`; build it and fan it out here, before the record
# copy leaves scope, never after the lock is released (ANALYSIS 4.1).
settleWorldLocked(conn, env.reqId, wallFrame(sceneId, wallObjectOf(added.get)),
@@ -1960,41 +2095,12 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
ackErr(conn, env.reqId, CodeInvalid)
return
var patch: WallPatch
var anyField = false
if not fieldOrNull(env.node, "a").isNil:
let a = parseVec(env.node{"a"})
if a.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
patch.a = a
anyField = true
if not fieldOrNull(env.node, "b").isNil:
let b = parseVec(env.node{"b"})
if b.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
patch.b = b
anyField = true
if not fieldOrNull(env.node, "kind").isNil:
let node = env.node{"kind"}
if node.kind != JString:
ackErr(conn, env.reqId, CodeInvalid)
return
let kind = parseKind(node.getStr)
if kind.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
patch.kind = kind
anyField = true
if not fieldOrNull(env.node, "flags").isNil:
let flags = parseFlags(env.node{"flags"})
if flags.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
patch.flags = flags
anyField = true
if not anyField:
# `wall.update` changes at least one of a, b, kind, flags (6.1).
if not parseWallPatch(env, patch):
ackErr(conn, env.reqId, CodeInvalid)
return
if not wallPatchHasField(patch):
# A fieldless update (or one naming only the ignored `open`/`kind`) is `invalid`, before the
# target lookup (6.1, 6.5 steps 7 then 8).
ackErr(conn, env.reqId, CodeInvalid)
return
var outcome = waNotFound
@@ -2020,7 +2126,7 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
discard applyRuleOutcome(mr)
refreshAll()
persistAllExplored()
updatedKind = $updated.get.kind
updatedKind = derivedKind(updated.get)
# Frame and record copy both live and die inside this lock (ANALYSIS 4.1).
settleWorldLocked(conn, env.reqId, wallFrame(sceneId, wallObjectOf(updated.get)))
ok = true
+36 -5
View File
@@ -14,8 +14,10 @@ import std/[json, monotimes, unicode]
import geometry
const
ProtoVersion* = 1
## M3 ships proto 1 (DESIGN 6.2). The client announces it as `/ws?proto=N`.
ProtoVersion* = 2
## proto 2 is the wall-model build (DESIGN 6.2, ADR-0011, #54): the wall payloads carry the
## typed geometry and switches, `kind` is output-only and the GM frames echo the full record.
## The client announces it as `/ws?proto=N`.
MaxInboundBytes* = 64 * 1024
## DESIGN 6.4: mummy's `maxMessageLen`, inbound and cumulative across fragments.
RatePerSecond* = 30
@@ -211,10 +213,39 @@ proc rosterEntry*(id: int, name: JsonNode, role: string, colour: string,
# `n`: they are the only frames fanned out to several recipients at once (every GM connection), and
# each recipient gets its own number, added by the sender with `withN` right before the send.
proc wallObject*(id: int, a, b: Vec, kind: string, open: bool, flags: JsonNode): JsonNode =
type
WallView* = object
## One wall as the GM's `wall`/`walls` frames show it (DESIGN 6.1, proto 2, #54). The typed
## record plus the derived shape label; `height` and `label` are always present and null when
## unset (the frame is GM-only, so their presence marks nothing). This is the narrow wire shape,
## kept out of `world.nim` so the protocol shell stays free of storage imports.
id*: int
a*, b*: Vec
thickness*: float
position*: string
blocksSight*, blocksMovement*: bool
sightDir*: string
playerMarker*, door*, open*, secret*, locked*: bool
height*: float
hasHeight*: bool ## false = full height (`height:null`)
label*: string
hasLabel*: bool ## false = no label (`label:null`)
kind*: string ## the derived 5.8 shape label, output only
flags*: JsonNode
proc wallObject*(w: WallView): JsonNode =
## One wall as the wire shows it: the same shape inside `walls` and in a `wall` upsert.
%*{"id": id, "a": [a.x, a.y], "b": [b.x, b.y], "kind": kind, "open": open,
"flags": (if flags.isNil: newJObject() else: flags)}
%*{
"id": w.id, "a": [w.a.x, w.a.y], "b": [w.b.x, w.b.y],
"thickness": w.thickness, "position": w.position,
"height": (if w.hasHeight: %w.height else: newJNull()),
"kind": w.kind,
"blocks_sight": w.blocksSight, "blocks_movement": w.blocksMovement,
"sight_dir": w.sightDir, "player_marker": w.playerMarker,
"door": w.door, "open": w.open, "secret": w.secret, "locked": w.locked,
"label": (if w.hasLabel: %w.label else: newJNull()),
"flags": (if w.flags.isNil: newJObject() else: w.flags)
}
proc wallsFrame*(scene: int, walls: seq[JsonNode]): JsonNode =
## The GM snapshot of every wall in the scene (DESIGN 6.1 `walls`, 6.3: on connect and resync).
+3
View File
@@ -427,6 +427,9 @@ proc playerTokenFrame*(v: PlayerTokenView): JsonNode =
buckets["gm"] = v.faultGmProps
if faultActive("statusSource") and v.faultStatusEntries.len > 0:
result["statuses"] = statusesJson(v.faultStatusEntries)
if faultActive("wallHeight"):
# WALLDATA positive control (#54): a wall field name (`height`) in a player token frame.
result["height"] = %1.8
proc tokenGoneFrame*(id: string): JsonNode = %*{"t": "token_gone", "id": id}
+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)