231 lines
12 KiB
Vue
231 lines
12 KiB
Vue
<script setup lang="ts">
|
||
import { shortCount } from '~/lib/logic/analyze'
|
||
import { CHALLENGES } from '~/lib/logic/challenges'
|
||
import { campaignProgress, challengeName, challengeOpen, defUsable, isCampaign, KIND_UNLOCK, NEEDS } from '~/lib/logic/progress'
|
||
import { weekKey, weeklyId } from '~/lib/logic/puzzles'
|
||
import type { ComponentDef, NodeKind } from '~/lib/logic/types'
|
||
|
||
const ed = useEditor()
|
||
const tab = ref<'parts' | 'challenges'>('parts')
|
||
const query = ref('')
|
||
|
||
const BASICS: { kind: NodeKind; name: string; icon: string; key?: string; hint: string }[] = [
|
||
{ kind: 'nand', name: 'NAND', icon: 'lucide:square-function', key: 'N', hint: 'The one logic gate. Output is 0 only when both inputs are 1.' },
|
||
{ kind: 'input', name: 'Input', icon: 'lucide:toggle-left', key: 'I', hint: 'A switch. Inside a component it becomes an input pin.' },
|
||
{ kind: 'output', name: 'Output', icon: 'lucide:log-out', key: 'O', hint: 'Shows a value. Inside a component it becomes an output pin.' },
|
||
{ kind: 'lamp', name: 'Lamp', icon: 'lucide:lightbulb', key: 'L', hint: 'Lights up at 1.' },
|
||
{ kind: 'button', name: 'Push button', icon: 'lucide:circle-dot', key: 'B', hint: '1 while held down.' },
|
||
{ kind: 'panel', name: 'Input panel', icon: 'lucide:sliders-vertical', key: 'P', hint: 'A column of switches, buttons and clocks. On a component, "Attach input panel" makes one for its inputs.' },
|
||
{ kind: 'clock', name: 'Clock', icon: 'lucide:audio-waveform', key: 'C', hint: 'Square wave. Speed and pause are in the top bar.' },
|
||
{ kind: 'high', name: 'Constant 1', icon: 'lucide:plus', hint: 'Always 1, like the supply rail.' },
|
||
{ kind: 'low', name: 'Ground (0)', icon: 'lucide:arrow-down-to-line', hint: 'Always 0.' },
|
||
{ kind: 'hex', name: 'Hex display', icon: 'lucide:binary', key: 'H', hint: 'Shows 4 bits as 0–F.' },
|
||
{ kind: 'pixel', name: 'Pixel', icon: 'lucide:square', key: 'X', hint: 'One square of a screen, lit at 1. Pixels inside a component show on its box as one screen: build your own LCD.' },
|
||
{ kind: 'bundle', name: 'Cable bundle', icon: 'lucide:merge', hint: 'Gathers several wires into one cable. Or select wires and press "Bundle into cable".' },
|
||
{ kind: 'split', name: 'Cable split', icon: 'lucide:split', hint: 'Fans a cable out into its wires again.' },
|
||
{ kind: 'note', name: 'Note', icon: 'lucide:sticky-note', key: 'T', hint: 'Text label for your drawing.' },
|
||
]
|
||
|
||
const GROUP_ORDER = ['', 'Puzzles', 'Gates', 'Routing', 'Arithmetic', 'Memory', 'Displays', 'Demos']
|
||
|
||
const groups = computed(() => {
|
||
const q = query.value.trim().toLowerCase()
|
||
const map = new Map<string, ComponentDef[]>()
|
||
for (const d of ed.project.value.components) {
|
||
if (q && !d.name.toLowerCase().includes(q)) continue
|
||
const g = d.group?.trim() ?? ''
|
||
if (!map.has(g)) map.set(g, [])
|
||
map.get(g)!.push(d)
|
||
}
|
||
const rank = (g: string) => (GROUP_ORDER.includes(g) ? GROUP_ORDER.indexOf(g) : GROUP_ORDER.length)
|
||
return [...map.entries()]
|
||
.sort(([a], [b]) => rank(a) - rank(b) || a.localeCompare(b))
|
||
.map(([name, defs]) => ({ name: name || 'My components', defs }))
|
||
})
|
||
|
||
function pick(kind: NodeKind, chip?: string) {
|
||
if (chip && !ed.canPlace(chip)) return
|
||
if (!ed.canPlaceKind(kind)) {
|
||
ed.play('error')
|
||
return ed.notify(lockedHint(kind), 'error')
|
||
}
|
||
ed.placing.value = { kind, chip }
|
||
}
|
||
|
||
function onDragStart(e: DragEvent, kind: NodeKind, chip?: string) {
|
||
if (chip && !ed.canPlace(chip)) {
|
||
e.preventDefault()
|
||
return
|
||
}
|
||
e.dataTransfer?.setData('application/x-logigate', JSON.stringify({ kind, chip }))
|
||
if (e.dataTransfer) e.dataTransfer.effectAllowed = 'copy'
|
||
}
|
||
|
||
const solved = computed(() => new Set(ed.project.value.solved))
|
||
const best = computed(() => ed.project.value.best ?? {})
|
||
const solvedCount = computed(() => CHALLENGES.filter(c => solved.value.has(c.id)).length)
|
||
const campaign = computed(() => isCampaign(ed.project.value))
|
||
const progress = computed(() => campaignProgress(ed.project.value))
|
||
const open = (id: string) => challengeOpen(ed.project.value, id)
|
||
const needsText = (id: string) => `Solve ${(NEEDS[id] ?? []).filter(n => !solved.value.has(n)).map(challengeName).join(' and ')} first`
|
||
const puzzles = computed(() => (ed.project.value.puzzles ?? []).filter(p => !p.id.startsWith('week:') || p.id === weeklyId(weekKey())))
|
||
const weeklyStarted = computed(() => ed.project.value.components.some(d => d.challenge === weeklyId(weekKey())))
|
||
|
||
function lockedHint(kind: NodeKind) {
|
||
const by = KIND_UNLOCK[kind]
|
||
return by ? `Solve the ${challengeName(by)} challenge to unlock this part` : ''
|
||
}
|
||
|
||
function openWeekly() {
|
||
const spec = ed.project.value.puzzles?.find(p => p.id === weeklyId(weekKey()))
|
||
if (spec) ed.beginPuzzle(spec)
|
||
else void ed.openPuzzle('weekly')
|
||
}
|
||
|
||
function compTitle(d: ComponentDef) {
|
||
if (d.id === ed.def.value.id) return 'You are editing this one'
|
||
if (!defUsable(ed.project.value, d)) return 'Solve this challenge to use it as a part'
|
||
if (!ed.canPlace(d.id)) return 'Contains the circuit you are editing'
|
||
return d.description || 'Click, then click on the canvas to place'
|
||
}
|
||
const started = computed(() => new Map(ed.project.value.components.filter(d => d.challenge).map(d => [d.challenge!, d])))
|
||
</script>
|
||
|
||
<template>
|
||
<aside class="palette">
|
||
<div class="tabs">
|
||
<button :class="{ active: tab === 'parts' }" @click="tab = 'parts'">Parts</button>
|
||
<button :class="{ active: tab === 'challenges' }" @click="tab = 'challenges'">
|
||
Challenges
|
||
<span class="count">{{ solvedCount }}/{{ CHALLENGES.length }}</span>
|
||
</button>
|
||
</div>
|
||
|
||
<div v-if="tab === 'parts'" class="scroll">
|
||
<h3>Basics</h3>
|
||
<ul class="items">
|
||
<li v-for="item in BASICS" :key="item.kind">
|
||
<button
|
||
class="item"
|
||
:class="{ active: ed.placing.value?.kind === item.kind && !ed.placing.value?.chip, blocked: !ed.canPlaceKind(item.kind) }"
|
||
:title="ed.canPlaceKind(item.kind) ? item.hint : lockedHint(item.kind)"
|
||
:draggable="ed.canPlaceKind(item.kind)"
|
||
@click="pick(item.kind)"
|
||
@dragstart="onDragStart($event, item.kind)"
|
||
>
|
||
<span v-if="item.kind === 'nand'" class="nand-badge">&</span>
|
||
<Icon v-else :name="item.icon" class="size-4 shrink-0 text-(--muted)" />
|
||
<span class="grow truncate">{{ item.name }}</span>
|
||
<Icon v-if="!ed.canPlaceKind(item.kind)" name="lucide:lock" class="size-3.5 shrink-0 text-(--muted)" />
|
||
<kbd v-else-if="item.key">{{ item.key }}</kbd>
|
||
</button>
|
||
</li>
|
||
</ul>
|
||
|
||
<div class="search">
|
||
<Icon name="lucide:search" class="size-3.5 text-(--muted)" />
|
||
<input v-model="query" placeholder="Find component" />
|
||
</div>
|
||
|
||
<template v-for="g in groups" :key="g.name">
|
||
<h3>{{ g.name }}</h3>
|
||
<ul class="items">
|
||
<li v-for="d in g.defs" :key="d.id" class="comp-row">
|
||
<button
|
||
class="item"
|
||
:class="{ active: ed.placing.value?.chip === d.id, current: d.id === ed.def.value.id, blocked: !ed.canPlace(d.id) }"
|
||
:title="compTitle(d)"
|
||
:draggable="ed.canPlace(d.id)"
|
||
@click="pick('chip', d.id)"
|
||
@dragstart="onDragStart($event, 'chip', d.id)"
|
||
>
|
||
<Icon :name="d.challenge && solved.has(d.challenge) ? 'lucide:badge-check' : 'lucide:box'" class="size-4 shrink-0" :class="d.challenge && solved.has(d.challenge) ? 'text-(--on)' : 'text-(--muted)'" />
|
||
<span class="grow truncate">{{ d.name }}</span>
|
||
<span v-if="d.id === ed.def.value.id" class="editing">editing</span>
|
||
<span v-else-if="ed.transistors(d.id)" class="tcount" :title="`${ed.transistors(d.id).toLocaleString()} transistors`">{{ shortCount(ed.transistors(d.id)) }}T</span>
|
||
</button>
|
||
<button v-if="d.id !== ed.def.value.id" class="open" title="Open and edit" @click="ed.open(d.id)">
|
||
<Icon name="lucide:pencil" class="size-3.5" />
|
||
</button>
|
||
</li>
|
||
</ul>
|
||
</template>
|
||
|
||
<div class="grid gap-2 px-3 pt-2 pb-4">
|
||
<button class="btn w-full" @click="ed.createComponent()">
|
||
<Icon name="lucide:plus" class="size-4" /> New component
|
||
</button>
|
||
<button class="btn btn-ghost w-full" title="NOT, AND, … up to a 4-bit counter, all built from NAND" @click="ed.loadExamples()">
|
||
<Icon name="lucide:library" class="size-4" /> Add example parts
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div v-else class="scroll">
|
||
<div v-if="campaign" class="campaign">
|
||
<div class="flex items-center justify-between">
|
||
<span class="font-semibold">Campaign</span>
|
||
<span class="tabular-nums text-(--muted)">{{ solvedCount }} / {{ CHALLENGES.length }}</span>
|
||
</div>
|
||
<div class="bar"><span :style="{ width: `${progress * 100}%` }" /></div>
|
||
<p>Every part you solve becomes yours to use. New parts and challenges open as you go.</p>
|
||
</div>
|
||
<p v-else class="intro">
|
||
Build each part from NAND and the parts you already made. Start a challenge, wire it up, then press
|
||
<strong>Check</strong> on the right.
|
||
</p>
|
||
|
||
<div class="weekly">
|
||
<div class="flex items-center gap-2">
|
||
<Icon name="lucide:calendar-days" class="size-4 shrink-0 text-(--accent)" />
|
||
<span class="grow font-medium">Puzzle of the week</span>
|
||
<button class="btn-sm" @click="openWeekly">{{ weeklyStarted ? 'Open' : 'Play' }}</button>
|
||
</div>
|
||
<p>A new logic puzzle every Monday, the same for everyone. Fewest transistors wins.</p>
|
||
</div>
|
||
|
||
<template v-if="puzzles.length">
|
||
<h3>Puzzles</h3>
|
||
<ul class="challenges">
|
||
<li v-for="p in puzzles" :key="p.id" :class="{ solved: solved.has(p.id) }">
|
||
<div class="flex items-center gap-2">
|
||
<Icon :name="solved.has(p.id) ? 'lucide:circle-check' : 'lucide:puzzle'" class="size-4 shrink-0" :class="solved.has(p.id) ? 'text-(--on)' : 'text-(--muted)'" />
|
||
<span class="grow font-medium">{{ p.name }}</span>
|
||
<button class="btn-sm" @click="ed.beginPuzzle(p)">Open</button>
|
||
</div>
|
||
<p class="pins">
|
||
{{ p.inputs.join(', ') }} → {{ p.outputs.join(', ') }}
|
||
<span v-if="best[p.id] !== undefined" class="best"> · best {{ best[p.id]!.toLocaleString() }} transistors</span>
|
||
</p>
|
||
</li>
|
||
</ul>
|
||
</template>
|
||
|
||
<h3>Challenges</h3>
|
||
<ul class="challenges">
|
||
<li v-for="c in CHALLENGES" :key="c.id" :class="{ solved: solved.has(c.id), locked: !open(c.id) }">
|
||
<div class="flex items-center gap-2">
|
||
<Icon
|
||
:name="solved.has(c.id) ? 'lucide:circle-check' : open(c.id) ? 'lucide:circle' : 'lucide:lock'"
|
||
class="size-4 shrink-0"
|
||
:class="solved.has(c.id) ? 'text-(--on)' : 'text-(--muted)'"
|
||
/>
|
||
<span class="grow font-medium">{{ c.name }}</span>
|
||
<button class="btn-sm" :disabled="!open(c.id) && !started.has(c.id)" :title="open(c.id) ? '' : needsText(c.id)" @click="ed.beginChallenge(c)">
|
||
{{ started.has(c.id) ? 'Open' : 'Start' }}
|
||
</button>
|
||
</div>
|
||
<p v-if="!open(c.id)" class="needs">{{ needsText(c.id) }}</p>
|
||
<p>{{ c.goal }}</p>
|
||
<p class="pins">
|
||
{{ c.inputs.join(', ') }} → {{ c.outputs.join(', ') }}
|
||
<span v-if="best[c.id] !== undefined" class="best" title="Fewest transistors in a passing solution so far">
|
||
· best {{ best[c.id]!.toLocaleString() }} transistors
|
||
</span>
|
||
</p>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</aside>
|
||
</template>
|