Startup restores each player's grid from its chunk rows (explored_loaded log) and every mutation writes only the chunks its refresh dirtied, in the mutation's transaction: a move keeps position and chunks in one NORMAL commit, a GM authoring write adds one NORMAL chunk transaction under the same lock, before the ack. db.withTransaction is the shared seam.
102 lines
4.5 KiB
Nim
102 lines
4.5 KiB
Nim
## Per-player vision and explored state (DESIGN 3.1, 4.1, 4.2, 5.1, 8; M3 slice 5, issue #20).
|
|
## Pure and in-process: no IO, no clock, no wire shapes. main.nim holds one `PlayerVision` per player
|
|
## id and calls `refresh` after every mutation that can change that player's sight.
|
|
##
|
|
## Vision (5.1): one visibility polygon per token the player controls that carries the `vision` flag,
|
|
## cast from the token centre against the scene's blocking walls with the token's range (Q7: the
|
|
## token's `vision_radius` if it sets one, else the scene's `vision_radius_default`). Open doors do
|
|
## not block; geometry.nim does the casting.
|
|
##
|
|
## Explored (5.1, 8): the union of the cells every polygon ever overlapped, per player, strictly
|
|
## per player (Q9 FIXED). It only grows: a wall edit or a door closing never un-explores. `cells`
|
|
## are `(cx, cy)` from the world origin, cell size = the scene's `cell` (= `tile_size` in v1, Q4).
|
|
## Persistence (chunked bitmaps, #21) lives in server/explored.nim, which stores this module's
|
|
## `explored` set and restores it at startup; this module owns the in-memory set only.
|
|
##
|
|
## Token visibility to a player (3.1): a token this player controls is always visible; any other
|
|
## token only while its centre is inside one of the player's polygons and it is not `gm_only`.
|
|
## `Poly` ids are token ids, which the controlling player already knows (`mine`).
|
|
##
|
|
## Change tracking: `ver` counts polygon changes (the client gets `vision` only when it changes, 6.4)
|
|
## and `fresh` holds the cells this mutation added (4.2 step 8: cleared after egress).
|
|
import std/[algorithm, math, sets]
|
|
import tokens, worldmap, geometry, world
|
|
|
|
type
|
|
Poly* = object
|
|
tokenId*: string
|
|
pts*: seq[Vec]
|
|
|
|
PlayerVision* = object
|
|
polys*: seq[Poly]
|
|
explored*: HashSet[int] ## cell index cy * cols + cx
|
|
ver*: int ## bumped when `polys` changes
|
|
fresh*: seq[(int, int)] ## cells added by the current mutation (cx, cy), sorted
|
|
|
|
proc cols*(sc: SceneRec): int =
|
|
## Cells across the scene (DESIGN 8: ceil(w / cell)).
|
|
max(1, int(ceil(sc.w / float(sc.cell))))
|
|
|
|
proc rows*(sc: SceneRec): int =
|
|
max(1, int(ceil(sc.h / float(sc.cell))))
|
|
|
|
proc cellIndex*(sc: SceneRec, cx, cy: int): int = cy * sc.cols + cx
|
|
|
|
proc cellOf*(sc: SceneRec, idx: int): (int, int) =
|
|
let c = sc.cols
|
|
(idx mod c, idx div c)
|
|
|
|
proc computePolys*(sc: SceneRec, tokens: seq[TokenRec], playerId: int): seq[Poly] =
|
|
## One polygon per token of this player that has vision, in token order (a stable wire order).
|
|
if playerId <= 0: return
|
|
let walls = geometryWalls(sc)
|
|
for t in tokens:
|
|
if t.owner != playerId or not t.hasVision: continue
|
|
result.add Poly(tokenId: t.id,
|
|
pts: visibilityPolygon(t.pos, walls, visionRange(sc, t)))
|
|
|
|
proc refresh*(pv: var PlayerVision, sc: SceneRec, tokens: seq[TokenRec], playerId: int) =
|
|
## Recomputes the polygons, bumps `ver` when they changed, and folds every newly seen cell into
|
|
## `explored`/`fresh`. Cells already explored are never re-announced.
|
|
let polys = computePolys(sc, tokens, playerId)
|
|
if polys != pv.polys:
|
|
pv.polys = polys
|
|
inc pv.ver
|
|
var added = initHashSet[int]()
|
|
for poly in pv.polys:
|
|
for (cx, cy) in cellsTouched(poly.pts, float(sc.cell)):
|
|
if cx < 0 or cy < 0 or cx >= sc.cols or cy >= sc.rows: continue
|
|
let idx = sc.cellIndex(cx, cy)
|
|
if idx in pv.explored or idx in added: continue
|
|
added.incl idx
|
|
pv.fresh.add (cx, cy)
|
|
for idx in added: pv.explored.incl idx
|
|
pv.fresh.sort()
|
|
|
|
proc clearFresh*(pv: var PlayerVision) =
|
|
## 4.2 step 8: after the egress fan-out every connection has the cells.
|
|
pv.fresh.setLen(0)
|
|
|
|
proc exploredFull*(pv: PlayerVision, sc: SceneRec): seq[(int, int)] =
|
|
## Every explored cell, sorted by (cy, cx): the connect/resync payload (6.1 `explored full`).
|
|
var idx = newSeq[int](pv.explored.len)
|
|
var i = 0
|
|
for k in pv.explored:
|
|
idx[i] = k
|
|
inc i
|
|
idx.sort()
|
|
result = newSeqOfCap[(int, int)](idx.len)
|
|
for k in idx: result.add sc.cellOf(k)
|
|
|
|
proc insideAny*(pv: PlayerVision, p: Vec): bool =
|
|
for poly in pv.polys:
|
|
if inside(poly.pts, p): return true
|
|
|
|
proc visibleTo*(pv: PlayerVision, t: TokenRec, playerId: int): bool =
|
|
## DESIGN 3.1: controlled tokens always, anything else only inside own LOS and not `gm_only`.
|
|
## This is the only copy of the rule: the leak-fault builds add tokens that this returns false for
|
|
## instead of re-implementing it, so a mutation here still breaks the clean path (DESIGN 10.3).
|
|
if t.owner == playerId: return true
|
|
if t.gmOnly: return false
|
|
pv.insideAny(t.pos)
|