import { axialToPixel, type AxialCoord } from "./hex.ts"; import { getTopologyNodeRoutingCost, isTopologyNodeTraversable, resolveTopologyNodeOperationalState, type TopologyNodeOperationalState } from "./nodeStatus.ts"; import { routeConnector, type ConnectorRoutingMode, type ConnectorRoutingStatus, } from "./connectorRouting.ts"; export interface ConnectorOffset { x: number; y: number; } export type ConnectorState = "idle" | "active" | "related" | "disabled"; export type ConnectorBoundaryState = "local" | "cross-boundary"; export type SocketDirection = "n" | "nw" | "ne" | "sw" | "se" | "s"; export type SocketState = "hidden" | "available" | "connected" | "hovered" | "active" | "transmitting"; export interface ConnectorNode extends AxialCoord { id: string; size?: "xs" | "sm" | "md" | "lg" | "xl" | "root"; active?: boolean; selected?: boolean; disabled?: boolean; status?: string; } export interface ConnectorEdge { from: string; to: string; fromSocket?: SocketDirection; toSocket?: SocketDirection; state?: ConnectorState; active?: boolean; muted?: boolean; disabled?: boolean; } export interface ConnectorRoute { from: string; to: string; d: string; start: { x: number; y: number }; end: { x: number; y: number }; connectorState: ConnectorState; startDirection: SocketDirection | null; endDirection: SocketDirection | null; routingMode: ConnectorRoutingMode; routingStatus: ConnectorRoutingStatus; routeCells: AxialCoord[]; waypoints: AxialCoord[]; boundaryState: ConnectorBoundaryState; sourceOperationalState: TopologyNodeOperationalState; targetOperationalState: TopologyNodeOperationalState; } export interface ConnectorGraph { routes: ConnectorRoute[]; socketMap: Map>>; } export interface ConnectorGraphOptions { routingMode?: ConnectorRoutingMode; cells?: Array<{ id: string; q: number; r: number }>; sameBoundary?: (sourceId: string, targetId: string) => boolean; } const sizeFactors: Record, number> = { xs: 1.2, sm: 1.55, md: 2.05, lg: 2.65, xl: 3.15, root: 4, }; const socketPositions: Record, Record> = { xs: { n: { x: 50, y: 2.2 }, nw: { x: 5.8, y: 25.2 }, ne: { x: 94.2, y: 25.2 }, sw: { x: 5.8, y: 74.8 }, se: { x: 94.2, y: 74.8 }, s: { x: 50, y: 97.8 }, }, sm: { n: { x: 50, y: 1.8 }, nw: { x: 6.1, y: 24.9 }, ne: { x: 93.9, y: 24.9 }, sw: { x: 6.1, y: 75.1 }, se: { x: 93.9, y: 75.1 }, s: { x: 50, y: 98.2 }, }, md: { n: { x: 50, y: 1.2 }, nw: { x: 6.8, y: 24.2 }, ne: { x: 93.2, y: 24.2 }, sw: { x: 6.8, y: 75.8 }, se: { x: 93.2, y: 75.8 }, s: { x: 50, y: 98.8 }, }, lg: { n: { x: 50, y: 0.95 }, nw: { x: 7, y: 23.8 }, ne: { x: 93, y: 23.8 }, sw: { x: 7, y: 76.2 }, se: { x: 93, y: 76.2 }, s: { x: 50, y: 99.05 }, }, xl: { n: { x: 50, y: 0.45 }, nw: { x: 7.6, y: 22.9 }, ne: { x: 92.4, y: 22.9 }, sw: { x: 7.6, y: 77.1 }, se: { x: 92.4, y: 77.1 }, s: { x: 50, y: 99.55 }, }, root: { n: { x: 50, y: 0.25 }, nw: { x: 8, y: 22.2 }, ne: { x: 92, y: 22.2 }, sw: { x: 8, y: 77.8 }, se: { x: 92, y: 77.8 }, s: { x: 50, y: 99.75 }, }, }; const directionByStep: Record = { "0,-1": "n", "1,-1": "ne", "1,0": "se", "0,1": "s", "-1,1": "sw", "-1,0": "nw", }; function directionForStep(from: AxialCoord, to: AxialCoord) { const dq = to.q - from.q; const dr = to.r - from.r; return directionByStep[`${dq},${dr}`] ?? null; } function pointToString(point: { x: number; y: number }) { return `${point.x.toFixed(3)} ${point.y.toFixed(3)}`; } function nodeSizeMetrics(size: NonNullable, radius: number) { const factor = sizeFactors[size]; const width = radius * factor; const height = width * 1.12; return { width, height }; } function socketPoint(node: ConnectorNode, direction: SocketDirection | null, radius: number, offset: ConnectorOffset = { x: 0, y: 0 }) { const center = axialToPixel({ q: node.q, r: node.r }, radius, offset); if (!direction) return center; const size = node.size ?? "md"; const { width, height } = nodeSizeMetrics(size, radius); const anchor = socketPositions[size][direction]; return { x: center.x + (anchor.x / 100 - 0.5) * width, y: center.y + (anchor.y / 100 - 0.5) * height, }; } function leadSocketPoint(node: ConnectorNode, direction: SocketDirection | null, radius: number, offset: ConnectorOffset = { x: 0, y: 0 }) { return socketPoint(node, direction, radius, offset); } function resolveConnectorState(edge: ConnectorEdge): ConnectorState { if (edge.disabled) return "disabled"; if (edge.state) return edge.state; if (edge.active) return "active"; if (edge.muted) return "related"; return "idle"; } function nodeIsUnavailable(node: ConnectorNode) { return !isTopologyNodeTraversable(node); } function mergeSocketState( current: Partial>, direction: SocketDirection | null, state: SocketState, ) { if (!direction) return current; const next = { ...current }; const existing = next[direction]; if (existing === "transmitting" || existing === "active" || existing === "hovered" || existing === "connected") { return next; } next[direction] = state; return next; } function buildConnectorPath(from: ConnectorNode, to: ConnectorNode, radius: number, offset: ConnectorOffset = { x: 0, y: 0 }, edge: Partial = {}) { const start = axialToPixel({ q: from.q, r: from.r }, radius, offset); const end = axialToPixel({ q: to.q, r: to.r }, radius, offset); const points: { x: number; y: number }[] = [start, end]; return { path: points.map((point, index) => `${index === 0 ? "M" : "L"} ${pointToString(point)}`).join(" "), start, end, startDirection: edge.fromSocket ?? null, endDirection: edge.toSocket ?? null, routingMode: "straight" as const, routingStatus: "routed" as const, routeCells: [{ q: from.q, r: from.r }, { q: to.q, r: to.r }], waypoints: [{ q: from.q, r: from.r }, { q: to.q, r: to.r }], }; } function buildRoutedConnectorPath( from: ConnectorNode, to: ConnectorNode, nodes: ConnectorNode[], radius: number, offset: ConnectorOffset, edge: ConnectorEdge, options: ConnectorGraphOptions, ) { const routingMode = options.routingMode ?? "straight"; if (routingMode === "straight" || !options.cells?.length) { return buildConnectorPath(from, to, radius, offset, edge); } const blocked = nodes .filter((node) => node.id !== from.id && node.id !== to.id) .map(({ q, r }) => ({ q, r })); const route = routeConnector({ mode: routingMode, cells: options.cells, from, to, radius, offset, blocked, }); const nextCell = route.cells[1] ?? null; const previousCell = route.cells.at(-2) ?? null; return { path: route.d, start: route.points[0] ?? axialToPixel(from, radius, offset), end: route.points.at(-1) ?? axialToPixel(to, radius, offset), startDirection: edge.fromSocket ?? (nextCell ? directionForStep(from, nextCell) : null), endDirection: edge.toSocket ?? (previousCell ? directionForStep(to, previousCell) : null), routingMode, routingStatus: route.status, routeCells: route.cells, waypoints: route.waypoints, }; } export function buildConnectorGraph( nodes: ConnectorNode[], edges: ConnectorEdge[], radius: number, offset: ConnectorOffset = { x: 0, y: 0 }, options: ConnectorGraphOptions = {}, ): ConnectorGraph { const nodeMap = new Map(nodes.map((node) => [node.id, node])); const socketMap = new Map>>(); for (const node of nodes) { socketMap.set(node.id, {}); } const routes = edges .map((edge) => { const from = nodeMap.get(edge.from); const to = nodeMap.get(edge.to); if (!from || !to) return null; const connector = buildRoutedConnectorPath( from, to, nodes, radius, offset, edge, options, ); const fromSockets = socketMap.get(from.id) ?? {}; const toSockets = socketMap.get(to.id) ?? {}; const activeState = edge.active ? "active" : "connected"; const sourceOperationalState = resolveTopologyNodeOperationalState(from); const targetOperationalState = resolveTopologyNodeOperationalState(to); const sourceUnavailable = !Number.isFinite(getTopologyNodeRoutingCost(from)); const targetUnavailable = !Number.isFinite(getTopologyNodeRoutingCost(to)); const unavailableRoute = sourceUnavailable || targetUnavailable; socketMap.set(from.id, mergeSocketState(fromSockets, connector.startDirection, activeState)); socketMap.set(to.id, mergeSocketState(toSockets, connector.endDirection, activeState)); return { from: edge.from, to: edge.to, connectorState: unavailableRoute ? "disabled" : resolveConnectorState(edge), d: connector.path, start: connector.start, end: connector.end, startDirection: connector.startDirection, endDirection: connector.endDirection, routingMode: connector.routingMode, routingStatus: connector.routingStatus, routeCells: connector.routeCells, waypoints: connector.waypoints, boundaryState: options.sameBoundary?.(edge.from, edge.to) === false ? "cross-boundary" : "local", sourceOperationalState, targetOperationalState, } satisfies ConnectorRoute; }) .filter((route): route is ConnectorRoute => Boolean(route)); return { routes, socketMap }; }