import { axialToPixel, createHexGrid, hexDistance, hexLine, type AxialCoord, type HexCell, } from "./hex.ts"; import { resolveTopologyBoundary, sameBoundary, type TopologyBoundary, type TopologyBoundaryDefinition } from "./topologyBoundary.ts"; import type { TopologyNodeOperationalState } from "./nodeStatus.ts"; export interface TopologyEntity extends AxialCoord { id: string; label: string; subtitle?: string; image?: string; imageOffsetY?: number; type?: "anchor" | "section" | "entity" | "data"; status?: TopologyNodeOperationalState | string; } export interface TopologyRelationship { from: string; to: string; kind?: string; active?: boolean; muted?: boolean; } export interface TopologyModelOptions { radius: number; columns: number; rows: number; originQ?: number; originR?: number; entities?: TopologyEntity[]; relationships?: TopologyRelationship[]; boundary?: TopologyBoundaryDefinition | null; } export class TopologyModel { radius: number; columns: number; rows: number; originQ: number; originR: number; cells: HexCell[]; entities: TopologyEntity[]; relationships: TopologyRelationship[]; boundary: TopologyBoundaryDefinition | null; constructor(options: TopologyModelOptions) { this.radius = options.radius; this.columns = options.columns; this.rows = options.rows; this.originQ = options.originQ ?? 0; this.originR = options.originR ?? 0; this.entities = options.entities ?? []; this.relationships = options.relationships ?? []; this.boundary = options.boundary ?? null; this.cells = createHexGrid({ radius: this.radius, columns: this.columns, rows: this.rows, originQ: this.originQ, originR: this.originR, }); } getCell(coord: AxialCoord): HexCell | undefined { return this.cells.find( (cell) => cell.q === coord.q && cell.r === coord.r ); } getEntity(id: string): TopologyEntity | undefined { return this.entities.find((entity) => entity.id === id); } getEntityPosition(id: string) { const entity = this.getEntity(id); if (!entity) return null; return axialToPixel( { q: entity.q, r: entity.r }, this.radius ); } getRelationshipsForEntity(id: string): TopologyRelationship[] { return this.relationships.filter( (relationship) => relationship.from === id || relationship.to === id ); } getBoundary(entityId: string): TopologyBoundary | null { return resolveTopologyBoundary(entityId, { entities: this.entities, relationships: this.relationships, boundary: this.boundary, }); } sameBoundary(fromId: string, toId: string) { return sameBoundary(fromId, toId, { entities: this.entities, relationships: this.relationships, boundary: this.boundary, }); } getPathBetweenEntities(fromId: string, toId: string): AxialCoord[] { const from = this.getEntity(fromId); const to = this.getEntity(toId); if (!from || !to) return []; return hexLine( { q: from.q, r: from.r }, { q: to.q, r: to.r } ); } getDistanceBetweenEntities(fromId: string, toId: string): number | null { const from = this.getEntity(fromId); const to = this.getEntity(toId); if (!from || !to) return null; return hexDistance( { q: from.q, r: from.r }, { q: to.q, r: to.r } ); } getRing(center: AxialCoord, distance: number): AxialCoord[] { if (distance === 0) return [center]; const directions: AxialCoord[] = [ { q: 1, r: 0 }, { q: 1, r: -1 }, { q: 0, r: -1 }, { q: -1, r: 0 }, { q: -1, r: 1 }, { q: 0, r: 1 }, ]; let coord: AxialCoord = { q: center.q + directions[4].q * distance, r: center.r + directions[4].r * distance, }; const results: AxialCoord[] = []; for (const direction of directions) { for (let i = 0; i < distance; i++) { results.push(coord); coord = { q: coord.q + direction.q, r: coord.r + direction.r, }; } } return results; } getSpiral(center: AxialCoord, radius: number): AxialCoord[] { const results: AxialCoord[] = []; for (let distance = 0; distance <= radius; distance++) { results.push(...this.getRing(center, distance)); } return results; } }