@clarity-js/engine 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,213 @@
1
- # Temporary Holding Version
1
+ # @clarity-js/engine
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A game engine for AI agents, on [Clarity.js](https://clarity-js.com). Say what the game is in one sentence and
4
+ it starts making it: the kit, the goal and the look are read from the sentence, levels are made and checked,
5
+ bots play every level, what fails is changed, and only then is the game handed over — as a folder you can run.
6
+
7
+ ```sh
8
+ npx clarity game new "Push the tea glasses onto the tray marks"
9
+ ```
10
+
11
+ ```
12
+ "Glasses Tray Marks" (puzzle-grid, flat) in glasses-tray-marks/: game.json, rules.js, index.html, style.css, main.js, package.json, README.md
13
+ Ready: "Glasses Tray Marks" (puzzle-grid)
14
+ l1: random 0%, greedy 50% in 10 moves, planner 100% in 9 moves
15
+ l2: random 0%, greedy 50% in 21 moves, planner 100% in 16 moves
16
+ l3: random 0%, greedy 0%, planner 100% in 22 moves
17
+ ✓ starts and does not break: every bot played every level to its end
18
+ ✓ l1 can be finished: the planner finished 100% of 4 games
19
+ ✓ gets harder: later levels take as long or longer
20
+ ✓ not finished by chance: random moves rarely finish the first level
21
+ ```
22
+
23
+ ```sh
24
+ npm install @clarity-js/engine @ozsarman/clarityjs
25
+ ```
26
+
27
+ ## A game is two files
28
+
29
+ **game.json** is plain data: the kit, the look, the feel, the levels and the checks. An agent writes it and
30
+ reads it; `clarity game validate` says what is wrong in it, with the line and what to write instead.
31
+
32
+ ```json
33
+ {
34
+ "engine": 1,
35
+ "title": "Lantern Night Market",
36
+ "pitch": "Switch off every lantern in the night market",
37
+ "kit": "puzzle-grid",
38
+ "look": { "style": "neon-night", "palette": ["#0b0f2b", "#ff2e88", "#29f0ff", "#b8ff3c", "#ffd23f"], "camera": "top" },
39
+ "feel": { "pace": "steady", "input": ["tap", "arrows"], "device": "any" },
40
+ "rules": "rules.js",
41
+ "levels": [
42
+ { "id": "l1", "goal": "lights", "size": [4, 4], "presses": 3 },
43
+ { "id": "l2", "goal": "lights", "size": [5, 5], "presses": 5 }
44
+ ],
45
+ "checks": { "bots": ["random", "greedy", "planner"], "finish": ">= 0.7" }
46
+ }
47
+ ```
48
+
49
+ **rules.js** is the kit's hooks, changed where the game differs. Every hook is optional; keep it plain data and
50
+ the seeded `ctx.rng` (no DOM, no `Math.random`), so a seed always plays the same game.
51
+
52
+ ```js
53
+ export default {
54
+ setup(state, ctx, level) {}, // after the kit has set the level up
55
+ step(state, dt, ctx) {}, // every 1/60 s
56
+ actions: { hint(state, ctx) {} }, // actions of your own, beside the kit's
57
+ won: (state) => state.used <= 10 && state.cells.every((v) => !v),
58
+ lost: (state) => false,
59
+ score: (state) => state.score,
60
+ }
61
+ ```
62
+
63
+ ## Kits
64
+
65
+ | kit | what it makes | actions |
66
+ |---|---|---|
67
+ | `puzzle-grid` | a board and one move at a time: `match` colours, `push` crates onto marks, switch `lights` off, `slide` tiles into order | `swap(x, y, dir)`, `move(dir)`, `press(x, y)` |
68
+ | `platformer` | run and jump past ledges, coins, walkers and spikes to the door (coyote time, a jump buffer) | `input(dir, jump)` |
69
+ | `top-down` | walk four ways through rooms, swing at chasers and sentries, a key for the locked door, gems, the way out | `input(dx, dy, attack)`, `attack(dx, dy)` |
70
+ | `shmup` | a ship that fires by itself, waves (line, vee, sine, dive, column), aimed shots, power-ups, a boss (ring, fan, spiral) | `input(dx, dy, focus)` |
71
+ | `racing` | a closed track from control points, a car that grips and runs wide, laps, rivals whose pace is tuned by playing | `input(steer, throttle)` |
72
+ | `card-board` | `shed` (match the colour or the number, empty your hand first) or `line` (tokens in a row) against a rival that thinks | `play(i, color)`, `draw()`, `drop(col)` |
73
+ | `explore-3d` | an island drawn as a diorama: find what was lost, ask the one who knows, bring it home; fog, slopes, water | `input(dx, dy, talk)`, `talk()` |
74
+ | `physics-toy` | `topple` a tower with a few throws, or `stack` pieces to a height — on Clarity's own physics | `throw(angle, power)`, `drop(x)` |
75
+ | `blank` | no kit: setup, step, actions and won/lost are all yours | yours |
76
+
77
+ Maps are one string a row. Platformer: `#` ground, `-` ledge, `@` start, `o` coin, `E` walker, `^` spikes,
78
+ `D` door, `C` checkpoint. Top-down: `#` wall, `.` floor, `@` start, `X` way out, `E` chaser, `S` sentry, `k` key,
79
+ `L` locked door, `h` heart, `o` gem. Push: `#` wall, `.` mark, `$` crate, `*` crate on a mark, `@` keeper.
80
+ `kits()` (or `clarity game kits`) lists each kit's settings with their ranges.
81
+
82
+ **Looks** are recipes: `flat`, `pastel`, `pixel`, `neon-night`, `ink-sketch`, `toy-plastic`, `paper-marbling`,
83
+ `low-poly-diorama`, each a palette, a background, an ink and a finish — corners (`radius`), `glow`, `gloss`,
84
+ `outline`, a `pattern` (marble, hatch, bevel) and, in 3D, `shade`. game.json's `look` changes any of them:
85
+ `{ "style": "pastel", "radius": 0.4, "glow": 0.2 }`.
86
+
87
+ ## Your own art
88
+
89
+ The kits draw shapes in the game's look. `platformer`, `top-down` and `shmup` draw pictures instead when
90
+ game.json has `art`: a sheet of frames, and which frames stand for which part of the game.
91
+
92
+ ```json
93
+ "art": {
94
+ "sheet": "art/sheet.png",
95
+ "frame": [16, 16],
96
+ "fps": 8,
97
+ "sprites": {
98
+ "hero": { "idle": 0, "run": [1, 2, 3, 4], "jump": 5, "fall": 6, "size": [1, 1.5] },
99
+ "coin": [10, 11, 12, 13],
100
+ "walker": [14, 15],
101
+ "ground": 16, "grass": 17
102
+ }
103
+ }
104
+ ```
105
+
106
+ `sheet` is the picture's address from the page (or a `data:` address). `frame` cuts it as a grid, numbered across
107
+ and then down from 0; `frames: { "tree": [x, y, width, height] }` names rectangles instead. A part is one frame,
108
+ frames shown in turn, or a list for each thing it does; `size` is how many cells wide and high it is drawn
109
+ (standing on its cell's lower edge), `fps` its own pace. Pixels stay hard (`"smooth": true` for painted art).
110
+ **A part with no picture keeps its shape**, so art can come one piece at a time.
111
+
112
+ | kit | parts |
113
+ |---|---|
114
+ | `platformer` | `hero` (idle, run, jump, fall), `walker`, `coin`, `ground` (earth), `grass` (the top of the ground), `ledge`, `spikes`, `door`, `doorOpen`, `flag`, `flagOn` |
115
+ | `top-down` | `hero` (idle, walk, attack, hurt), `chaser`, `sentry`, `wall`, `wallFront` (a wall with floor below it), `floor`, `locked`, `exit`, `exitOpen`, `key`, `gem`, `heart`, `shot` |
116
+ | `shmup` | `ship`, `foe` (line, vee, sine, dive, column), `boss`, `shot`, `bullet`, `item` |
117
+
118
+ Things that face a way are mirrored by themselves. `clarity game validate` checks the art like the rest of
119
+ game.json: a part the kit does not have, or a frame that is not there, is told with its line and what to write.
120
+ `clarity gen sprite` makes sheets. In code: `createArt(game.art, { image })` → `art.draw(context, part, x, y, w,
121
+ h, { state, t, flip })`, `ART_ROLES`, `checkArt(art, kit)`.
122
+
123
+ For a game drawn by the GPU instead (thousands of sprites, tile maps from the Tiled editor, a camera that
124
+ follows, parallax): `<Sprites>`, `<Tilemap>` and `<Camera2D>` in `@clarity-js/gl`.
125
+
126
+ ## Bots, checks and fitting
127
+
128
+ Three bots play every kit: `random`, `greedy` (the move that does best now) and `planner` (search for push and
129
+ slide, algebra for lights, two moves ahead for match, candidate jumps for the platformer, ways through the rooms
130
+ for top-down, moves tried ahead against the bullets for the shmup, the racing line and the braking points for a
131
+ race, games played out to the end for cards and a look ahead for tokens, the unknown explored for explore-3d, and
132
+ throws tried on a copy of the physics for the toy). `play()` gives each
133
+ level's finish rate, time or moves and where the runner died, and the checks:
134
+
135
+ - **starts and does not break** — every bot played every level to its end;
136
+ - **every level can be finished** — the planner finishes at least `checks.finish` of its games; a failing
137
+ level comes with the smallest change that makes it pass (more moves, a lower target, more time);
138
+ - **gets harder**, **not finished by chance** — soft: said, not failed.
139
+
140
+ `fit()` plays, applies those changes and plays again until every check passes.
141
+
142
+ ## In code
143
+
144
+ ```js
145
+ import { newGame, loadGame, play, fit, kits, validateGame } from '@clarity-js/engine'
146
+
147
+ const { game, rules } = newGame({ pitch: 'A short platformer across neon rooftops' })
148
+ const report = play(loadGame(game)) // { ok, checks, levels }
149
+ const { game: better } = fit(game) // until it is ready
150
+ ```
151
+
152
+ ```js
153
+ import { mountGame } from '@clarity-js/engine/view'
154
+ import game from './game.json'
155
+ import rules from './rules.js'
156
+
157
+ mountGame(document.getElementById('app'), game, rules)
158
+ ```
159
+
160
+ The view draws on a canvas in the game's look and is played with keys, touch (a pad on a phone), a gamepad —
161
+ or by an agent: its `__clarity_ai__` contract reads `state`, `level`, `levels`, `title` and `legal` and does the
162
+ kit's actions, `restart` and `next`, which is what `clarity mcp` reads and plays. It steps on Clarity's shared
163
+ clock, so a test or an agent can hold time and step it.
164
+
165
+ `@clarity-js/engine/editor` is a level editor for a page: maps are painted cell by cell, every other level is a
166
+ form of its numbers; Play tries the level where it is edited, Ask the bots says how often each finishes it and
167
+ what to change, and Download gives back game.json. clarity-js.com/make is that editor with a game maker.
168
+
169
+ ```js
170
+ import { mountEditor } from '@clarity-js/engine/editor'
171
+ mountEditor(document.getElementById('editor'), game, rules, { onChange: (game) => save(game) })
172
+ ```
173
+
174
+ `@clarity-js/engine/node` writes and reads game folders: `createGame({ pitch, dir })`, `readGame(dir)`,
175
+ `playGame(dir)`, `formatReport(report)`.
176
+
177
+ ## Sound
178
+
179
+ Music is composed for the game from its title and a mood (`calm`, `playful`, `tense`, or `none`): chords, a bass,
180
+ a melody in the mood's scale and drums for the livelier ones, scheduled on Web Audio's clock. Effects are
181
+ synthesized for what happens (a jump, a coin, a hit, a cleared row, a win). No files and nobody's samples: the
182
+ same seed is the same tune. `"sound": { "music": "calm", "sfx": true, "volume": 0.6 }`; M (or the speaker button)
183
+ mutes. `compose({ seed, mood })` gives the notes as data; `renderMusic(song, seconds)` draws them offline.
184
+
185
+ ## Export
186
+
187
+ ```sh
188
+ npx clarity game export --target html # one file: code, look and sound inside; opens from the disk
189
+ npx clarity game export --target itch # a zip for itch.io (index.html at its root, paths relative)
190
+ npx clarity game export --target pwa # installable: a manifest, icons from the palette, offline
191
+ npx clarity game export --target desktop # an Electron shell: npm install && npm start
192
+ npx clarity game export --target capacitor # a phone app's www/: npx cap add android
193
+ npx clarity game export --target quest # installable in the Quest's browser, full screen
194
+ npx clarity game export --target web # a static folder
195
+ ```
196
+
197
+ Every target comes from one build of the folder (its Vite, or esbuild). Electron and Capacitor are not
198
+ downloaded: the folder has their package.json, and installing is the next step.
199
+
200
+ ## The editor and an agent
201
+
202
+ The editor's Easier and Harder take the kit's smallest change (`levelChanges(game, level, 'harder')`) and ask the
203
+ bots again. A request written in it ("hide a gem behind the first door") waits in its contract: an agent
204
+ connected through `clarity mcp` reads `requests` and answers with `setLevel(index, level, note)`, which is checked
205
+ like any game.json and comes in as a step that Undo takes back.
206
+
207
+ ## For an agent
208
+
209
+ `npx clarity mcp --make` gives an agent `game_kits`, `game_new`, `game_validate`, `game_play` and `game_export`
210
+ beside the tools for writing Clarity. `npx clarity game kits | new | play | validate | export` does the same from a
211
+ shell, with `--json` for a script.
212
+
213
+ MIT © Özdemir Şarman
package/index.d.ts ADDED
@@ -0,0 +1,206 @@
1
+ // @clarity-js/engine — a game engine for AI agents, on Clarity.js.
2
+
3
+ /** game.json: plain data. The kit, the look, the levels and the checks; the rules are in rules.js. */
4
+ export interface Game {
5
+ engine: 1;
6
+ title: string;
7
+ pitch?: string;
8
+ kit: KitName;
9
+ look?: { style?: Style; palette?: string[]; camera?: 'top' | 'side' | 'follow' | 'diorama' } & Partial<Recipe>;
10
+ feel?: { pace?: 'calm' | 'steady' | 'fast'; input?: string[]; device?: 'phone-first' | 'any' };
11
+ rules: string;
12
+ settings?: Record<string, number>;
13
+ levels?: Level[];
14
+ checks?: { bots?: BotName[]; finish?: string | number };
15
+ sound?: { music?: 'calm' | 'tense' | 'playful' | 'none'; sfx?: boolean; volume?: number };
16
+ text?: Record<string, string>;
17
+ splash?: unknown;
18
+ credits?: unknown;
19
+ /** Pictures in place of the kit's shapes (platformer, top-down, shmup): see createArt(). */
20
+ art?: GameArt;
21
+ }
22
+
23
+ /** A frame of the art's sheet: its number in the grid, or its name in `frames`. */
24
+ export type ArtFrame = number | string;
25
+ export interface GameArt {
26
+ /** The picture's address, from the page (or a data: address). */
27
+ sheet: string;
28
+ /** Cut as a grid of [width, height] pixels, numbered across then down from 0. */
29
+ frame?: [number, number];
30
+ /** Or rectangles by name: [x, y, width, height]. */
31
+ frames?: Record<string, [number, number, number, number]>;
32
+ fps?: number;
33
+ /** true: smoothed when scaled (painted art). Default: hard pixels. */
34
+ smooth?: boolean;
35
+ /** Which frames stand for which part of the game: one frame, frames in turn, or a list for each thing the part does ({ idle, run, …, size: [w, h] in cells, fps }). */
36
+ sprites: Record<string, ArtFrame | ArtFrame[] | ({ size?: [number, number]; fps?: number } & Record<string, ArtFrame | ArtFrame[] | [number, number] | number | undefined>)>;
37
+ }
38
+ export interface Art {
39
+ readonly ready: boolean;
40
+ has(role: string): boolean;
41
+ frames(role: string, state?: string | null): { frames: ArtFrame[]; fps: number; size: [number, number] | null } | null;
42
+ /** Draw a part in a box. → false when there is no picture for it (draw the shape instead). */
43
+ draw(c: CanvasRenderingContext2D, role: string, x: number, y: number, w: number, h: number, options?: { state?: string | null; t?: number; flip?: boolean; key?: number }): boolean;
44
+ }
45
+ /** A game's art, ready to draw (null: the game has none). image: a picture already loaded, in place of fetching `sheet`. */
46
+ export function createArt(spec: GameArt | null | undefined, options?: { base?: string | null; image?: CanvasImageSource | null; onLoad?: () => void }): Art | null;
47
+ /** What is wrong with a game's "art": [path, message, fix?] rows. */
48
+ export function checkArt(art: unknown, kit: KitName): Array<[string, string, string?]>;
49
+ /** The parts of each kit a picture can stand for, and what the ones that do several things do. */
50
+ export const ART_ROLES: Record<'platformer' | 'top-down' | 'shmup', Record<string, string[]>>;
51
+
52
+ export type KitName = 'puzzle-grid' | 'platformer' | 'top-down' | 'shmup' | 'racing' | 'card-board' | 'explore-3d' | 'physics-toy' | 'blank';
53
+ export type Style = 'paper-marbling' | 'pixel' | 'neon-night' | 'ink-sketch' | 'toy-plastic' | 'pastel' | 'flat' | 'low-poly-diorama';
54
+ /** A look's finish: corners (a share of a shape's size), glow, gloss, outline, a pattern, the shading of sides in 3D. */
55
+ export interface Recipe { background: string; ink: string; radius: number; glow: number; gloss: number; outline: number; pattern: 'marble' | 'hatch' | 'bevel' | null; shade: number }
56
+ export type BotName = 'random' | 'greedy' | 'planner';
57
+
58
+ /**
59
+ * A level. puzzle-grid: goal, size, moves, target, colors, map (push), presses (lights), shuffle (slide).
60
+ * platformer: map, coins, time. top-down: map, gems, time. shmup: length, waves, boss. racing: track, width, laps,
61
+ * rivals, rivalPace, place. card-board: goal (shed: colors, ranks, hand, rivals, rival, wilds, extra; line: size,
62
+ * inRow, rival). explore-3d: size, heights, things, goal {x, y}, start, fog. physics-toy: goal (topple: blocks,
63
+ * targets, shots; stack: platform, pieces, height).
64
+ */
65
+ export interface Level {
66
+ id?: string;
67
+ goal?: 'match' | 'push' | 'lights' | 'slide' | 'shed' | 'line' | 'topple' | 'stack' | { x: number; y: number };
68
+ size?: [number, number];
69
+ moves?: number;
70
+ target?: number;
71
+ colors?: number;
72
+ presses?: number;
73
+ shuffle?: number;
74
+ /** One string a row. Push: # wall, . mark, $ crate, * crate on a mark, @ keeper. Platformer: # ground, - ledge, @ start, o coin, E walker, ^ spikes, D door, C checkpoint. */
75
+ map?: string[];
76
+ coins?: number;
77
+ time?: number;
78
+ [key: string]: unknown;
79
+ }
80
+
81
+ /** The hooks of rules.js (its default export). Every one is optional. ctx: { rng, emit(event), t }. */
82
+ export interface Rules {
83
+ setup?(state: any, ctx: Ctx, level: Level): void;
84
+ step?(state: any, dt: number, ctx: Ctx): void;
85
+ actions?: Record<string, (state: any, ctx: Ctx, ...args: any[]) => unknown>;
86
+ won?(state: any): boolean;
87
+ lost?(state: any): boolean;
88
+ score?(state: any): number;
89
+ value?(state: any): number;
90
+ legal?(state: any): Array<{ act: string; args: any[] }>;
91
+ report?(state: any): any;
92
+ }
93
+ export interface Ctx { rng: (() => number) & { int(n: number): number; pick<T>(list: T[]): T }; emit(event: object): void; t: number; }
94
+
95
+ export interface Mistake { path: string; message: string; fix?: string; line?: number | null }
96
+ export interface Validation { ok: boolean; errors: Mistake[]; warnings: Mistake[]; game: Game | null }
97
+
98
+ /** A simulation of one level (createSim's, with legal() and value() for the bots). */
99
+ export interface GameSim {
100
+ seed: any; t: number; state: any; log: Array<{ t: number; act: string; args: any[] }>; events: any[]; actions: string[];
101
+ kit: KitCard;
102
+ step(dt: number): void;
103
+ act(name: string, ...args: any[]): unknown;
104
+ report(): any;
105
+ on(fn: (event: any) => void): () => void;
106
+ snapshot(): object;
107
+ restore(snap: object): GameSim;
108
+ legal?(): Array<{ act: string; args: any[] }>;
109
+ value(): number;
110
+ sense?(): { dir: number; wall: boolean; gap: boolean; spikes: boolean; walker: boolean; doorAbove: boolean; onGround: boolean };
111
+ }
112
+
113
+ export interface KitCard {
114
+ name: string; title: string; describe: string;
115
+ settings: Record<string, { value: number; min?: number; max?: number; unit?: string }>;
116
+ goals?: string[]; level?: Record<string, Level>; examples: string[]; input: string[];
117
+ continuous?: boolean;
118
+ }
119
+
120
+ export interface LoadedGame { game: Game; kit: KitCard; rules: Rules; levels: Level[]; make(level?: number, seed?: any): GameSim }
121
+
122
+ export interface BotRun { bot: BotName; seed: any; won: boolean; lost: boolean; t: number; moves: number; score: number; deaths: Array<{ x: number; y: number; why: string }>; progress?: number; error?: string }
123
+ export interface Check { name: string; pass: boolean; soft?: boolean; value?: unknown; want?: number; why: string; fix?: { change?: Partial<Level>; rate?: number; say: string } }
124
+ export interface LevelReport { id: string; bots: Record<string, { rate: number; games: number; time: number | null; moves: number | null; score: number; errors: string[] }>; deaths: Array<{ x: number; y: number; why: string }> }
125
+ export interface Report { title: string; kit: string; ok: boolean; checks: Check[]; levels: LevelReport[] }
126
+
127
+ export const KITS: KitName[];
128
+ export const STYLES: Style[];
129
+ export const BOTS: BotName[];
130
+ export const LOOKS: Record<Style, Recipe & { palette: string[] }>;
131
+ /** A game's look: its style's recipe with what game.json changes. */
132
+ export function lookOf(game: Game): Recipe & { palette: string[] };
133
+
134
+ /** The kits, as cards: what each gives, its settings and goals, example pitches. */
135
+ export function kits(): KitCard[];
136
+ /** game.json against its schema (its text: then each mistake has its line). */
137
+ export function validateGame(game: Game | string): Validation;
138
+ /** Where each value starts in a JSON text, by path ('levels[0].moves'). */
139
+ export function jsonPlaces(text: string): Map<string, number>;
140
+ /** A game ready to play. Throws with every mistake of game.json. */
141
+ export function loadGame(game: Game | string, rules?: Rules): LoadedGame;
142
+ /** Bots play every level on a few seeds; the game's checks pass or fail, each with why and, where found, the fix. */
143
+ export function play(loaded: LoadedGame, options?: { seeds?: number; bots?: BotName[]; seconds?: number; turns?: number; fixes?: boolean }): Report;
144
+ /** Play, apply the fixes found, play again — until every check passes or `rounds` are spent. */
145
+ export function fit(game: Game | string, rules?: Rules, options?: { rounds?: number; seeds?: number; bots?: BotName[]; seconds?: number; turns?: number }): { game: Game; report: Report; rounds: Array<{ fixed: string[] }> };
146
+ /** A first game from one sentence: the kit, the goal and the look read from it; levels made and checked. */
147
+ export function newGame(options: { pitch: string; kit?: Game['kit']; goal?: Level['goal']; style?: Style }): { game: Game; rules: string };
148
+ /** What a pitch says: kit, goal, style, pace, hard (−1, 0, 1), phone. */
149
+ export function readPitch(pitch: string): { kit: Game['kit']; goal?: Level['goal']; style: Style; pace: 'calm' | 'steady' | 'fast'; hard: number; phone: boolean };
150
+ export function titleOf(pitch: string): string;
151
+ export function levelsFor(options: { kit: Game['kit']; goal?: Level['goal']; hard?: number }, rng: () => number): Level[];
152
+ export function platformerLevel(rng: () => number, options?: { length?: number; gaps?: number; walkers?: number; spikes?: number; ledges?: number; coins?: number; height?: number }): string[];
153
+ export function pushLevel(rng: () => number, options?: { w?: number; h?: number; crates?: number; walls?: number; pulls?: number }): string[];
154
+ /** A push level and the moves of the way found to solve it; with minMoves, made again until one takes that many. */
155
+ export function pushLevelWith(rng: () => number, options?: { w?: number; h?: number; crates?: number; walls?: number; pulls?: number; minMoves?: number; tries?: number }): { map: string[]; moves: number | null };
156
+ /** One game played by a bot. */
157
+ export function runBot(make: (seed: any) => GameSim, options?: { bot?: BotName; seed?: any; seconds?: number; turns?: number; budget?: number }): BotRun;
158
+ /** Lights out by algebra: the cells to press (indices), or null. */
159
+ export function solveLights(cells: number[], w: number, h: number): number[] | null;
160
+ /** The shortest moves to a push or slide goal, by search, or null. */
161
+ export function search(sim: GameSim, options?: { budget?: number }): Array<{ act: string; args: any[] }> | null;
162
+
163
+ /** Level makers of the kits, each by a seeded chance (createRng from @ozsarman/clarityjs/sim). */
164
+ export function topdownLevel(rng: () => number, options?: { rooms?: number; chasers?: number; sentries?: number; gems?: number; locked?: boolean; room?: [number, number] }): string[];
165
+ export function shmupLevel(rng: () => number, options?: { length?: number; density?: number; fire?: number; bossHp?: number; pattern?: 'ring' | 'fan' | 'spiral' }): Level;
166
+ export function trackPoints(rng: () => number, options?: { points?: number; radius?: number; wobble?: number }): Array<[number, number]>;
167
+ export function buildTrack(points: Array<[number, number]>, samples?: number): { pts: Array<{ x: number; y: number; s: number; h: number; bend: number }>; total: number; n: number };
168
+ export function bestLap(level: Level, game?: Game): number | null;
169
+ export function exploreLevel(rng: () => number, options?: { size?: number; items?: number; trees?: number; rocks?: number; npcs?: number; fog?: number; says?: string }): Level;
170
+ export function toppleLevel(rng: () => number, options?: { floors?: number; towers?: number; shots?: number; distance?: number }): Level;
171
+ export function stackLevel(rng: () => number, options?: { pieces?: number; platform?: number; height?: number }): Level;
172
+ /** A race's rivals as fast as they can be while the planner still finishes in place; a card game's head start. */
173
+ export function tuneRace(level: Level, hard?: number): Level;
174
+ export function tuneShed(level: Level): Level;
175
+
176
+ /** The changes that make a level easier or harder for its kit, smallest first. */
177
+ export function levelChanges(game: Game | KitName, level: Level, way?: 'easier' | 'harder'): Array<{ change: Partial<Level>; say: string }>;
178
+
179
+ /** Music composed from a seed in a mood, as data (times in beats). */
180
+ export interface Song { mood: string; bpm: number; beats: number; notes: Array<{ t: number; dur: number; note: number; vel: number; voice: 'lead' | 'bass' | 'pad' | 'kick' | 'snare' | 'hat' }>; lead: OscillatorType; bass: OscillatorType }
181
+ export const MOODS: Record<'calm' | 'playful' | 'tense', { scale: number[]; root: number; bpm: number; lead: OscillatorType; bass: OscillatorType; swing: number; density: number; drums: boolean }>;
182
+ export const EFFECTS: Record<string, object[]>;
183
+ export const EVENT_SOUNDS: Record<string, string | null>;
184
+ export function compose(options?: { seed?: any; mood?: 'calm' | 'playful' | 'tense'; bars?: number }): Song;
185
+ export function moodOf(game: Game): 'calm' | 'playful' | 'tense' | null;
186
+ /** Music and effects for a game in the page (the context starts on the first press). */
187
+ export function createSound(game: Game, options?: { context?: AudioContext; seed?: any }): { readonly context: AudioContext | null; readonly muted: boolean; mood: string | null; heard: string[]; start(): void; stop(): void; play(effect: string): void; hear(event: { type: string }): void; mute(on?: boolean): boolean; close(): void };
188
+ export function renderMusic(song: Song, seconds?: number, OfflineContext?: typeof OfflineAudioContext): Promise<AudioBuffer>;
189
+
190
+ // ── @clarity-js/engine/view ──
191
+ /** The game in the page: drawn on a canvas in its look; keys, touch, a pad, or an agent through its contract. Returns unmount. */
192
+ export function mountGame(el: Element, game: Game, rules?: Rules, options?: { level?: number; seed?: any }): () => void;
193
+ export function GameView(props: { game: Game; rules?: Rules; level?: number; seed?: any }): Node;
194
+
195
+ // ── @clarity-js/engine/node ──
196
+ export function readGame(dir: string): Promise<{ dir: string; text: string; validation: Validation; game: Game | null; rules: Rules }>;
197
+ export function createGame(options: { pitch: string; dir?: string; base?: string; kit?: Game['kit']; goal?: Level['goal']; style?: Style; seeds?: number; overwrite?: boolean }): Promise<{ dir: string; game: Game; report: Report; rounds: Array<{ fixed: string[] }>; files: string[] }>;
198
+ export function playGame(dir: string, options?: { seeds?: number; bots?: BotName[]; seconds?: number; turns?: number }): Promise<Report | { ok: false; validation: Validation }>;
199
+ export function formatReport(report: Report | { validation: Validation }): string;
200
+
201
+ // ── @clarity-js/engine/editor ──
202
+ /** A level editor in the page: maps painted cell by cell, other levels as forms; Play, Ask the bots, download. Returns unmount. */
203
+ export function mountEditor(el: Element, game: Game, rules?: Rules, options?: { onChange?: (game: Game) => void; level?: number }): () => void;
204
+ export function LevelEditor(props: { game: Game; rules?: Rules; onChange?: (game: Game) => void; level?: number }): Node;
205
+ /** The map characters of each map kit: [character, label, palette index]. */
206
+ export const TILES: Record<'platformer' | 'top-down' | 'push', Array<[string, string, number]>>;
package/package.json CHANGED
@@ -1,6 +1,26 @@
1
1
  {
2
2
  "name": "@clarity-js/engine",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "A game engine for AI agents, on Clarity.js: one sentence becomes a game (game.json + rules.js) that bots play and measure before it is handed over. Kits: puzzle-grid, platformer, top-down, shmup, racing, card-board, explore-3d, physics-toy.",
5
+ "type": "module",
6
+ "main": "./src/index.js",
7
+ "module": "./src/index.js",
8
+ "types": "./index.d.ts",
9
+ "exports": {
10
+ ".": { "types": "./index.d.ts", "import": "./src/index.js", "default": "./src/index.js" },
11
+ "./view": { "types": "./index.d.ts", "import": "./src/view.js", "default": "./src/view.js" },
12
+ "./node": { "types": "./index.d.ts", "import": "./src/node.js", "default": "./src/node.js" },
13
+ "./editor": { "types": "./index.d.ts", "import": "./src/editor.js", "default": "./src/editor.js" },
14
+ "./schema.json": "./schema.json",
15
+ "./package.json": "./package.json"
16
+ },
17
+ "files": ["src/", "schema.json", "index.d.ts", "README.md"],
18
+ "sideEffects": false,
19
+ "keywords": ["clarity", "game-engine", "ai-agents", "mcp", "procedural", "bots", "platformer", "puzzle", "shmup", "racing", "level-editor"],
20
+ "author": "Özdemir Şarman",
21
+ "license": "MIT",
22
+ "peerDependencies": { "@ozsarman/clarityjs": ">=1.15.0" },
23
+ "homepage": "https://clarity-js.com/docs/engine",
24
+ "repository": { "type": "git", "url": "git+https://github.com/ozdemirsarman/clarity.js.git", "directory": "packages/clarity-engine" },
25
+ "engines": { "node": ">=18.0.0" }
26
+ }