diff --git a/README.md b/README.md index f55ad23..a3fa5ce 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,9 @@ Nuxt 4 (client-only editor, `ssr: false`), Tailwind CSS v4, Lucide icons via `@n ## What it does - Parts: NAND, input switch, output pin, lamp, push button, clock, constant 1, ground, hex display, pixel, cable bundle and split, input panel, note. +- Campaign (Projects → New campaign): you start with NAND, inputs, outputs, lamps and notes, and no examples. Each challenge opens once the ones it builds on are solved (NOT → AND, OR, SR Latch → …), and your own solution becomes a part you can place. Other basic parts unlock on the way: constants with NOT, the input panel with AND, hex display and pixel with the decoder, cables with Adder 2, the button with the SR latch, the clock with the D latch. Rules in [app/lib/logic/progress.ts](app/lib/logic/progress.ts). Sandbox projects keep everything open. +- Puzzles: "Publish as puzzle" (This circuit) turns a circuit into a link `/puzzle/`. Only its behaviour is published, as a truth table, or for circuits with memory as a run of input changes from power-on; the circuit stays private. Whoever opens the link gets the puzzle in their project, sees the target and the author's transistor count, and after a pass their solution goes to the server, which re-runs the checks and counts the transistors itself (with a fixed work budget) and keeps each player's best. The board shows everyone's transistor counts and your place. The puzzle of the week is generated from the ISO week, the same for everyone: three or four inputs, one or two outputs, each depending on every input. Players are anonymous ids per browser. +- Sound (speaker in the top bar; volume and clock ticks under Display): clicks for placing, wiring, switches and buttons, a tick per clock cycle up to 8 Hz (off by default), and jingles for a solve, a failed check, unlocks and new records. All synthesised with Web Audio, no sound files ([app/lib/audio.ts](app/lib/audio.ts)). - Transistors: every component shows how many it takes, counted as a CMOS NAND (4 transistors) per gate at every depth: in the palette, its settings and the circuit stats. Each challenge keeps your record, the fewest transistors of any passing solution, and says so when you beat it. - Cables: a bundle gathers 2 to 32 wires into one thick cable, a split fans it out again in the same order; one bundle can feed several splits. Select some wires and press "Bundle into cable" to have both ends placed for you (the cable takes the wires' colour); "Remove cable" turns it back into single wires. A cable end only joins another cable end, and the cable shows its width. - Pixels and screens: a pixel is a one-grid square that lights at 1, in five colours. Inside a component all pixels show on its box as one screen without gaps (every column and row they sit on becomes one of the screen), so you build your own LCD; see "Screen 4×4" and "Demo: Screen". @@ -38,6 +41,7 @@ All in [app/lib/logic/](app/lib/logic/), plain TypeScript with no Vue dependency - `analyze.ts` builds truth tables, counts NANDs, computes the longest gate path, and runs challenge tests. - `examples.ts` builds the example library with a small builder; `ops.ts` holds project edits (package selection, copy/paste, prune dangling wires, recursion check). - `routing.ts` lays out all wires of a circuit (free lanes, detours, separation, crossings, and the tidy layout). +- `progress.ts` holds the campaign rules; `puzzles.ts` makes, checks and scores puzzles and generates the weekly one (shared with the server, [server/api/puzzles/](server/api/puzzles/), storage in `data/puzzles`). - `cables.ts` bundles wires into cables and takes them apart again. - `panels.ts` docks input panels to components and keeps them in step; `labels.ts` does series names and names taken from pins. - `wires.ts` edits wire routes (segment and corner drags, splits, branches); `project.ts` validates project data for both the browser and the server; `storage.ts` is the browser's project library. diff --git a/app/assets/css/main.css b/app/assets/css/main.css index 2d6c031..4242eb2 100644 --- a/app/assets/css/main.css +++ b/app/assets/css/main.css @@ -1712,3 +1712,83 @@ kbd { .challenge .score strong { color: var(--on); } + +/* ── Campaign and puzzles (palette) ─────────────────────────────── */ +.campaign, +.weekly { + margin: 12px 12px 4px; + padding: 10px 12px; + border: 1px solid var(--border); + border-radius: 8px; + background: var(--panel-2); + font-size: 12px; +} + +.campaign p, +.weekly p { + margin-top: 6px; + color: var(--muted); + line-height: 1.45; +} + +.weekly { + border-color: rgb(76 194 255 / 0.35); +} + +.campaign .bar { + height: 6px; + margin-top: 8px; + border-radius: 3px; + background: var(--bg); + overflow: hidden; +} + +.campaign .bar span { + display: block; + height: 100%; + background: var(--on); + border-radius: 3px; + transition: width 0.6s cubic-bezier(0.2, 0.8, 0.2, 1); +} + +.challenges li.locked { + opacity: 0.55; +} + +.challenges .needs { + color: var(--lamp); +} + +.btn-sm:disabled { + opacity: 0.45; + cursor: not-allowed; +} + +/* Unlocks after a solve, and the puzzle's target. */ +.challenge .unlock { + display: flex; + gap: 8px; + margin-top: 8px; + padding: 8px 10px; + border-radius: 6px; + font-size: 12px; + color: var(--lamp); + background: rgb(255 211 77 / 0.08); + border: 1px solid rgb(255 211 77 / 0.3); + animation: unlock-in 0.5s cubic-bezier(0.2, 0.8, 0.2, 1); +} + +.challenge .unlock p + p { + margin-top: 2px; +} + +@keyframes unlock-in { + from { + opacity: 0; + transform: translateY(4px) scale(0.98); + } +} + +.target-table .truth { + max-height: 220px; +} diff --git a/app/components/editor/App.vue b/app/components/editor/App.vue index 0caf70d..507643c 100644 --- a/app/components/editor/App.vue +++ b/app/components/editor/App.vue @@ -40,6 +40,7 @@ onMounted(() => { Click the pin {{ link.dir === 'out' ? 'it goes to' : 'it comes from' }}, or a part to use its next free pin. Esc stops. + diff --git a/app/components/editor/Canvas.vue b/app/components/editor/Canvas.vue index 6316677..7ffe36e 100644 --- a/app/components/editor/Canvas.vue +++ b/app/components/editor/Canvas.vue @@ -203,6 +203,7 @@ function onDrop(e: DragEvent) { if (!raw) return const p = JSON.parse(raw) as { kind: CircuitNode['kind']; chip?: string } if (p.chip && !ed.canPlace(p.chip)) return ed.notify('A component cannot contain itself', 'error') + if (!ed.canPlaceKind(p.kind)) return const [x, y] = originFor(p, toWorld(e)) ed.addNode(p, x, y) } @@ -656,7 +657,7 @@ function onKey(e: KeyboardEvent) { } else if (!mod && !e.altKey && key === 'w' && ed.selection.value.length) ed.startLinking(ed.selection.value, e.shiftKey ? 'in' : undefined) else if (!mod && !e.altKey && key === 'f') fit() else if (!mod && !e.altKey && e.key === '?') ed.showHelp.value = !ed.showHelp.value - else if (!mod && !e.altKey && QUICK[key]) ed.placing.value = { kind: QUICK[key]! } + else if (!mod && !e.altKey && QUICK[key] && ed.canPlaceKind(QUICK[key]!)) ed.placing.value = { kind: QUICK[key]! } } function onKeyUp(e: KeyboardEvent) { diff --git a/app/components/editor/Inspector.vue b/app/components/editor/Inspector.vue index 255e3f1..25d9a0c 100644 --- a/app/components/editor/Inspector.vue +++ b/app/components/editor/Inspector.vue @@ -2,7 +2,7 @@ import type { ChallengeCheck } from '~/composables/useEditor' import { tableMarkdown, type TruthTable } from '~/lib/logic/analyze' import { cableOf } from '~/lib/logic/cables' -import { CHALLENGES } from '~/lib/logic/challenges' +import { apiId, isTable } from '~/lib/logic/puzzles' import { chipPins } from '~/lib/logic/geometry' import { usages } from '~/lib/logic/ops' import { WIRE_COLORS, type CrossingStyle, type SymbolKind } from '~/lib/logic/types' @@ -58,8 +58,29 @@ function setCrossing(c: CrossingStyle) { const pins = computed(() => chipPins(def.value)) const users = computed(() => usages(ed.project.value, def.value.id)) const groupNames = computed(() => [...new Set(ed.project.value.components.map(d => d.group).filter(Boolean))]) -const challenge = computed(() => CHALLENGES.find(c => c.id === def.value.challenge)) +// The challenge or puzzle this circuit is for. +const challenge = computed(() => { + const t = ed.task.value + const c = t?.challenge ?? t?.puzzle + return t && c ? { id: t.id, name: c.name, goal: c.goal, inputs: c.inputs, outputs: c.outputs, puzzle: t.puzzle } : undefined +}) const solved = computed(() => !!challenge.value && ed.project.value.solved.includes(challenge.value.id)) +const board = computed(() => (challenge.value?.puzzle ? ed.boards[challenge.value.id] : undefined)) +// What the puzzle asks for, as a table to read. +const target = computed(() => { + const p = challenge.value?.puzzle + if (!p) return null + return { inputs: p.inputs, outputs: p.outputs, rows: p.steps.map(s => ({ ins: p.inputs.map(l => s.set[l] ?? 0), outs: p.outputs.map(l => s.expect[l] ?? 0), settled: true })) } +}) +watch( + () => challenge.value?.puzzle, + p => { + if (p && !ed.boards[p.id]) void ed.loadBoard(p) + }, + { immediate: true }, +) +const showTarget = ref(true) +const puzzleLink = computed(() => (challenge.value?.puzzle ? `${location.origin}/puzzle/${apiId(challenge.value.puzzle.id)}` : '')) const packName = ref('') function pack() { @@ -163,10 +184,19 @@ watch(() => def.value.id, () => (confirmDelete.value = false))

- Challenge: {{ challenge.name }} + {{ challenge.puzzle ? 'Puzzle' : 'Challenge' }}: {{ challenge.name }}

{{ challenge.goal }}

Pins: {{ challenge.inputs.join(', ') }} → {{ challenge.outputs.join(', ') }}

+

@@ -182,6 +212,21 @@ watch(() => def.value.id, () => (confirmDelete.value = false)) New record! {{ check.record - check.transistors }} fewer than before ({{ check.record }}).

Fewer transistors is better: 4 per NAND gate. Can you make it smaller?

+
+ +
+

Unlocked: {{ check.unlocked.join(', ') }}

+

New challenges: {{ check.nextUp.join(', ') }}

+
+
+
@@ -227,6 +272,14 @@ watch(() => def.value.id, () => (confirmDelete.value = false))

{{ err }}

+

Used in

+ diff --git a/app/components/editor/PublishDialog.vue b/app/components/editor/PublishDialog.vue new file mode 100644 index 0000000..868b38b --- /dev/null +++ b/app/components/editor/PublishDialog.vue @@ -0,0 +1,85 @@ + + +