Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | /** * Proposition dependency graph — DAG computation + layout for the tech tree. * * Built on top of book1.ts (single source of truth for all 48 propositions). * Uses d3-dag with optimal crossing minimization (ILP-based) and transitive * reduction to remove redundant edges. */ import { graphStratify, sugiyama, decrossOpt, coordSimplex, layeringSimplex } from 'd3-dag' import { propositions, getPrerequisites, getDependents, getProposition, getAllPrerequisites, } from './book1' import { PRECOMPUTED_NODES, PRECOMPUTED_EDGES } from './book1Layout' import { PROP_REGISTRY } from '../propositions/registry' // --------------------------------------------------------------------------- // Which propositions are playable (have interactive implementations) // --------------------------------------------------------------------------- export const IMPLEMENTED_PROPS = new Set(Object.keys(PROP_REGISTRY).map(Number)) // --------------------------------------------------------------------------- // Node status computation // --------------------------------------------------------------------------- export type NodeStatus = 'completed' | 'available' | 'locked' | 'coming-soon' /** * Determine the status of a single proposition node. * * - completed: player has finished it * - available: implemented + all prerequisite propositions completed (or no deps) * - locked: implemented but some prerequisite not completed * - coming-soon: not yet implemented */ export function getNodeStatus(propId: number, completed: Set<number>): NodeStatus { if (completed.has(propId)) return 'completed' if (!IMPLEMENTED_PROPS.has(propId)) return 'coming-soon' const deps = getPrerequisites(propId) const allDepsMet = deps.every((d) => completed.has(d)) return allDepsMet ? 'available' : 'locked' } /** * Which nodes should be visible on the map. * * Default view: all implemented props + ONE level of direct dependents as * "coming-soon" teasers (only dependents whose prerequisites are ALL visible). * * Full view: all 48 nodes. */ export function getVisibleNodes(completed: Set<number>, showAll: boolean): number[] { if (showAll) return propositions.map((p) => p.id) const visible = new Set<number>() // Always show all implemented props for (const id of IMPLEMENTED_PROPS) { visible.add(id) } // Add direct dependents of implemented props as teasers, // but only those whose ALL prerequisites are in the visible set // (prevents pulling in distant unreachable nodes) for (const id of IMPLEMENTED_PROPS) { for (const depId of getDependents(id)) { if (visible.has(depId)) continue const deps = getPrerequisites(depId) if (deps.every((d) => visible.has(d) || IMPLEMENTED_PROPS.has(d))) { visible.add(depId) } } } return [...visible].sort((a, b) => a - b) } /** * Given a just-completed proposition, which NEW propositions become available? * (Only counts implemented propositions whose deps are now all satisfied.) */ export function getUnlockedBy(propId: number, completed: Set<number>): number[] { const afterCompletion = new Set(completed) afterCompletion.add(propId) const dependents = getDependents(propId) return dependents.filter((depId) => { if (!IMPLEMENTED_PROPS.has(depId)) return false if (completed.has(depId)) return false const deps = getPrerequisites(depId) return deps.every((d) => afterCompletion.has(d)) }) } /** * Get the next available implemented proposition to play. * Prefers the lowest-numbered available proposition. */ export function getNextProp(completed: Set<number>): number | null { for (const p of propositions) { if (!IMPLEMENTED_PROPS.has(p.id)) continue if (completed.has(p.id)) continue const deps = getPrerequisites(p.id) if (deps.every((d) => completed.has(d))) return p.id } return null } // --------------------------------------------------------------------------- // Transitive reduction // --------------------------------------------------------------------------- /** * Compute the transitive reduction of the DAG for a set of visible nodes. * * Removes edge (u→v) if there is another path from u to v of length ≥ 2. * This eliminates the long-range "skip" edges that create visual clutter. * * For each node v with parents P: * Edge (u→v) is redundant if u is a transitive ancestor of any other * parent w ∈ P (meaning u→...→w→v already exists). */ function transitiveReduction(visibleIds: number[]): Array<{ from: number; to: number }> { const visibleSet = new Set(visibleIds) const edges: Array<{ from: number; to: number }> = [] for (const v of visibleIds) { const directParents = getPrerequisites(v).filter((d) => visibleSet.has(d)) if (directParents.length <= 1) { // 0 or 1 parents → no redundancy possible for (const u of directParents) { edges.push({ from: u, to: v }) } continue } // For each parent u, check if it's a transitive ancestor of any OTHER parent const ancestorSets = new Map<number, Set<number>>() for (const u of directParents) { // getAllPrerequisites returns the full ancestor set (transitive) in topo order ancestorSets.set(u, new Set(getAllPrerequisites(u))) } for (const u of directParents) { let isRedundant = false for (const w of directParents) { if (w === u) continue // If w's ancestors include u, then u→...→w→v already exists if (ancestorSets.get(w)!.has(u)) { isRedundant = true break } } if (!isRedundant) { edges.push({ from: u, to: v }) } } } return edges } // --------------------------------------------------------------------------- // Layout via d3-dag (optimal crossing minimization) // --------------------------------------------------------------------------- export interface NodeLayout { x: number y: number level: number } /** * Edge with routing points for SVG rendering. */ export interface LayoutEdge { from: number to: number points: Array<{ x: number; y: number }> } const NODE_W = 130 const NODE_H = 48 /** * Compute the topological level of every proposition. * Level = longest path from a root (prop with no prop dependencies). */ function computeLevels(): Map<number, number> { const levels = new Map<number, number>() function getLevel(id: number): number { if (levels.has(id)) return levels.get(id)! const deps = getPrerequisites(id) if (deps.length === 0) { levels.set(id, 0) return 0 } const level = 1 + Math.max(...deps.map(getLevel)) levels.set(id, level) return level } for (const p of propositions) getLevel(p.id) return levels } const PROP_LEVELS = computeLevels() /** * Compute layout using d3-dag Sugiyama algorithm with ILP-based optimal * crossing minimization and transitive reduction. * * Returns both node positions and routed edge paths. */ export function computeLayout(visibleIds: number[]): { nodes: Map<number, NodeLayout> edges: LayoutEdge[] } { const nodes = new Map<number, NodeLayout>() if (visibleIds.length === 0) return { nodes, edges: [] } // For the full 48-node view, use pre-computed optimal layout // (decrossOpt ILP takes ~4 min — too slow for runtime) const allIds = propositions.map((p) => p.id) if (visibleIds.length === allIds.length && visibleIds.every((id, i) => id === allIds[i])) { for (const id of visibleIds) { const pos = PRECOMPUTED_NODES[id] if (pos) nodes.set(id, pos) } return { nodes, edges: PRECOMPUTED_EDGES } } // For smaller subsets, compute layout dynamically with decrossOpt const reducedEdges = transitiveReduction(visibleIds) // Build parent map for graphStratify const visibleSet = new Set(visibleIds) const parentMap = new Map<string, string[]>() for (const id of visibleIds) { parentMap.set(String(id), []) } for (const { from, to } of reducedEdges) { if (visibleSet.has(from) && visibleSet.has(to)) { parentMap.get(String(to))!.push(String(from)) } } const stratData = visibleIds.map((id) => ({ id: String(id), parentIds: parentMap.get(String(id))!, })) const dag = graphStratify()(stratData) const layout = sugiyama() .layering(layeringSimplex()) .decross(decrossOpt()) .coord(coordSimplex()) .nodeSize([NODE_W + 24, NODE_H + 60]) .gap([24, 12]) layout(dag) // Extract node positions for (const node of dag.nodes()) { const id = Number(node.data.id) nodes.set(id, { x: node.x, y: node.y, level: PROP_LEVELS.get(id) ?? 0, }) } // Extract edge routing points (d3-dag uses [x, y] tuples) const layoutEdges: LayoutEdge[] = [] for (const link of dag.links()) { const from = Number(link.source.data.id) const to = Number(link.target.data.id) layoutEdges.push({ from, to, points: link.points.map(([x, y]: [number, number]) => ({ x, y })), }) } return { nodes, edges: layoutEdges } } /** * Get the max topological level across all 48 propositions. */ export function getMaxLevel(): number { return Math.max(...PROP_LEVELS.values()) } /** * Get all edges (prerequisite → dependent) for the visible nodes. * Uses transitive reduction to eliminate redundant cross-level edges. */ export function getEdges(visibleIds: number[]): Array<{ from: number; to: number }> { return transitiveReduction(visibleIds) } // Re-export helpers consumers need export { getProposition, getPrerequisites, getDependents } |