logigate/app/lib/logic/cables.ts

92 lines
4.6 KiB
TypeScript

// Cables: several wires bundled into one. A bundle part gathers the wires,
// one cable runs to a split part (or several), which fans them out again.
// Wire i of the cable is input i of the bundle and output i of the split.
import { cableBits, MAX_BITS, pinPos, shapeOf, type DefLookup } from './geometry'
import { uid } from './ops'
import type { CircuitNode, ComponentDef, Wire } from './types'
const isCable = (def: ComponentDef, w: Wire) => def.nodes.find(n => n.id === w.from.node)?.kind === 'bundle'
/** The bundle a wire belongs to: the wire is the cable itself, or runs into or out of its ends. */
export function cableOf(def: ComponentDef, wire: Wire): CircuitNode | undefined {
const nodes = new Map(def.nodes.map(n => [n.id, n]))
const from = nodes.get(wire.from.node)
if (from?.kind === 'bundle') return from
const to = nodes.get(wire.to.node)
if (to?.kind === 'bundle') return to
if (from?.kind === 'split') {
const feed = def.wires.find(w => w.to.node === from.id)
const b = feed && nodes.get(feed.from.node)
if (b?.kind === 'bundle') return b
}
return undefined
}
/**
* Replaces the given wires with a cable: a bundle just after their sources,
* a split just before their targets, wires in the order of the targets top
* to bottom. Returns the new parts, or a reason it can't.
*/
export function bundleWires(def: ComponentDef, wireIds: string[], get: DefLookup): { ok: true; bundle: CircuitNode; split: CircuitNode } | { ok: false; error: string } {
const nodes = new Map(def.nodes.map(n => [n.id, n]))
const ids = new Set(wireIds)
const wires = def.wires.filter(w => ids.has(w.id))
if (wires.length < 2) return { ok: false, error: 'Select at least two wires' }
if (wires.length > MAX_BITS) return { ok: false, error: `A cable holds at most ${MAX_BITS} wires` }
if (wires.some(w => isCable(def, w))) return { ok: false, error: 'A cable cannot go into another cable' }
const ends = wires.map(w => {
const fn = nodes.get(w.from.node)!
const tn = nodes.get(w.to.node)!
return { w, a: pinPos(fn, shapeOf(fn, get), w.from.pin, 'out')!, b: pinPos(tn, shapeOf(tn, get), w.to.pin, 'in')! }
})
if (ends.some(e => !e.a || !e.b)) return { ok: false, error: 'Some wires have no pins' }
ends.sort((p, q) => p.b.y - q.b.y || p.a.y - q.a.y)
const n = ends.length
const mid = Math.floor((n - 1) / 2)
const mean = (v: number[]) => Math.round(v.reduce((s, x) => s + x, 0) / v.length)
const right = Math.max(...ends.map(e => e.a.x))
const left = Math.min(...ends.map(e => e.b.x))
const bx = right + 2
const sx = Math.max(bx + 4, left - 3)
const color = wires.find(w => w.color)?.color
const bundle: CircuitNode = { id: uid('n'), kind: 'bundle', bits: n, x: bx, y: mean(ends.map(e => e.a.y)) - mid }
const split: CircuitNode = { id: uid('n'), kind: 'split', bits: n, x: sx, y: mean(ends.map(e => e.b.y)) - mid }
def.nodes.push(bundle, split)
def.wires = def.wires.filter(w => !ids.has(w.id))
ends.forEach((e, i) => {
const pin = `b${i}`
def.wires.push({ id: uid('w'), from: e.w.from, to: { node: bundle.id, pin }, ...(e.w.color ? { color: e.w.color } : {}) })
def.wires.push({ id: uid('w'), from: { node: split.id, pin }, to: e.w.to, ...(e.w.color ? { color: e.w.color } : {}) })
})
def.wires.push({ id: uid('w'), from: { node: bundle.id, pin: 'q' }, to: { node: split.id, pin: 'a' }, ...(color ? { color } : {}) })
return { ok: true, bundle, split }
}
/**
* Takes a cable apart again: its bundle and splits go, and each source is
* wired straight to the targets its wire reached. Returns how many wires
* now run on their own.
*/
export function dissolveCable(def: ComponentDef, bundleId: string): number {
const bundle = def.nodes.find(n => n.id === bundleId && n.kind === 'bundle')
if (!bundle) return 0
const splits = new Set(def.wires.filter(w => w.from.node === bundle.id).map(w => w.to.node))
for (const id of [...splits]) if (def.nodes.find(n => n.id === id)?.kind !== 'split') splits.delete(id)
const added: Wire[] = []
for (let i = 0; i < cableBits(bundle); i++) {
const pin = `b${i}`
const feed = def.wires.find(w => w.to.node === bundle.id && w.to.pin === pin)
if (!feed) continue
for (const w of def.wires) {
if (!splits.has(w.from.node) || w.from.pin !== pin) continue
added.push({ id: uid('w'), from: feed.from, to: w.to, ...(w.color ? { color: w.color } : {}) })
}
}
const gone = new Set([bundle.id, ...splits])
def.nodes = def.nodes.filter(n => !gone.has(n.id))
def.wires = [...def.wires.filter(w => !gone.has(w.from.node) && !gone.has(w.to.node)), ...added]
return added.length
}