Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / games / logicgate / AGENTS.md

AGENTS.md6.0 KiBRenderedCodeRaw
1# AGENTS.md — games/logicgate/ (':games:logicgate')
2
3> Scoped supplement. Global rules in root `AGENTS.md` apply. Stay inside `games/logicgate/` + allowed shared modules. Do not root-scan.
4
5_Install: ./install games/logicgate (dev by default)._
6<!-- last-reviewed: 2026-11-01 -->
7- Gradle: :games:logicgate / dir: games/logicgate/
8- Package roots present: domain, data, platform, ui
9- Entry files: MainActivity.kt, Route.kt, Navigation.kt
10- Deps: :sdk:games
11- Metadata: metadata_data/games-logicgate.md (present)
12- Rust: no / screenshotTest: yes
13
14## Overview
15**Purpose:** NAND-to-computer circuit puzzle. Two bottom-bar tabs. **Computer** has 28 levels in 5 chapters (Basics, Routing & buses, Arithmetic, Memory, CPU), each checked by a real cycle-accurate simulator, finishing with a CPU that runs three programs out of RAM. In **Sandbox**, players build their own components (custom pins, unlocked built-ins and other components) and can then use them in any level. 100% offline.
16**Game shape:** `domain/` (pure Kotlin: circuit model, chip catalog, simulator, verifier, levels) → `platform/LogicViewModel` (progression, components, catalog) + `platform/EditorSession` (undo/redo, debounced re-verify on `Dispatchers.Default`) → `ui/` (one-canvas editor, grouped chip tray, test panel, level map, sandbox list). `data/` persists drafts (DataStore key `logicgate2_circuits`), components (DataStore key `logicgate2_components`) and best costs (`LevelStatsRepository` prefs `logicgate2_stats`).
17**Entry points:** `Route` → `MainActivity.kt` → `Navigation.kt` → `ui/GamePage.kt` / `ui/ComponentPage.kt` → `ui/GameScreen.kt` → `platform/EditorSession.kt` → `domain/sim/Verifier.kt`.
18**Model:**
19- Signals are `Int` bit masks on typed 1/4/8-bit pins. SPLIT/JOIN chips convert widths, and wires must match widths.
20- Level I/O are real `LevelInput`/`LevelOutput` nodes. Unconnected inputs read 0.
21- One tick of an implicit global clock = settle, then latch. Feedback loops iterate Gauss-Seidel (cap 64 passes → oscillation). State-only chips (DFF/REG/PC) break loops.
22- Solving a level unlocks its chip as a trusted native behaviour (`domain/model/Chips.kt`). Level pins, tests and par for reward levels derive from that chip (`ChipLevel.kt`), so they cannot drift.
23- Sandbox components (`domain/model/CustomChip.kt`, ids `custom:<uuid>`) are a `Board` (circuit + editable `LevelIo`) behind `Behavior.Composite`. `ChipCatalog` resolves built-ins + components (costs summed recursively; self-containing components resolve to null). `domain/sim/Flattener.kt` inlines composites before simulating, so feedback across component boundaries is exact. Chip pin order is the board's pin nodes sorted top-to-bottom. When an edit reorders or removes pins, `remapChipPins` rewires every other component and level draft that uses the component.
24- Sandbox tray = `LevelRegistry.unlockedChips(completed)` + components that don't contain the open one + `PinChips` (pseudo-chips `pin:in|out:{1,4,8}` that add board pins). Level tray = `allowedChips(level)` + every component. Tray tabs come from `domain/levels/ChipGroups.kt` (Pins, built-ins by chapter, player group names).
25- ISA (`domain/model/Isa.kt`): `op[4]|operand[4]`, LDA STA ADD SUB LDI JMP JZ HLT, one instruction per tick via dual-port RAM16.
26**Gotchas:**
27- Pars are asserted equal to the reference solution costs in `src/test/.../domain/LevelSolutionsTest.kt`. Change a chip's `nandCost` and its reference together.
28- `ui/canvas/` is world-unit geometry (≈dp at 1×). `CameraState.scale` already includes density, so canvas text uses `sizeWorld.toSp()`.
29- Nothing edits on finger-down. Chips are created by pulling them up out of the tray (`ChipTray`: an upward drag claims the gesture, a sideways one scrolls) or from `ui/ChipInfoSheet.kt`, opened by tapping a tray card (chip names and blurbs are `strings.xml` resources mapped in `domain/model/ChipInfo.kt`; UI resolves names via `ui/ChipLabels.kt`). Gates are deleted by dropping them back on the bottom area (`ui/canvas/TrashZone.kt`) or from the selection bar.
30- Level titles, briefs and hints are `@StringRes` ids (`lg_level_<id>_{title,brief,hint}`); gate-named level titles alias the chip name string.
31- Test rows carry plain-language titles (`domain/levels/RowTitles.kt`, e.g. `1 OR 1 = 1`), which are shown instead of row numbers.
32
33## Key files
34### domain/ — pure, unit-tested
35- domain/model/Circuit.kt — nodes, pins, wires, immutable edits, `sanitized`
36- domain/model/Board.kt (circuit + io, pin add/remove/rename), domain/model/CustomChip.kt (+ `PinChips`, `pinRemap`, `remapChipPins`), domain/model/ChipCatalog.kt
37- domain/model/ChipSpec.kt, domain/model/Chips.kt, domain/model/ChipInfo.kt (chip name + blurb string ids), domain/model/Isa.kt
38- domain/sim/Simulator.kt, domain/sim/Scheduler.kt — tick semantics, SCC ordering
39- domain/sim/Verifier.kt, domain/sim/TestSpec.kt, domain/sim/Scoring.kt, domain/sim/Flattener.kt
40- domain/levels/ChipGroups.kt (tray tabs)
41- domain/levels/LevelRegistry.kt + {Basics,Routing,Arithmetic,Memory,Cpu}Levels.kt
42### data/ + platform/
43- data/CircuitStore.kt, data/ComponentStore.kt, data/ProgressStore.kt
44- platform/LogicUiContract.kt (state + `LogicActions.Noop`, `EditorMode` Level/Sandbox), platform/EditorSession.kt, platform/LogicViewModel.kt
45- platform/LogicAchievements.kt (`assets/achievements.json`, ids `lg2_*`), platform/AppBackupAgent.kt
46### ui/
47- ui/GameScreen.kt, ui/CircuitCanvas.kt, ui/ChipTray.kt, ui/TestPanel.kt, ui/SelectionBar.kt
48- ui/BriefSheet.kt, ui/ChipInfoSheet.kt, ui/PinLine.kt, ui/BusValueDialog.kt, ui/LevelCompleteOverlay.kt, ui/ProgressionScreen.kt, ui/StarRating.kt
49- ui/SandboxPage.kt, ui/SandboxScreen.kt, ui/ComponentPage.kt, ui/ComponentSheet.kt (name, group, pins, delete)
50- ui/canvas/ — Camera, CameraState, GateGeometry (layout), HitTest, CanvasGestures, CanvasController, GatePainter, GateShapes
51
52## Verify (this module only)
53```
54./gradlew :games:logicgate:testDevUnitTest
55./gradlew :games:logicgate:compileDevKotlin
56./check games/logicgate
57./gradlew :games:logicgate:checkMetadata
58```