107 lines
3.2 KiB
TypeScript
107 lines
3.2 KiB
TypeScript
// Campaign: a project where parts are earned. It starts with NAND, inputs,
|
|
// outputs and lamps. Each challenge opens once the ones it builds on are
|
|
// solved, and your own solution becomes the part you use from then on.
|
|
// Basic parts like the clock unlock on the way. Sandbox projects have
|
|
// everything from the start.
|
|
|
|
import { CHALLENGES } from './challenges'
|
|
import type { ComponentDef, NodeKind, Project } from './types'
|
|
|
|
/** Display names of the basic parts. */
|
|
export const KIND_NAMES: Partial<Record<NodeKind, string>> = {
|
|
nand: 'NAND gate',
|
|
input: 'Input switch',
|
|
output: 'Output pin',
|
|
lamp: 'Lamp',
|
|
button: 'Push button',
|
|
clock: 'Clock',
|
|
high: 'Constant 1',
|
|
low: 'Ground (0)',
|
|
hex: 'Hex display',
|
|
note: 'Note',
|
|
panel: 'Input panel',
|
|
outpanel: 'Output panel',
|
|
bundle: 'Cable bundle',
|
|
split: 'Cable split',
|
|
pixel: 'Pixel',
|
|
portin: 'Connector in',
|
|
portout: 'Connector out',
|
|
tag: 'Send signal',
|
|
ref: 'Use signal',
|
|
terminal: 'Terminal',
|
|
keyboard: 'Keyboard',
|
|
}
|
|
|
|
/** Challenges each one builds on. */
|
|
export const NEEDS: Record<string, string[]> = {
|
|
not: [],
|
|
and: ['not'],
|
|
or: ['not'],
|
|
nor: ['or'],
|
|
xor: ['and'],
|
|
xnor: ['xor'],
|
|
and3: ['and'],
|
|
mux: ['and', 'or'],
|
|
demux: ['and'],
|
|
dec2: ['and'],
|
|
ha: ['xor'],
|
|
fa: ['ha', 'or'],
|
|
eq2: ['xnor'],
|
|
add2: ['fa'],
|
|
sr: ['not'],
|
|
dlatch: ['sr'],
|
|
dff: ['dlatch'],
|
|
}
|
|
|
|
/** Basic parts that have to be earned, and the challenge that earns each. */
|
|
export const KIND_UNLOCK: Partial<Record<NodeKind, string>> = {
|
|
high: 'not',
|
|
low: 'not',
|
|
panel: 'and',
|
|
outpanel: 'and',
|
|
hex: 'dec2',
|
|
pixel: 'dec2',
|
|
terminal: 'dec2',
|
|
keyboard: 'dec2',
|
|
bundle: 'add2',
|
|
split: 'add2',
|
|
portin: 'add2',
|
|
portout: 'add2',
|
|
button: 'sr',
|
|
clock: 'dlatch',
|
|
}
|
|
|
|
export const isCampaign = (p: Project) => p.mode === 'campaign'
|
|
|
|
const solved = (p: Project, id: string) => p.solved.includes(id)
|
|
|
|
export function kindUnlocked(p: Project, kind: NodeKind): boolean {
|
|
const by = KIND_UNLOCK[kind]
|
|
return !isCampaign(p) || !by || solved(p, by)
|
|
}
|
|
|
|
/** A challenge can be started once everything it builds on is solved. */
|
|
export function challengeOpen(p: Project, id: string): boolean {
|
|
return !isCampaign(p) || (NEEDS[id] ?? []).every(n => solved(p, n))
|
|
}
|
|
|
|
/** In a campaign, a challenge's component becomes usable as a part once it is solved. */
|
|
export function defUsable(p: Project, def: ComponentDef): boolean {
|
|
return !isCampaign(p) || !def.challenge || solved(p, def.challenge)
|
|
}
|
|
|
|
export const challengeName = (id: string) => CHALLENGES.find(c => c.id === id)?.name ?? id
|
|
|
|
/** What solving a challenge opens up: further challenges and basic parts. */
|
|
export function unlockedBy(p: Project, id: string): { challenges: string[]; kinds: NodeKind[] } {
|
|
const after = { ...p, solved: [...new Set([...p.solved, id])] }
|
|
const challenges = CHALLENGES.filter(c => !solved(p, c.id) && c.id !== id && !challengeOpen(p, c.id) && challengeOpen(after, c.id)).map(c => c.id)
|
|
const kinds = (Object.keys(KIND_UNLOCK) as NodeKind[]).filter(k => KIND_UNLOCK[k] === id && !solved(p, id))
|
|
return { challenges, kinds }
|
|
}
|
|
|
|
/** Share of the campaign done, 0 to 1. */
|
|
export function campaignProgress(p: Project): number {
|
|
return CHALLENGES.filter(c => solved(p, c.id)).length / CHALLENGES.length
|
|
}
|