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.
171 lines
7.5 KiB
Nim
171 lines
7.5 KiB
Nim
## Per-player vision and explored (#20, DESIGN 3.1, 4.2, 5.1, 8): one polygon per controlled token
|
|
## with the `vision` flag, walls and open doors, the explored set that only grows, the fresh cells of
|
|
## one mutation, and the token-visibility rule (controlled always, LOS + not `gm_only` otherwise).
|
|
import std/[algorithm, json, math, options, sequtils, sets, unittest]
|
|
import geometry, tokens, vision, world
|
|
|
|
proc sceneWith(walls: seq[WallRec]): SceneRec =
|
|
SceneRec(id: 1, name: "Scene", w: 4096, h: 4096, tileSize: 32, cell: 32, gridSize: 0,
|
|
visionRadiusDefault: 400, tokenRadiusDefault: 20, artVersion: 1, nextWallId: 1,
|
|
extra: newJObject(), walls: walls)
|
|
|
|
proc wall(ax, ay, bx, by: float, door = false, open = false): WallRec =
|
|
result = defaultWall((ax, ay), (bx, by))
|
|
result.door = door
|
|
result.open = open
|
|
|
|
proc tok(id: string, x, y: float, owner: int, visionFlag = true, gmOnly = false): TokenRec =
|
|
TokenRec(id: id, scene: 1, kind: "pc", descriptiveName: id, actualName: id, pos: (x, y),
|
|
owner: owner, gmOnly: gmOnly,
|
|
flags: (if visionFlag: %*{"vision": true} else: %*{}))
|
|
|
|
proc exploredCells(pv: PlayerVision, sc: SceneRec): int = pv.explored.len
|
|
|
|
suite "polygons (5.1: one per controlled token with the `vision` flag)":
|
|
test "only the player's own vision tokens produce one, in token order":
|
|
let sc = sceneWith(@[])
|
|
let tokens = @[tok("mine", 400, 400, 2), tok("noflag", 800, 800, 2, visionFlag = false),
|
|
tok("theirs", 1200, 1200, 3), tok("second", 1600, 1600, 2)]
|
|
let polys = computePolys(sc, tokens, 2)
|
|
check polys.len == 2
|
|
check polys[0].tokenId == "mine"
|
|
check polys[1].tokenId == "second"
|
|
check polys[0].pts.len >= 3
|
|
|
|
test "a player with no vision token has no polygons at all":
|
|
let sc = sceneWith(@[])
|
|
check computePolys(sc, @[tok("theirs", 100, 100, 3)], 2).len == 0
|
|
check computePolys(sc, @[], 2).len == 0
|
|
|
|
test "the polygon is bounded by the token's range, and a closer wall cuts it":
|
|
let sc = sceneWith(@[wall(700, 0, 700, 4096)])
|
|
let tokens = @[tok("me", 400, 400, 2)]
|
|
let free = computePolys(sceneWith(@[]), tokens, 2)[0].pts
|
|
# nothing reaches farther than the 400 px default radius (small tolerance for the 64-gon)
|
|
for p in free: check hypot(p.x - 400, p.y - 400) <= 401
|
|
let cut = computePolys(sc, tokens, 2)[0].pts
|
|
for p in cut: check p.x <= 701
|
|
check cut.anyIt(it.x > 690) # the wall is reached, not clipped short
|
|
|
|
test "the token's own vision_radius overrides the scene default":
|
|
let sc = sceneWith(@[])
|
|
var t = tok("me", 400, 400, 2)
|
|
t.visionRadius = some(120.0)
|
|
let pts = computePolys(sc, @[t], 2)[0].pts
|
|
for p in pts: check hypot(p.x - 400, p.y - 400) <= 121
|
|
|
|
test "an open door does not block; closing it does (5.1, 5.5)":
|
|
let closed = sceneWith(@[wall(700, 0, 700, 4096, door = true, open = false)])
|
|
let open = sceneWith(@[wall(700, 0, 700, 4096, door = true, open = true)])
|
|
let tokens = @[tok("me", 400, 400, 2)]
|
|
check computePolys(closed, tokens, 2)[0].pts.allIt(it.x <= 701)
|
|
check computePolys(open, tokens, 2)[0].pts.anyIt(it.x > 790)
|
|
|
|
suite "explored (5.1, 8: strictly per player, only grows)":
|
|
test "the cells under the polygon are explored, and `fresh` reports them once":
|
|
let sc = sceneWith(@[])
|
|
var pv: PlayerVision
|
|
pv.refresh(sc, @[tok("me", 400, 400, 2)], 2)
|
|
check pv.explored.len > 0
|
|
check pv.fresh.len == pv.explored.len # first sight: everything is fresh
|
|
let first = pv.explored.len
|
|
pv.clearFresh()
|
|
check pv.fresh.len == 0
|
|
pv.refresh(sc, @[tok("me", 400, 400, 2)], 2) # nothing moved
|
|
check pv.explored.len == first
|
|
check pv.fresh.len == 0
|
|
|
|
test "moving adds cells and never forgets the old ones":
|
|
let sc = sceneWith(@[])
|
|
var pv: PlayerVision
|
|
pv.refresh(sc, @[tok("me", 400, 400, 2)], 2)
|
|
let first = pv.explored.len
|
|
pv.clearFresh()
|
|
pv.refresh(sc, @[tok("me", 1600, 1600, 2)], 2)
|
|
check pv.explored.len > first
|
|
check pv.fresh.len == pv.explored.len - first
|
|
# the old area stays explored even though it is no longer visible
|
|
let oldCell = sc.cellIndex(12, 12)
|
|
check oldCell in pv.explored
|
|
check not pv.insideAny((400.0, 400.0))
|
|
|
|
test "a closed door hides the cells behind it; opening it adds them":
|
|
var sc = sceneWith(@[wall(700, 0, 700, 4096, door = true, open = false)])
|
|
var pv: PlayerVision
|
|
pv.refresh(sc, @[tok("me", 400, 400, 2)], 2)
|
|
let behind = sc.cellIndex(23, 12) # x ~ 750, past the closed door
|
|
check behind notin pv.explored
|
|
sc.walls[0].open = true
|
|
pv.clearFresh()
|
|
pv.refresh(sc, @[tok("me", 400, 400, 2)], 2)
|
|
check behind in pv.explored
|
|
check pv.fresh.len > 0
|
|
|
|
test "each player has their own set; nobody shares explored cells (Q9 FIXED)":
|
|
let sc = sceneWith(@[])
|
|
var a, b: PlayerVision
|
|
a.refresh(sc, @[tok("a", 400, 400, 2), tok("b", 3000, 3000, 3)], 2)
|
|
b.refresh(sc, @[tok("a", 400, 400, 2), tok("b", 3000, 3000, 3)], 3)
|
|
check a.explored.len > 0 and b.explored.len > 0
|
|
check sc.cellIndex(12, 12) in a.explored
|
|
check sc.cellIndex(12, 12) notin b.explored
|
|
check sc.cellIndex(93, 93) in b.explored
|
|
check sc.cellIndex(93, 93) notin a.explored
|
|
|
|
test "the full payload is sorted by row and never contains a cell twice":
|
|
let sc = sceneWith(@[])
|
|
var pv: PlayerVision
|
|
pv.refresh(sc, @[tok("me", 400, 400, 2)], 2)
|
|
let full = pv.exploredFull(sc)
|
|
check full.len == pv.explored.len
|
|
for i in 1 ..< full.len:
|
|
# sorted by row, then column: the RLE rows of 6.1 need contiguous columns per row
|
|
check (full[i - 1][1], full[i - 1][0]) < (full[i][1], full[i][0])
|
|
for (cx, cy) in full: check sc.cellOf(sc.cellIndex(cx, cy)) == (cx, cy)
|
|
|
|
suite "version counter (6.4: `vision` only on change)":
|
|
test "ver moves with the polygons and stands still without a change":
|
|
let sc = sceneWith(@[])
|
|
var pv: PlayerVision
|
|
check pv.ver == 0
|
|
pv.refresh(sc, @[tok("me", 400, 400, 2)], 2)
|
|
check pv.ver == 1
|
|
pv.refresh(sc, @[tok("me", 400, 400, 2)], 2)
|
|
check pv.ver == 1
|
|
pv.refresh(sc, @[tok("me", 401, 400, 2)], 2)
|
|
check pv.ver == 2
|
|
pv.refresh(sc, @[], 2) # the token went away
|
|
check pv.ver == 3
|
|
check pv.polys.len == 0
|
|
|
|
suite "token visibility (3.1)":
|
|
test "a controlled token is always visible, gm_only included":
|
|
let sc = sceneWith(@[])
|
|
var pv: PlayerVision
|
|
pv.refresh(sc, @[tok("me", 400, 400, 2)], 2)
|
|
check pv.visibleTo(tok("me", 400, 400, 2), 2)
|
|
check pv.visibleTo(tok("hidden-own", 3000, 3000, 2, gmOnly = true), 2)
|
|
|
|
test "another token needs to be inside the polygon":
|
|
let sc = sceneWith(@[wall(700, 0, 700, 4096)])
|
|
var pv: PlayerVision
|
|
pv.refresh(sc, @[tok("me", 400, 400, 2)], 2)
|
|
check pv.visibleTo(tok("near", 600, 400, 3), 2)
|
|
check not pv.visibleTo(tok("behind", 900, 400, 3), 2) # past the wall
|
|
check not pv.visibleTo(tok("far", 3000, 3000, 3), 2) # beyond the range
|
|
|
|
test "a gm_only token inside the LOS is not visible to anyone else":
|
|
let sc = sceneWith(@[])
|
|
var pv: PlayerVision
|
|
pv.refresh(sc, @[tok("me", 400, 400, 2)], 2)
|
|
check not pv.visibleTo(tok("secret", 500, 500, 0, gmOnly = true), 2)
|
|
check pv.visibleTo(tok("plain", 500, 500, 0), 2)
|
|
|
|
test "a player with no vision token sees only what they control":
|
|
let sc = sceneWith(@[])
|
|
var pv: PlayerVision
|
|
pv.refresh(sc, @[tok("blind", 400, 400, 2, visionFlag = false)], 2)
|
|
check pv.polys.len == 0
|
|
check pv.visibleTo(tok("blind", 400, 400, 2, visionFlag = false), 2)
|
|
check not pv.visibleTo(tok("nearby", 420, 420, 3), 2)
|