## Player marker emission and persistence (DESIGN 5.8, 6.1, 6.3, 8; ADR-0015, #59). ## ## A marker exists for a player exactly when a wall is both `player_marker:true` and derives ## `window`, and any point of the wall's visible extent has been inside one of that player's vision ## polygons. In this round the visible extent is the drawn segment (thickness is inert, 5.10), so the ## test is: an endpoint inside the polygon, or the segment crossing one of its edges. The gate is the ## player's own polygon, the same one that decides token visibility (5.1), so nothing behind a ## sight-blocking wall qualifies and the check never needs a second vision implementation. ## ## The marker is per-player memory like explored: it is minted once, gets that player's own opaque ## 12-character id (never the wall id), keeps the wall's drawn midpoint rounded once as its anchor, ## and is persisted in `marker_seen` as part of the same derived-memory transaction as the explored ## chunks (4.2 step 6). `vision.PlayerVision.markers` holds it in memory and `restore` loads it at ## startup, before the first connection (4.4). This round only adds: retraction (wall removed, switch ## off, no longer a window, geometry moved), re-earning a fresh id and the marker fault classes are ## #60; `sync` is the seam where that lifetime rule lands. ## ## No DB transaction control here: the caller owns it, exactly like explored.nim. import std/[math, options, strutils, tables] import db_connector/db_sqlite import geometry, tokens, vision, world proc anchorOf*(w: WallRec): (int, int) = ## The wall's drawn midpoint, rounded once to integer px and fixed for the wall (5.8, 6.1). (int(round((w.a.x + w.b.x) / 2.0)), int(round((w.a.y + w.b.y) / 2.0))) proc extentSeen*(pv: PlayerVision, a, b: Vec): bool = ## True when any point of the wall's visible extent (the segment in this round) is inside one of ## the player's polygons: an endpoint inside, or the segment crossing a polygon edge. Boundary ## contacts are inclusive, which is the generous side of "any point" and cannot admit anything ## behind a sight blocker (the polygon itself stops there). for poly in pv.polys: if poly.pts.len < 3: continue if inside(poly.pts, a) or inside(poly.pts, b): return true for i in 0 ..< poly.pts.len: if segsCross(a, b, poly.pts[i], poly.pts[(i + 1) mod poly.pts.len]): return true proc qualifies*(pv: PlayerVision, w: WallRec): bool = ## The complete emission gate of 5.8: the switch on, the derived shape label a window, and part of ## the visible extent seen. A solid wall with `player_marker:true` fails the kind gate. w.playerMarker and derivedKind(w) == "window" and pv.extentSeen(w.a, w.b) proc markerIdTaken(pv: PlayerVision, id: string): bool = for marker in pv.markers.values: if marker.id == id: return true proc freshMarkerId(pv: PlayerVision): string = result = newTokenId() var tries = 0 while markerIdTaken(pv, result) and tries < 64: result = newTokenId() inc tries proc syncMarkers*(pv: var PlayerVision, sc: SceneRec): int = ## Adds one marker per wall that now qualifies and is not in this player's memory yet; returns how ## many were minted. Add-only by design in this round (#59): a marker that stops qualifying stays, ## so walking away, a door closing and lost sight never retract it. The retraction/re-earn rule is ## #60 and belongs here, next to this loop. for w in sc.walls: if w.id in pv.markers: continue if not pv.qualifies(w): continue let (x, y) = anchorOf(w) let marker = Marker(id: freshMarkerId(pv), x: x, y: y) pv.markers[w.id] = marker pv.markerFresh.add (w.id, marker) inc result # --------------------------------------------------------------------------------------------------- # Persistence (marker_seen, migration step 8; the caller owns the transaction) const InsertSql = """INSERT INTO marker_seen (player_id, scene_id, wall_id, marker_id) VALUES (?, ?, ?, ?)""" proc writeNew*(db: DbConn, sceneId, playerId: int, fresh: var seq[(int, Marker)]): int = ## Inserts the markers the last refresh minted for one player and clears the buffer. Called inside ## the mutation's transaction, after the wall row (for a wall write) committed and before the ack: ## a marker can never precede the wall that causes it (5.8, 4.2 step 6). if fresh.len == 0: return 0 let ps = db.prepare(InsertSql) try: for (wallId, marker) in fresh: db.exec(ps, playerId, sceneId, wallId, marker.id) inc result finally: finalize(ps) fresh.setLen(0) proc restore*(db: DbConn, sc: SceneRec, visions: var Table[int, PlayerVision]) = ## Startup (4.4): every `marker_seen` row back into its player's memory, before the first ## connection, with the anchor recomputed from the loaded wall (the anchor is derived, not stored). ## A row whose wall is missing cannot exist (the composite FK cascades); skipping one defensively ## keeps a hand-edited DB from crashing startup. for row in db.getAllRows(sql""" SELECT player_id, wall_id, marker_id FROM marker_seen WHERE scene_id = ?""", sc.id): let pid = parseInt(row[0]) let wallId = parseInt(row[1]) let wall = sc.wallById(wallId) if wall.isNone: continue let (x, y) = anchorOf(wall.get) visions.mgetOrPut(pid, PlayerVision()).markers[wallId] = Marker(id: row[2], x: x, y: y) proc markerCount*(db: DbConn, sceneId: int): int = ## Scene-wide marker rows: an O(1) startup log number (11.3) and a test probe. parseInt(db.getValue(sql"SELECT count(*) FROM marker_seen WHERE scene_id = ?", sceneId))