@mariozechner/pi-coding-agent 0.45.5 → 0.45.7

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.
@@ -0,0 +1,132 @@
1
+ /**
2
+ * DOOM Component for overlay mode
3
+ *
4
+ * Renders DOOM frames using half-block characters (▀) with 24-bit color.
5
+ * Height is calculated from width to maintain DOOM's aspect ratio.
6
+ */
7
+
8
+ import type { Component } from "@mariozechner/pi-tui";
9
+ import { isKeyRelease, type TUI } from "@mariozechner/pi-tui";
10
+ import type { DoomEngine } from "./doom-engine.js";
11
+ import { DoomKeys, mapKeyToDoom } from "./doom-keys.js";
12
+
13
+ function renderHalfBlock(
14
+ rgba: Uint8Array,
15
+ width: number,
16
+ height: number,
17
+ targetCols: number,
18
+ targetRows: number,
19
+ ): string[] {
20
+ const lines: string[] = [];
21
+ const scaleX = width / targetCols;
22
+ const scaleY = height / (targetRows * 2);
23
+
24
+ for (let row = 0; row < targetRows; row++) {
25
+ let line = "";
26
+ const srcY1 = Math.floor(row * 2 * scaleY);
27
+ const srcY2 = Math.floor((row * 2 + 1) * scaleY);
28
+
29
+ for (let col = 0; col < targetCols; col++) {
30
+ const srcX = Math.floor(col * scaleX);
31
+ const idx1 = (srcY1 * width + srcX) * 4;
32
+ const idx2 = (srcY2 * width + srcX) * 4;
33
+ const r1 = rgba[idx1] ?? 0,
34
+ g1 = rgba[idx1 + 1] ?? 0,
35
+ b1 = rgba[idx1 + 2] ?? 0;
36
+ const r2 = rgba[idx2] ?? 0,
37
+ g2 = rgba[idx2 + 1] ?? 0,
38
+ b2 = rgba[idx2 + 2] ?? 0;
39
+ line += `\x1b[38;2;${r1};${g1};${b1}m\x1b[48;2;${r2};${g2};${b2}m▀`;
40
+ }
41
+ line += "\x1b[0m";
42
+ lines.push(line);
43
+ }
44
+ return lines;
45
+ }
46
+
47
+ export class DoomOverlayComponent implements Component {
48
+ private engine: DoomEngine;
49
+ private tui: TUI;
50
+ private interval: ReturnType<typeof setInterval> | null = null;
51
+ private onExit: () => void;
52
+
53
+ // Opt-in to key release events for smooth movement
54
+ wantsKeyRelease = true;
55
+
56
+ constructor(tui: TUI, engine: DoomEngine, onExit: () => void, resume = false) {
57
+ this.tui = tui;
58
+ this.engine = engine;
59
+ this.onExit = onExit;
60
+
61
+ // Unpause if resuming
62
+ if (resume) {
63
+ this.engine.pushKey(true, DoomKeys.KEY_PAUSE);
64
+ this.engine.pushKey(false, DoomKeys.KEY_PAUSE);
65
+ }
66
+
67
+ this.startGameLoop();
68
+ }
69
+
70
+ private startGameLoop(): void {
71
+ this.interval = setInterval(() => {
72
+ try {
73
+ this.engine.tick();
74
+ this.tui.requestRender();
75
+ } catch {
76
+ // WASM error (e.g., exit via DOOM menu) - treat as quit
77
+ this.dispose();
78
+ this.onExit();
79
+ }
80
+ }, 1000 / 35);
81
+ }
82
+
83
+ handleInput(data: string): void {
84
+ // Q to pause and exit (but not on release)
85
+ if (!isKeyRelease(data) && (data === "q" || data === "Q")) {
86
+ // Send DOOM's pause key before exiting
87
+ this.engine.pushKey(true, DoomKeys.KEY_PAUSE);
88
+ this.engine.pushKey(false, DoomKeys.KEY_PAUSE);
89
+ this.dispose();
90
+ this.onExit();
91
+ return;
92
+ }
93
+
94
+ const doomKeys = mapKeyToDoom(data);
95
+ if (doomKeys.length === 0) return;
96
+
97
+ const released = isKeyRelease(data);
98
+
99
+ for (const key of doomKeys) {
100
+ this.engine.pushKey(!released, key);
101
+ }
102
+ }
103
+
104
+ render(width: number): string[] {
105
+ // DOOM renders at 640x400 (1.6:1 ratio)
106
+ // With half-block characters, each terminal row = 2 pixels
107
+ // So effective ratio is 640:200 = 3.2:1 (width:height in terminal cells)
108
+ // Add 1 row for footer
109
+ const ASPECT_RATIO = 3.2;
110
+ const MIN_HEIGHT = 10;
111
+ const height = Math.max(MIN_HEIGHT, Math.floor(width / ASPECT_RATIO));
112
+
113
+ const rgba = this.engine.getFrameRGBA();
114
+ const lines = renderHalfBlock(rgba, this.engine.width, this.engine.height, width, height);
115
+
116
+ // Footer
117
+ const footer = " DOOM | Q=Pause | WASD=Move | Shift+WASD=Run | Space=Use | F=Fire | 1-7=Weapons";
118
+ const truncatedFooter = footer.length > width ? footer.slice(0, width) : footer;
119
+ lines.push(`\x1b[2m${truncatedFooter}\x1b[0m`);
120
+
121
+ return lines;
122
+ }
123
+
124
+ invalidate(): void {}
125
+
126
+ dispose(): void {
127
+ if (this.interval) {
128
+ clearInterval(this.interval);
129
+ this.interval = null;
130
+ }
131
+ }
132
+ }
@@ -0,0 +1,173 @@
1
+ /**
2
+ * DOOM Engine - WebAssembly wrapper for doomgeneric
3
+ */
4
+
5
+ import { existsSync, readFileSync } from "node:fs";
6
+ import { createRequire } from "node:module";
7
+ import { dirname, join } from "node:path";
8
+ import { fileURLToPath } from "node:url";
9
+
10
+ export interface DoomModule {
11
+ _doomgeneric_Create: (argc: number, argv: number) => void;
12
+ _doomgeneric_Tick: () => void;
13
+ _DG_GetFrameBuffer: () => number;
14
+ _DG_GetScreenWidth: () => number;
15
+ _DG_GetScreenHeight: () => number;
16
+ _DG_PushKeyEvent: (pressed: number, key: number) => void;
17
+ _malloc: (size: number) => number;
18
+ _free: (ptr: number) => void;
19
+ HEAPU8: Uint8Array;
20
+ HEAPU32: Uint32Array;
21
+ FS_createDataFile: (parent: string, name: string, data: number[], canRead: boolean, canWrite: boolean) => void;
22
+ FS_createPath: (parent: string, path: string, canRead: boolean, canWrite: boolean) => string;
23
+ setValue: (ptr: number, value: number, type: string) => void;
24
+ getValue: (ptr: number, type: string) => number;
25
+ }
26
+
27
+ export class DoomEngine {
28
+ private module: DoomModule | null = null;
29
+ private frameBufferPtr: number = 0;
30
+ private initialized = false;
31
+ private wadPath: string;
32
+ private _width = 640;
33
+ private _height = 400;
34
+
35
+ constructor(wadPath: string) {
36
+ this.wadPath = wadPath;
37
+ }
38
+
39
+ get width(): number {
40
+ return this._width;
41
+ }
42
+
43
+ get height(): number {
44
+ return this._height;
45
+ }
46
+
47
+ async init(): Promise<void> {
48
+ // Locate WASM build
49
+ const __dirname = dirname(fileURLToPath(import.meta.url));
50
+ const buildDir = join(__dirname, "doom", "build");
51
+ const doomJsPath = join(buildDir, "doom.js");
52
+
53
+ if (!existsSync(doomJsPath)) {
54
+ throw new Error(`WASM not found at ${doomJsPath}. Run ./doom/build.sh first`);
55
+ }
56
+
57
+ // Read WAD file
58
+ const wadData = readFileSync(this.wadPath);
59
+ const wadArray = Array.from(new Uint8Array(wadData));
60
+
61
+ // Load WASM module - eval to bypass jiti completely
62
+ const doomJsCode = readFileSync(doomJsPath, "utf-8");
63
+ const moduleExports: { exports: unknown } = { exports: {} };
64
+ const nativeRequire = createRequire(doomJsPath);
65
+ const moduleFunc = new Function("module", "exports", "__dirname", "__filename", "require", doomJsCode);
66
+ moduleFunc(moduleExports, moduleExports.exports, buildDir, doomJsPath, nativeRequire);
67
+ const createDoomModule = moduleExports.exports as (config: unknown) => Promise<DoomModule>;
68
+
69
+ const moduleConfig = {
70
+ locateFile: (path: string) => {
71
+ if (path.endsWith(".wasm")) {
72
+ return join(buildDir, path);
73
+ }
74
+ return path;
75
+ },
76
+ print: () => {},
77
+ printErr: () => {},
78
+ preRun: [
79
+ (module: DoomModule) => {
80
+ // Create /doom directory and add WAD
81
+ module.FS_createPath("/", "doom", true, true);
82
+ module.FS_createDataFile("/doom", "doom1.wad", wadArray, true, false);
83
+ },
84
+ ],
85
+ };
86
+
87
+ this.module = await createDoomModule(moduleConfig);
88
+ if (!this.module) {
89
+ throw new Error("Failed to initialize DOOM module");
90
+ }
91
+
92
+ // Initialize DOOM
93
+ this.initDoom();
94
+
95
+ // Get framebuffer info
96
+ this.frameBufferPtr = this.module._DG_GetFrameBuffer();
97
+ this._width = this.module._DG_GetScreenWidth();
98
+ this._height = this.module._DG_GetScreenHeight();
99
+ this.initialized = true;
100
+ }
101
+
102
+ private initDoom(): void {
103
+ if (!this.module) return;
104
+
105
+ const args = ["doom", "-iwad", "/doom/doom1.wad"];
106
+ const argPtrs: number[] = [];
107
+
108
+ for (const arg of args) {
109
+ const ptr = this.module._malloc(arg.length + 1);
110
+ for (let i = 0; i < arg.length; i++) {
111
+ this.module.setValue(ptr + i, arg.charCodeAt(i), "i8");
112
+ }
113
+ this.module.setValue(ptr + arg.length, 0, "i8");
114
+ argPtrs.push(ptr);
115
+ }
116
+
117
+ const argvPtr = this.module._malloc(argPtrs.length * 4);
118
+ for (let i = 0; i < argPtrs.length; i++) {
119
+ this.module.setValue(argvPtr + i * 4, argPtrs[i]!, "i32");
120
+ }
121
+
122
+ this.module._doomgeneric_Create(args.length, argvPtr);
123
+
124
+ for (const ptr of argPtrs) {
125
+ this.module._free(ptr);
126
+ }
127
+ this.module._free(argvPtr);
128
+ }
129
+
130
+ /**
131
+ * Run one game tick
132
+ */
133
+ tick(): void {
134
+ if (!this.module || !this.initialized) return;
135
+ this.module._doomgeneric_Tick();
136
+ }
137
+
138
+ /**
139
+ * Get current frame as RGBA pixel data
140
+ * DOOM outputs ARGB, we convert to RGBA
141
+ */
142
+ getFrameRGBA(): Uint8Array {
143
+ if (!this.module || !this.initialized) {
144
+ return new Uint8Array(this._width * this._height * 4);
145
+ }
146
+
147
+ const pixels = this._width * this._height;
148
+ const buffer = new Uint8Array(pixels * 4);
149
+
150
+ for (let i = 0; i < pixels; i++) {
151
+ const argb = this.module.getValue(this.frameBufferPtr + i * 4, "i32");
152
+ const offset = i * 4;
153
+ buffer[offset + 0] = (argb >> 16) & 0xff; // R
154
+ buffer[offset + 1] = (argb >> 8) & 0xff; // G
155
+ buffer[offset + 2] = argb & 0xff; // B
156
+ buffer[offset + 3] = 255; // A
157
+ }
158
+
159
+ return buffer;
160
+ }
161
+
162
+ /**
163
+ * Push a key event
164
+ */
165
+ pushKey(pressed: boolean, key: number): void {
166
+ if (!this.module || !this.initialized) return;
167
+ this.module._DG_PushKeyEvent(pressed ? 1 : 0, key);
168
+ }
169
+
170
+ isInitialized(): boolean {
171
+ return this.initialized;
172
+ }
173
+ }
@@ -0,0 +1,104 @@
1
+ /**
2
+ * DOOM key codes (from doomkeys.h)
3
+ */
4
+ export const DoomKeys = {
5
+ KEY_RIGHTARROW: 0xae,
6
+ KEY_LEFTARROW: 0xac,
7
+ KEY_UPARROW: 0xad,
8
+ KEY_DOWNARROW: 0xaf,
9
+ KEY_STRAFE_L: 0xa0,
10
+ KEY_STRAFE_R: 0xa1,
11
+ KEY_USE: 0xa2,
12
+ KEY_FIRE: 0xa3,
13
+ KEY_ESCAPE: 27,
14
+ KEY_ENTER: 13,
15
+ KEY_TAB: 9,
16
+ KEY_F1: 0x80 + 0x3b,
17
+ KEY_F2: 0x80 + 0x3c,
18
+ KEY_F3: 0x80 + 0x3d,
19
+ KEY_F4: 0x80 + 0x3e,
20
+ KEY_F5: 0x80 + 0x3f,
21
+ KEY_F6: 0x80 + 0x40,
22
+ KEY_F7: 0x80 + 0x41,
23
+ KEY_F8: 0x80 + 0x42,
24
+ KEY_F9: 0x80 + 0x43,
25
+ KEY_F10: 0x80 + 0x44,
26
+ KEY_F11: 0x80 + 0x57,
27
+ KEY_F12: 0x80 + 0x58,
28
+ KEY_BACKSPACE: 127,
29
+ KEY_PAUSE: 0xff,
30
+ KEY_EQUALS: 0x3d,
31
+ KEY_MINUS: 0x2d,
32
+ KEY_RSHIFT: 0x80 + 0x36,
33
+ KEY_RCTRL: 0x80 + 0x1d,
34
+ KEY_RALT: 0x80 + 0x38,
35
+ } as const;
36
+
37
+ import { Key, matchesKey, parseKey } from "@mariozechner/pi-tui";
38
+
39
+ /**
40
+ * Map terminal key input to DOOM key codes
41
+ * Supports both raw terminal input and Kitty protocol sequences
42
+ */
43
+ export function mapKeyToDoom(data: string): number[] {
44
+ // Arrow keys
45
+ if (matchesKey(data, Key.up)) return [DoomKeys.KEY_UPARROW];
46
+ if (matchesKey(data, Key.down)) return [DoomKeys.KEY_DOWNARROW];
47
+ if (matchesKey(data, Key.right)) return [DoomKeys.KEY_RIGHTARROW];
48
+ if (matchesKey(data, Key.left)) return [DoomKeys.KEY_LEFTARROW];
49
+
50
+ // WASD - check both raw char and Kitty sequences
51
+ if (data === "w" || matchesKey(data, "w")) return [DoomKeys.KEY_UPARROW];
52
+ if (data === "W" || matchesKey(data, Key.shift("w"))) return [DoomKeys.KEY_UPARROW, DoomKeys.KEY_RSHIFT];
53
+ if (data === "s" || matchesKey(data, "s")) return [DoomKeys.KEY_DOWNARROW];
54
+ if (data === "S" || matchesKey(data, Key.shift("s"))) return [DoomKeys.KEY_DOWNARROW, DoomKeys.KEY_RSHIFT];
55
+ if (data === "a" || matchesKey(data, "a")) return [DoomKeys.KEY_STRAFE_L];
56
+ if (data === "A" || matchesKey(data, Key.shift("a"))) return [DoomKeys.KEY_STRAFE_L, DoomKeys.KEY_RSHIFT];
57
+ if (data === "d" || matchesKey(data, "d")) return [DoomKeys.KEY_STRAFE_R];
58
+ if (data === "D" || matchesKey(data, Key.shift("d"))) return [DoomKeys.KEY_STRAFE_R, DoomKeys.KEY_RSHIFT];
59
+
60
+ // Fire - F key
61
+ if (data === "f" || data === "F" || matchesKey(data, "f") || matchesKey(data, Key.shift("f"))) {
62
+ return [DoomKeys.KEY_FIRE];
63
+ }
64
+
65
+ // Use/Open
66
+ if (data === " " || matchesKey(data, Key.space)) return [DoomKeys.KEY_USE];
67
+
68
+ // Menu/UI keys
69
+ if (matchesKey(data, Key.enter)) return [DoomKeys.KEY_ENTER];
70
+ if (matchesKey(data, Key.escape)) return [DoomKeys.KEY_ESCAPE];
71
+ if (matchesKey(data, Key.tab)) return [DoomKeys.KEY_TAB];
72
+ if (matchesKey(data, Key.backspace)) return [DoomKeys.KEY_BACKSPACE];
73
+
74
+ // Ctrl keys (except Ctrl+C) = fire (legacy support)
75
+ const parsed = parseKey(data);
76
+ if (parsed?.startsWith("ctrl+") && parsed !== "ctrl+c") {
77
+ return [DoomKeys.KEY_FIRE];
78
+ }
79
+ if (data.length === 1 && data.charCodeAt(0) < 32 && data !== "\x03") {
80
+ return [DoomKeys.KEY_FIRE];
81
+ }
82
+
83
+ // Weapon selection (0-9)
84
+ if (data >= "0" && data <= "9") return [data.charCodeAt(0)];
85
+
86
+ // Plus/minus for screen size
87
+ if (data === "+" || data === "=") return [DoomKeys.KEY_EQUALS];
88
+ if (data === "-") return [DoomKeys.KEY_MINUS];
89
+
90
+ // Y/N for prompts
91
+ if (data === "y" || data === "Y" || matchesKey(data, "y") || matchesKey(data, Key.shift("y"))) {
92
+ return ["y".charCodeAt(0)];
93
+ }
94
+ if (data === "n" || data === "N" || matchesKey(data, "n") || matchesKey(data, Key.shift("n"))) {
95
+ return ["n".charCodeAt(0)];
96
+ }
97
+
98
+ // Other printable characters (for cheats)
99
+ if (data.length === 1 && data.charCodeAt(0) >= 32) {
100
+ return [data.toLowerCase().charCodeAt(0)];
101
+ }
102
+
103
+ return [];
104
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * DOOM Overlay Demo - Play DOOM as an overlay
3
+ *
4
+ * Usage: pi --extension ./examples/extensions/doom-overlay
5
+ *
6
+ * Commands:
7
+ * /doom-overlay - Play DOOM in an overlay (Q to pause/exit)
8
+ *
9
+ * This demonstrates that overlays can handle real-time game rendering at 35 FPS.
10
+ */
11
+
12
+ import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
13
+ import { DoomOverlayComponent } from "./doom-component.js";
14
+ import { DoomEngine } from "./doom-engine.js";
15
+ import { ensureWadFile } from "./wad-finder.js";
16
+
17
+ // Persistent engine instance - survives between invocations
18
+ let activeEngine: DoomEngine | null = null;
19
+ let activeWadPath: string | null = null;
20
+
21
+ export default function (pi: ExtensionAPI) {
22
+ pi.registerCommand("doom-overlay", {
23
+ description: "Play DOOM as an overlay. Q to pause and exit.",
24
+
25
+ handler: async (args, ctx) => {
26
+ if (!ctx.hasUI) {
27
+ ctx.ui.notify("DOOM requires interactive mode", "error");
28
+ return;
29
+ }
30
+
31
+ // Auto-download WAD if not present
32
+ ctx.ui.notify("Loading DOOM...", "info");
33
+ const wad = args?.trim() ? args.trim() : await ensureWadFile();
34
+
35
+ if (!wad) {
36
+ ctx.ui.notify("Failed to download DOOM WAD file. Check your internet connection.", "error");
37
+ return;
38
+ }
39
+
40
+ try {
41
+ // Reuse existing engine if same WAD, otherwise create new
42
+ let isResume = false;
43
+ if (activeEngine && activeWadPath === wad) {
44
+ ctx.ui.notify("Resuming DOOM...", "info");
45
+ isResume = true;
46
+ } else {
47
+ ctx.ui.notify(`Loading DOOM from ${wad}...`, "info");
48
+ activeEngine = new DoomEngine(wad);
49
+ await activeEngine.init();
50
+ activeWadPath = wad;
51
+ }
52
+
53
+ await ctx.ui.custom(
54
+ (tui, _theme, _keybindings, done) => {
55
+ return new DoomOverlayComponent(tui, activeEngine!, () => done(undefined), isResume);
56
+ },
57
+ {
58
+ overlay: true,
59
+ overlayOptions: {
60
+ width: "75%",
61
+ maxHeight: "95%",
62
+ anchor: "center",
63
+ margin: { top: 1 },
64
+ },
65
+ },
66
+ );
67
+ } catch (error) {
68
+ ctx.ui.notify(`Failed to load DOOM: ${error}`, "error");
69
+ activeEngine = null;
70
+ activeWadPath = null;
71
+ }
72
+ },
73
+ });
74
+ }
@@ -0,0 +1,51 @@
1
+ import { existsSync, writeFileSync } from "node:fs";
2
+ import { dirname, join, resolve } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+
5
+ // Get the bundled WAD path (relative to this module)
6
+ const __dirname = dirname(fileURLToPath(import.meta.url));
7
+ const BUNDLED_WAD = join(__dirname, "doom1.wad");
8
+ const WAD_URL = "https://distro.ibiblio.org/slitaz/sources/packages/d/doom1.wad";
9
+
10
+ const DEFAULT_WAD_PATHS = ["./doom1.wad", "./DOOM1.WAD", "~/doom1.wad", "~/.doom/doom1.wad"];
11
+
12
+ export function findWadFile(customPath?: string): string | null {
13
+ if (customPath) {
14
+ const resolved = resolve(customPath.replace(/^~/, process.env.HOME || ""));
15
+ if (existsSync(resolved)) return resolved;
16
+ return null;
17
+ }
18
+
19
+ // Check bundled WAD first
20
+ if (existsSync(BUNDLED_WAD)) {
21
+ return BUNDLED_WAD;
22
+ }
23
+
24
+ // Fall back to default paths
25
+ for (const p of DEFAULT_WAD_PATHS) {
26
+ const resolved = resolve(p.replace(/^~/, process.env.HOME || ""));
27
+ if (existsSync(resolved)) return resolved;
28
+ }
29
+
30
+ return null;
31
+ }
32
+
33
+ /** Download the shareware WAD if not present. Returns path or null on failure. */
34
+ export async function ensureWadFile(): Promise<string | null> {
35
+ // Check if already exists
36
+ const existing = findWadFile();
37
+ if (existing) return existing;
38
+
39
+ // Download to bundled location
40
+ try {
41
+ const response = await fetch(WAD_URL);
42
+ if (!response.ok) {
43
+ throw new Error(`HTTP ${response.status}`);
44
+ }
45
+ const buffer = await response.arrayBuffer();
46
+ writeFileSync(BUNDLED_WAD, Buffer.from(buffer));
47
+ return BUNDLED_WAD;
48
+ } catch {
49
+ return null;
50
+ }
51
+ }