pi-extensions 0.1.9
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/.ralph/import-cc-codex.md +31 -0
- package/.ralph/import-cc-codex.state.json +14 -0
- package/.ralph/mario-not-impl.md +69 -0
- package/.ralph/mario-not-impl.state.json +14 -0
- package/.ralph/mario-not-spec.md +163 -0
- package/.ralph/mario-not-spec.state.json +14 -0
- package/LICENSE +21 -0
- package/README.md +65 -0
- package/RELEASING.md +34 -0
- package/agent-guidance/CHANGELOG.md +4 -0
- package/agent-guidance/README.md +102 -0
- package/agent-guidance/agent-guidance.ts +147 -0
- package/agent-guidance/package.json +22 -0
- package/agent-guidance/setup.sh +75 -0
- package/agent-guidance/templates/CLAUDE.md +5 -0
- package/agent-guidance/templates/CODEX.md +92 -0
- package/agent-guidance/templates/GEMINI.md +5 -0
- package/arcade/CHANGELOG.md +4 -0
- package/arcade/README.md +85 -0
- package/arcade/assets/picman.png +0 -0
- package/arcade/assets/ping.png +0 -0
- package/arcade/assets/spice-invaders.png +0 -0
- package/arcade/assets/tetris.png +0 -0
- package/arcade/mario-not/README.md +30 -0
- package/arcade/mario-not/boss.js +103 -0
- package/arcade/mario-not/camera.js +59 -0
- package/arcade/mario-not/collision.js +91 -0
- package/arcade/mario-not/colors.js +36 -0
- package/arcade/mario-not/constants.js +97 -0
- package/arcade/mario-not/core.js +39 -0
- package/arcade/mario-not/death.js +77 -0
- package/arcade/mario-not/effects.js +84 -0
- package/arcade/mario-not/enemies.js +31 -0
- package/arcade/mario-not/engine.js +171 -0
- package/arcade/mario-not/fireballs.js +98 -0
- package/arcade/mario-not/items.js +24 -0
- package/arcade/mario-not/levels.js +403 -0
- package/arcade/mario-not/logic.js +104 -0
- package/arcade/mario-not/mario-not.ts +297 -0
- package/arcade/mario-not/player.js +244 -0
- package/arcade/mario-not/render.js +257 -0
- package/arcade/mario-not/spec.md +548 -0
- package/arcade/mario-not/state.js +246 -0
- package/arcade/mario-not/tests/e2e.test.js +855 -0
- package/arcade/mario-not/tests/engine.test.js +888 -0
- package/arcade/mario-not/tests/fixtures/story0-frame.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story1-camera.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story1-glyphs.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story10-item.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story11-hazards.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story12-used-block.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story13-pipes.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story14-goal.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story15-hud-narrow.txt +2 -0
- package/arcade/mario-not/tests/fixtures/story16-unknown-tile.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story17-mix.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story18-hud-score.txt +2 -0
- package/arcade/mario-not/tests/fixtures/story19-cue.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story2-enemy.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story20-camera-offset.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story21-hud-zero.txt +2 -0
- package/arcade/mario-not/tests/fixtures/story22-big-viewport.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story23-camera-negative.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story24-camera-width.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story25-camera-positive.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story26-hud-lives.txt +2 -0
- package/arcade/mario-not/tests/fixtures/story27-hud-coins.txt +2 -0
- package/arcade/mario-not/tests/fixtures/story28-item-viewport.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story29-enemy-viewport.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story3-hud.txt +2 -0
- package/arcade/mario-not/tests/fixtures/story30-hud-score.txt +2 -0
- package/arcade/mario-not/tests/fixtures/story31-particles-viewport.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story32-paused-frame.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story4-big.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story5-resume-hud.txt +2 -0
- package/arcade/mario-not/tests/fixtures/story6-particles.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story6-paused.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story7-powerup.txt +4 -0
- package/arcade/mario-not/tests/fixtures/story8-hud-time.txt +2 -0
- package/arcade/mario-not/tests/fixtures/story9-hud-level.txt +2 -0
- package/arcade/mario-not/tiles.js +79 -0
- package/arcade/mario-not/tsconfig.json +14 -0
- package/arcade/mario-not/types.js +225 -0
- package/arcade/package.json +26 -0
- package/arcade/picman.ts +328 -0
- package/arcade/ping.ts +594 -0
- package/arcade/spice-invaders.ts +1104 -0
- package/arcade/tetris.ts +662 -0
- package/code-actions/CHANGELOG.md +4 -0
- package/code-actions/README.md +65 -0
- package/code-actions/actions.ts +107 -0
- package/code-actions/index.ts +148 -0
- package/code-actions/package.json +22 -0
- package/code-actions/search.ts +79 -0
- package/code-actions/snippets.ts +179 -0
- package/code-actions/ui.ts +120 -0
- package/files-widget/CHANGELOG.md +90 -0
- package/files-widget/DESIGN.md +452 -0
- package/files-widget/README.md +122 -0
- package/files-widget/TODO.md +141 -0
- package/files-widget/browser.ts +922 -0
- package/files-widget/comment.ts +5 -0
- package/files-widget/constants.ts +18 -0
- package/files-widget/demo.svg +1 -0
- package/files-widget/file-tree.ts +224 -0
- package/files-widget/file-viewer.ts +93 -0
- package/files-widget/git.ts +107 -0
- package/files-widget/index.ts +140 -0
- package/files-widget/input-utils.ts +3 -0
- package/files-widget/package.json +22 -0
- package/files-widget/types.ts +28 -0
- package/files-widget/utils.ts +26 -0
- package/files-widget/viewer.ts +424 -0
- package/import-cc-codex/research/import-chats-from-other-agents.md +135 -0
- package/import-cc-codex/spec.md +79 -0
- package/package.json +29 -0
- package/ralph-wiggum/CHANGELOG.md +7 -0
- package/ralph-wiggum/README.md +96 -0
- package/ralph-wiggum/SKILL.md +73 -0
- package/ralph-wiggum/index.ts +792 -0
- package/ralph-wiggum/package.json +25 -0
- package/raw-paste/CHANGELOG.md +7 -0
- package/raw-paste/README.md +52 -0
- package/raw-paste/index.ts +112 -0
- package/raw-paste/package.json +22 -0
- package/tab-status/CHANGELOG.md +4 -0
- package/tab-status/README.md +61 -0
- package/tab-status/assets/tab-status.png +0 -0
- package/tab-status/package.json +22 -0
- package/tab-status/tab-status.ts +179 -0
- package/usage-extension/CHANGELOG.md +17 -0
- package/usage-extension/README.md +120 -0
- package/usage-extension/index.ts +628 -0
- package/usage-extension/package.json +22 -0
- package/usage-extension/screenshot.png +0 -0
package/arcade/picman.ts
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Picman - Pi eats tokens, avoids bugs! Play with /picman
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { ExtensionAPI, ExtensionCommandContext } from "@mariozechner/pi-coding-agent";
|
|
6
|
+
import { matchesKey, truncateToWidth, visibleWidth } from "@mariozechner/pi-tui";
|
|
7
|
+
|
|
8
|
+
const TICK_MS = 100;
|
|
9
|
+
const POWER_DURATION = 50;
|
|
10
|
+
const BUG_RESPAWN_TICKS = 30;
|
|
11
|
+
const INITIAL_LIVES = 3;
|
|
12
|
+
const BUG_MOVE_INTERVAL = 2;
|
|
13
|
+
const SAVE_TYPE = "picman-save";
|
|
14
|
+
|
|
15
|
+
type Direction = "up" | "down" | "left" | "right";
|
|
16
|
+
|
|
17
|
+
// The codebase - navigate and collect tokens!
|
|
18
|
+
const MAZE_TEMPLATE = [
|
|
19
|
+
"#####################",
|
|
20
|
+
"#.........#.........#",
|
|
21
|
+
"#O###.###.#.###.###O#",
|
|
22
|
+
"#.###.###.#.###.###.#",
|
|
23
|
+
"#...................#",
|
|
24
|
+
"#.###.#.#####.#.###.#",
|
|
25
|
+
"#.....#...#...#.....#",
|
|
26
|
+
"#####.### # ###.#####",
|
|
27
|
+
" #.# B B #.# ",
|
|
28
|
+
"#####.# BBB B#.#####",
|
|
29
|
+
" . BBB B . ",
|
|
30
|
+
"#####.# BBBBB #.#####",
|
|
31
|
+
" #.# #.# ",
|
|
32
|
+
"#####.# ##### #.#####",
|
|
33
|
+
"#.........#.........#",
|
|
34
|
+
"#.###.###.#.###.###.#",
|
|
35
|
+
"#O..#.....P.....#..O#",
|
|
36
|
+
"###.#.#.#####.#.#.###",
|
|
37
|
+
"#.....#...#...#.....#",
|
|
38
|
+
"#.#######.#.#######.#",
|
|
39
|
+
"#####################",
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
const DIRECTIONS: Record<Direction, { x: number; y: number }> = {
|
|
43
|
+
up: { x: 0, y: -1 }, down: { x: 0, y: 1 }, left: { x: -1, y: 0 }, right: { x: 1, y: 0 },
|
|
44
|
+
};
|
|
45
|
+
const OPPOSITE: Record<Direction, Direction> = { up: "down", down: "up", left: "right", right: "left" };
|
|
46
|
+
|
|
47
|
+
// ANSI colors
|
|
48
|
+
const [RESET, CYAN, BLUE, WHITE, DIM, RED] = ["\x1b[0m", "\x1b[1;96m", "\x1b[1;34m", "\x1b[1;97m", "\x1b[2m", "\x1b[1;91m"];
|
|
49
|
+
const GREEN = "\x1b[1;32m";
|
|
50
|
+
const BUG_COLORS = ["\x1b[1;91m", "\x1b[1;95m", "\x1b[1;93m", "\x1b[1;31m"]; // Red, Magenta, Yellow, Dark Red
|
|
51
|
+
const BUG_NAMES = ["Bug", "Error", "Crash", "Glitch"];
|
|
52
|
+
|
|
53
|
+
interface Bug {
|
|
54
|
+
x: number; y: number; homeX: number; homeY: number;
|
|
55
|
+
direction: Direction; color: string; name: string;
|
|
56
|
+
squashed: boolean; respawnTimer: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface GameState {
|
|
60
|
+
pi: { x: number; y: number };
|
|
61
|
+
piDir: Direction;
|
|
62
|
+
nextDir: Direction | null;
|
|
63
|
+
bugs: Bug[];
|
|
64
|
+
maze: string[][];
|
|
65
|
+
tokens: number; highScore: number; lives: number; level: number;
|
|
66
|
+
tokensRemaining: number; caffeine: number; tickCount: number;
|
|
67
|
+
gameOver: boolean; paused: boolean; mouthOpen: boolean;
|
|
68
|
+
deathAnimation: number; levelCompleteTimer: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const findInMaze = (char: string): { x: number; y: number }[] => {
|
|
72
|
+
const results: { x: number; y: number }[] = [];
|
|
73
|
+
MAZE_TEMPLATE.forEach((row, y) => {
|
|
74
|
+
for (let x = 0; x < row.length; x++) if (row[x] === char) results.push({ x, y });
|
|
75
|
+
});
|
|
76
|
+
return results;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const createMaze = (): string[][] => {
|
|
80
|
+
const maze = MAZE_TEMPLATE.map(row => row.split(""));
|
|
81
|
+
for (let y = 0; y < maze.length; y++)
|
|
82
|
+
for (let x = 0; x < maze[y].length; x++)
|
|
83
|
+
if (maze[y][x] === "P" || maze[y][x] === "B") maze[y][x] = " ";
|
|
84
|
+
return maze;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const countTokens = (maze: string[][]): number =>
|
|
88
|
+
maze.flat().filter(c => c === "." || c === "O").length;
|
|
89
|
+
|
|
90
|
+
const createBugs = (): Bug[] =>
|
|
91
|
+
findInMaze("B").slice(0, 4).map((pos, i) => ({
|
|
92
|
+
x: pos.x, y: pos.y, homeX: pos.x, homeY: pos.y,
|
|
93
|
+
direction: "up" as Direction, color: BUG_COLORS[i], name: BUG_NAMES[i],
|
|
94
|
+
squashed: false, respawnTimer: 0,
|
|
95
|
+
}));
|
|
96
|
+
|
|
97
|
+
const createState = (highScore = 0): GameState => {
|
|
98
|
+
const maze = createMaze();
|
|
99
|
+
const piStart = findInMaze("P")[0] || { x: 10, y: 16 };
|
|
100
|
+
return {
|
|
101
|
+
pi: piStart, piDir: "right", nextDir: null,
|
|
102
|
+
bugs: createBugs(), maze,
|
|
103
|
+
tokens: 0, highScore, lives: INITIAL_LIVES, level: 1,
|
|
104
|
+
tokensRemaining: countTokens(maze), caffeine: 0, tickCount: 0,
|
|
105
|
+
gameOver: false, paused: false, mouthOpen: true,
|
|
106
|
+
deathAnimation: 0, levelCompleteTimer: 0,
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const resetLevel = (s: GameState): void => {
|
|
111
|
+
s.maze = createMaze();
|
|
112
|
+
s.pi = findInMaze("P")[0] || { x: 10, y: 16 };
|
|
113
|
+
s.piDir = "right"; s.nextDir = null;
|
|
114
|
+
s.bugs = createBugs();
|
|
115
|
+
s.tokensRemaining = countTokens(s.maze);
|
|
116
|
+
s.caffeine = s.deathAnimation = s.levelCompleteTimer = 0;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const resetPositions = (s: GameState): void => {
|
|
120
|
+
s.pi = findInMaze("P")[0] || { x: 10, y: 16 };
|
|
121
|
+
s.piDir = "right"; s.nextDir = null;
|
|
122
|
+
s.caffeine = s.deathAnimation = 0;
|
|
123
|
+
s.bugs.forEach(b => { b.x = b.homeX; b.y = b.homeY; b.squashed = false; b.respawnTimer = 0; });
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const isWalkable = (maze: string[][], x: number, y: number): boolean =>
|
|
127
|
+
y >= 0 && y < maze.length && (x < 0 || x >= maze[0].length || maze[y][x] !== "#");
|
|
128
|
+
|
|
129
|
+
const wrap = (x: number, w: number): number => x < 0 ? w - 1 : x >= w ? 0 : x;
|
|
130
|
+
|
|
131
|
+
const dist = (a: { x: number; y: number }, b: { x: number; y: number }): number =>
|
|
132
|
+
Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
|
|
133
|
+
|
|
134
|
+
const getValidDirs = (maze: string[][], x: number, y: number, exclude?: Direction): Direction[] =>
|
|
135
|
+
(Object.keys(DIRECTIONS) as Direction[]).filter(dir => {
|
|
136
|
+
if (dir === exclude) return false;
|
|
137
|
+
const d = DIRECTIONS[dir];
|
|
138
|
+
return isWalkable(maze, x + d.x, y + d.y);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const moveBug = (b: Bug, s: GameState): void => {
|
|
142
|
+
if (b.squashed) {
|
|
143
|
+
if (--b.respawnTimer <= 0) { b.squashed = false; b.x = b.homeX; b.y = b.homeY; }
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const { maze, pi, caffeine } = s;
|
|
148
|
+
const validDirs = getValidDirs(maze, b.x, b.y, OPPOSITE[b.direction]);
|
|
149
|
+
|
|
150
|
+
if (validDirs.length === 0) {
|
|
151
|
+
const rev = OPPOSITE[b.direction], d = DIRECTIONS[rev];
|
|
152
|
+
if (isWalkable(maze, b.x + d.x, b.y + d.y)) {
|
|
153
|
+
b.direction = rev; b.x = wrap(b.x + d.x, maze[0].length); b.y += d.y;
|
|
154
|
+
}
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Target: flee if Pi is caffeinated, else chase with personality
|
|
159
|
+
let target = pi;
|
|
160
|
+
if (caffeine <= 0) {
|
|
161
|
+
if (b.name === "Error") {
|
|
162
|
+
const d = DIRECTIONS[s.piDir];
|
|
163
|
+
target = { x: pi.x + d.x * 4, y: pi.y + d.y * 4 };
|
|
164
|
+
} else if (b.name === "Glitch" && dist(b, pi) < 8) {
|
|
165
|
+
target = { x: 0, y: maze.length - 1 };
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Pick best direction
|
|
170
|
+
const bestDir = validDirs.reduce((best, dir) => {
|
|
171
|
+
const d = DIRECTIONS[dir];
|
|
172
|
+
const nx = wrap(b.x + d.x, maze[0].length), ny = b.y + d.y;
|
|
173
|
+
const score = dist({ x: nx, y: ny }, target);
|
|
174
|
+
const bestScore = dist({ x: wrap(b.x + DIRECTIONS[best].x, maze[0].length), y: b.y + DIRECTIONS[best].y }, target);
|
|
175
|
+
return caffeine > 0 ? (score > bestScore ? dir : best) : (score < bestScore ? dir : best);
|
|
176
|
+
}, validDirs[0]);
|
|
177
|
+
|
|
178
|
+
b.direction = bestDir;
|
|
179
|
+
const d = DIRECTIONS[bestDir];
|
|
180
|
+
b.x = wrap(b.x + d.x, maze[0].length); b.y += d.y;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const movePi = (s: GameState): void => {
|
|
184
|
+
const { maze, pi } = s;
|
|
185
|
+
|
|
186
|
+
// Try queued direction
|
|
187
|
+
if (s.nextDir) {
|
|
188
|
+
const d = DIRECTIONS[s.nextDir];
|
|
189
|
+
if (isWalkable(maze, pi.x + d.x, pi.y + d.y)) { s.piDir = s.nextDir; s.nextDir = null; }
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const d = DIRECTIONS[s.piDir];
|
|
193
|
+
if (isWalkable(maze, pi.x + d.x, pi.y + d.y)) {
|
|
194
|
+
pi.x = wrap(pi.x + d.x, maze[0].length); pi.y += d.y;
|
|
195
|
+
const cell = maze[pi.y][pi.x];
|
|
196
|
+
if (cell === ".") { maze[pi.y][pi.x] = " "; s.tokens += 10; s.tokensRemaining--; }
|
|
197
|
+
else if (cell === "O") { maze[pi.y][pi.x] = " "; s.tokens += 50; s.tokensRemaining--; s.caffeine = POWER_DURATION; }
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (s.tickCount % 3 === 0) s.mouthOpen = !s.mouthOpen;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const checkCollisions = (s: GameState): void => {
|
|
204
|
+
for (const b of s.bugs) {
|
|
205
|
+
if (b.squashed || b.x !== s.pi.x || b.y !== s.pi.y) continue;
|
|
206
|
+
if (s.caffeine > 0) { b.squashed = true; b.respawnTimer = BUG_RESPAWN_TICKS; s.tokens += 200; }
|
|
207
|
+
else if (--s.lives <= 0) s.gameOver = true;
|
|
208
|
+
else s.deathAnimation = 10;
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
class PicmanComponent {
|
|
213
|
+
private state: GameState;
|
|
214
|
+
private interval: ReturnType<typeof setInterval> | null = null;
|
|
215
|
+
private onClose: () => void;
|
|
216
|
+
private onSave: (state: GameState) => void;
|
|
217
|
+
private tui: { requestRender: () => void };
|
|
218
|
+
private cache = { lines: [] as string[], width: 0, version: -1 };
|
|
219
|
+
private version = 0;
|
|
220
|
+
|
|
221
|
+
constructor(tui: { requestRender: () => void }, onClose: () => void, onSave: (s: GameState) => void, saved?: GameState) {
|
|
222
|
+
this.tui = tui; this.onClose = onClose; this.onSave = onSave;
|
|
223
|
+
this.state = saved ? { ...saved, paused: true } : createState();
|
|
224
|
+
this.interval = setInterval(() => this.tick(), TICK_MS);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private tick(): void {
|
|
228
|
+
const s = this.state;
|
|
229
|
+
if (s.paused || s.gameOver) return;
|
|
230
|
+
s.tickCount++;
|
|
231
|
+
|
|
232
|
+
if (s.deathAnimation > 0) { if (--s.deathAnimation === 0) resetPositions(s); }
|
|
233
|
+
else if (s.levelCompleteTimer > 0) { if (--s.levelCompleteTimer === 0) { s.level++; resetLevel(s); } }
|
|
234
|
+
else if (s.tokensRemaining === 0) s.levelCompleteTimer = 20;
|
|
235
|
+
else {
|
|
236
|
+
if (s.caffeine > 0) s.caffeine--;
|
|
237
|
+
movePi(s);
|
|
238
|
+
const bugInterval = Math.max(1, BUG_MOVE_INTERVAL - Math.floor(s.level / 3));
|
|
239
|
+
if (s.tickCount % bugInterval === 0) s.bugs.forEach(b => moveBug(b, s));
|
|
240
|
+
checkCollisions(s);
|
|
241
|
+
if (s.tokens > s.highScore) s.highScore = s.tokens;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
this.version++;
|
|
245
|
+
this.tui.requestRender();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
handleInput(key: string): boolean {
|
|
249
|
+
const s = this.state;
|
|
250
|
+
if (matchesKey(key, "escape") || key === "q" || key === "Q") { this.onSave(s); this.onClose(); return true; }
|
|
251
|
+
if (key === "n" || key === "N") { Object.assign(this.state, createState(Math.max(s.highScore, s.tokens))); this.version++; this.tui.requestRender(); return true; }
|
|
252
|
+
if (key === " " || key === "p" || key === "P") { s.paused = !s.paused; this.version++; this.tui.requestRender(); return true; }
|
|
253
|
+
if (s.gameOver || s.paused) return true;
|
|
254
|
+
|
|
255
|
+
const dirMap: Record<string, Direction> = { k: "up", w: "up", j: "down", s: "down", h: "left", a: "left", l: "right", d: "right" };
|
|
256
|
+
if (matchesKey(key, "up")) s.nextDir = "up";
|
|
257
|
+
else if (matchesKey(key, "down")) s.nextDir = "down";
|
|
258
|
+
else if (matchesKey(key, "left")) s.nextDir = "left";
|
|
259
|
+
else if (matchesKey(key, "right")) s.nextDir = "right";
|
|
260
|
+
else if (dirMap[key]) s.nextDir = dirMap[key];
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
render(width: number, _height: number): string[] {
|
|
265
|
+
const pad = (line: string) => truncateToWidth(line, width) + " ".repeat(Math.max(0, width - visibleWidth(truncateToWidth(line, width))));
|
|
266
|
+
const minWidth = MAZE_TEMPLATE[0].length * 2 + 4;
|
|
267
|
+
|
|
268
|
+
if (width < minWidth) {
|
|
269
|
+
return ["", pad(`${CYAN}PICMAN${RESET}`), "", pad("Terminal too narrow"), pad(`Need ${minWidth} cols`), "", pad("[Q] Quit")];
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (this.cache.version === this.version && this.cache.width === width) return this.cache.lines;
|
|
273
|
+
|
|
274
|
+
const s = this.state, lines: string[] = [""];
|
|
275
|
+
const lives = Array(Math.max(0, s.lives)).fill(`${CYAN}Pi${RESET}`).join(" ");
|
|
276
|
+
lines.push(pad(`${CYAN}PICMAN${RESET} Tokens: ${WHITE}${s.tokens}${RESET} Hi: ${s.highScore} Lv: ${s.level} ${lives}`));
|
|
277
|
+
lines.push("");
|
|
278
|
+
|
|
279
|
+
if (s.gameOver) {
|
|
280
|
+
lines.push(pad(`${RED}SEGFAULT${RESET}`), pad(`Final Tokens: ${s.tokens}`), "", pad("[N] New Game [Q] Quit"), "");
|
|
281
|
+
} else {
|
|
282
|
+
if (s.paused) lines.push(pad(`${DIM}PAUSED${RESET}`), pad("[SPACE] Resume [N] New [Q] Quit"), "");
|
|
283
|
+
if (s.levelCompleteTimer > 0) lines.push(pad(`${GREEN}LEVEL ${s.level} SHIPPED!${RESET}`), "");
|
|
284
|
+
if (s.deathAnimation > 0) lines.push(pad(`${RED}BUG FOUND!${RESET}`), "");
|
|
285
|
+
|
|
286
|
+
for (let y = 0; y < s.maze.length; y++) {
|
|
287
|
+
let row = "";
|
|
288
|
+
for (let x = 0; x < s.maze[y].length; x++) {
|
|
289
|
+
const cell = s.maze[y][x];
|
|
290
|
+
if (s.pi.x === x && s.pi.y === y && s.deathAnimation === 0) {
|
|
291
|
+
const glyph = s.mouthOpen ? ({ right: "Pi", left: "iP", up: "Pi", down: "Pi" })[s.piDir] : "PI";
|
|
292
|
+
row += `${CYAN}${glyph}${RESET}`;
|
|
293
|
+
} else {
|
|
294
|
+
const bug = s.bugs.find(b => b.x === x && b.y === y && !b.squashed);
|
|
295
|
+
if (bug) {
|
|
296
|
+
const scared = s.caffeine > 0, blink = scared && s.caffeine < 15 && s.tickCount % 4 < 2;
|
|
297
|
+
row += `${blink ? WHITE : scared ? GREEN : bug.color}${scared ? "><" : "<>"}${RESET}`;
|
|
298
|
+
} else {
|
|
299
|
+
row += cell === "#" ? `${BLUE}██${RESET}` : cell === "." ? ` ${DIM};${RESET}` : cell === "O" ? `${GREEN}()${RESET}` : " ";
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
lines.push(pad(row));
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
lines.push("", pad(s.caffeine > 0 ? `${GREEN}CAFFEINATED!${RESET} [Arrows/HJKL] Move [SPACE] Pause [Q] Quit` : "[Arrows/HJKL] Move [SPACE] Pause [N] New [Q] Quit"), "");
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
this.cache = { lines, width, version: this.version };
|
|
310
|
+
return lines;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
dispose(): void { if (this.interval) clearInterval(this.interval); }
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export default function (api: ExtensionAPI) {
|
|
317
|
+
api.registerCommand("picman", {
|
|
318
|
+
description: "Play Picman! Collect tokens, squash bugs, drink coffee!",
|
|
319
|
+
handler: async (_args, ctx: ExtensionCommandContext) => {
|
|
320
|
+
if (!ctx.hasUI) { ctx.ui.notify("Picman requires interactive mode", "error"); return; }
|
|
321
|
+
|
|
322
|
+
const entries = ctx.sessionManager.getEntries();
|
|
323
|
+
const saved = entries.reverse().find(e => e.type === "custom" && e.customType === SAVE_TYPE)?.data as GameState | undefined;
|
|
324
|
+
|
|
325
|
+
await ctx.ui.custom((tui, _theme, _kb, done) => new PicmanComponent(tui, () => done(undefined), s => api.appendEntry(SAVE_TYPE, s), saved));
|
|
326
|
+
},
|
|
327
|
+
});
|
|
328
|
+
}
|