@nbtca/prompt 1.5.21 → 1.5.23

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.
Files changed (40) hide show
  1. package/README.md +6 -2
  2. package/dist/app/ambient.js +98 -0
  3. package/dist/app/app.js +3 -2
  4. package/dist/app/chrome.js +6 -2
  5. package/dist/app/views/docs-render.js +23 -15
  6. package/dist/app/views/docs.js +3 -0
  7. package/dist/app/views/events-render.js +57 -21
  8. package/dist/app/views/events.js +62 -9
  9. package/dist/app/views/home.js +66 -31
  10. package/dist/app/views/schedule-render.js +10 -3
  11. package/dist/app/views/schedule.js +16 -5
  12. package/dist/app/views/settings-render.js +13 -2
  13. package/dist/app/views/settings.js +27 -1
  14. package/dist/app/views/student-records-render.js +268 -0
  15. package/dist/app/views/student-records.js +196 -26
  16. package/dist/art/bitmaps.js +76 -0
  17. package/dist/art/glyphs.js +101 -0
  18. package/dist/art/motifs.js +198 -0
  19. package/dist/art/noise.js +43 -0
  20. package/dist/art/planet.js +184 -0
  21. package/dist/art/scene.js +446 -0
  22. package/dist/config/preferences.js +8 -1
  23. package/dist/core/components/empty-state.js +50 -0
  24. package/dist/core/components/menu.js +2 -2
  25. package/dist/core/components/picture.js +18 -0
  26. package/dist/core/logo.js +62 -43
  27. package/dist/core/pixel-art-fine.js +157 -0
  28. package/dist/core/pixel-art.js +223 -0
  29. package/dist/core/theme.js +1 -1
  30. package/dist/features/calendar-heatmap.js +35 -10
  31. package/dist/features/calendar.js +2 -2
  32. package/dist/features/event-art.js +16 -0
  33. package/dist/features/schedule-view.js +10 -0
  34. package/dist/i18n/locales/en.json +56 -6
  35. package/dist/i18n/locales/zh.json +55 -5
  36. package/dist/logo/ascii-emblem-large.txt +22 -0
  37. package/dist/logo/ascii-emblem-medium.txt +15 -0
  38. package/dist/logo/ascii-emblem.txt +11 -0
  39. package/dist/logo/ascii-logo.txt +3 -12
  40. package/package.json +3 -3
@@ -0,0 +1,101 @@
1
+ // Generated by scripts/generate-glyphs.mjs from JetBrains Mono; edit the script, not this file.
2
+ // One entry per printable ASCII character from space to ~: ink coverage 00 to ff for a 4 x 8 grid, row by row.
3
+ export const GLYPH_COLS = 4;
4
+ export const GLYPH_ROWS = 8;
5
+ export const GLYPHS = [
6
+ '0000000000000000000000000000000000000000000000000000000000000000',
7
+ '0000000000181800005858000051510000424200003d3d000014140000000000',
8
+ '00000000032a2a03099f9e090259580200000000000000000000000000000000',
9
+ '000000000020041d007d2f4e39b2a85b5fa4b239522b7d001402170000000000',
10
+ '00050700005768004b9b964e32be63000037a5605d959f60004b5a0000010200',
11
+ '00000000262900239293325473867700007c8c7c5e28989518001b1800000000',
12
+ '00000000003e2a0038828d0c29c30b07a22ba08a9f549f800b34031e00000000',
13
+ '000000000017170000545400002d2d0000000000000000000000000000000000',
14
+ '000000000011912200911f00009e0000009e0000008f210000108f2200000000',
15
+ '0000000022911100001f910000009e0000009e0000218f00228f100000000000',
16
+ '0000000000000000002c2c006294946202a0a002143737140000000000000000',
17
+ '0000000000000000001414000b5b5b0b3e99993e002f2f000000000000000000',
18
+ '0000000000000000000000000000000000000000003a4b00007b310000170200',
19
+ '00000000000000000000000001141401086c6c08000000000000000000000000',
20
+ '0000000000000000000000000000000000000000005c5c000016160000000000',
21
+ '000000000000474a0000a107002286000088210008a000004a45000000000000',
22
+ '00000000003b3b00596a6a5979434379793838794f7b7b500028280000000000',
23
+ '0000000000212300418f870004178700001787002364af34132e2e1c00000000',
24
+ '00000000003c3b005c6a735700005f4f004782003dc1552c142e2e1800000000',
25
+ '000000001b4242131a46bb1e00739c0a0701476062728a41002d240000000000',
26
+ '0000000000002f00004a6b0012a2231b806f8a461d367b460000100d00000000',
27
+ '0000000017424212576e41114f9c7f0d060141685a788449002a260000000000',
28
+ '0000000000062800007737001fc97510852525855e77775d0028280000000000',
29
+ '000000001f42422b735d638700008d1f000f9d0000733a00001f000000000000',
30
+ '00000000003b3b0055737355309392306e4f4f6e637575630029290000000000',
31
+ '00000000003b3c0068646469803030800a6ad018004a6d00001f030000000000',
32
+ '0000000000000000001616000056560000000000005454000018180000000000',
33
+ '0000000000000000001717000059590000000000004045000084290000180100',
34
+ '0000000000000000000011271b7e7a174292330000065d400000000000000000',
35
+ '0000000000000000000000003980803927595927112727110000000000000000',
36
+ '000000000000000027110000197f821b002e8e423e5d06000000000000000000',
37
+ '0000000009421b000a47a51e001c9a23008c2900006911000025030000000000',
38
+ '00000000003a45046f4d38838d2e7aa08d68258d8e2e826363642e0000293f00',
39
+ '00000000001e1e00008f91000196970233b8b8347c25237c1d00001d00000000',
40
+ '000000001a422e0063697e5163828c31634e52666378805e122e1f0000000000',
41
+ '0000000000373e00487d685b65390000653902063f8b7a5600242a0000000000',
42
+ '000000001a422600656988426539326c6539336b6578943a122e160000000000',
43
+ '000000001742421f5774411f578e641857591c0757835528102e2e1600000000',
44
+ '0000000019424222606d412160754c1960683713603e0000110b000000000000',
45
+ '0000000000393c004e776d546c3827126c3f767646867e4f0026290000000000',
46
+ '000000001a0f0f1a633b3b636383836363575763633b3b63120b0b1200000000',
47
+ '0000000014424214147d7d1400505000005050001a8b8b1a0e2e2e0e00000000',
48
+ '000000000000141400004f4f00004f4f11004f4f855a9a2902331c0000000000',
49
+ '000000001a0f052865396d4165819f006556ad026539575f120a012000000000',
50
+ '00000000091f000024790000247900002479000024a65539072e2e1f00000000',
51
+ '00000000231c1c2387827d8787847e8787232187871212871903031900000000',
52
+ '000000001b1e0c1b68a8306868a04d686847a7686830a4681309151300000000',
53
+ '00000000003a3a004d76764d6c32326c6c32326c458585450027270000000000',
54
+ '000000001a423702656a5c826555428e6581510765390000120a000000000000',
55
+ '00000000003b3b00596f6f5979242479792424794f8080500028a7170000141e',
56
+ '000000001a423300636a6b6c636162716375ba026339614d120a041c00000000',
57
+ '00000000003b38004f73723d33a14a01060d64685d78755d00292a0000000000',
58
+ '000000002a42422a2a7b7b29004f4f00004f4f00004f4f00000e0e0000000000',
59
+ '000000001b0e0e1b683535686835356868353568448686450028280000000000',
60
+ '000000002a00002a772a2a762e716e2e019d960100918d000015140000000000',
61
+ '000000002616182490686a898c6e6d858275727e6288875f0e17170e00000000',
62
+ '00000000280806283979723800908a0000989f0054595e552001012000000000',
63
+ '000000002c00002b6247426203a29d0300626200004f4f00000e0e0000000000',
64
+ '000000001b4242191a419f39001e8c00009b0f00519b5526142e2e1400000000',
65
+ '00000000008b8001009e0000009e0000009e0000009e00000089800100000000',
66
+ '000000004a47000008a1000000862200002188000000a0080000454a00000000',
67
+ '0000000001808b0000009e0000009e0000009e0000009e000180890000000000',
68
+ '0000000000171800008684002d615f2e0e03030e000000000000000000000000',
69
+ '0000000000000000000000000000000000000000000000004978784900000000',
70
+ '00000000005e0b0000141b000000000000000000000000000000000000000000',
71
+ '0000000000000000002627002c6d8249428490657761806502351d1200000000',
72
+ '000000001a0f0000654b2b006582814c6539306e65807f4e121f2e0000000000',
73
+ '000000000000000000222800438977516c3200004587745200252b0000000000',
74
+ '0000000000000f1a002b4b654c8181656e3039654e7f8065002e1f1200000000',
75
+ '000000000000000000242400477a7a4a7287794b487e6a300026290000000000',
76
+ '00000000000a40240088502354ca884700930b0000930b00001b020000000000',
77
+ '0000000000000000002b1d114b8483636b333a632e9c9363003a7e4c00413400',
78
+ '000000001a0f0000654c2b006580844c6539356865393568120a0a1300000000',
79
+ '00000000002c48000d3b31001b70790000247900266da63c142e2e2100000000',
80
+ '0000000000007301132b53002758b70000009e0000009e001945a3001c420c00',
81
+ '00000000181100005d41041b5d4183355d9fa7005d417744110c022000000000',
82
+ '000000003642050034a81300008a1300008a130000746c3900022a1f00000000',
83
+ '0000000000000000172a200d9577718d94414194944141941b0c0c1b00000000',
84
+ '0000000000000000111d2b006580844c6539356865393568120a0a1300000000',
85
+ '00000000000000000022220047858547722b2b72488382480024240000000000',
86
+ '0000000000000000111c2b006582814d6539306e657f7f4e654c2e001c0f0000',
87
+ '0000000000000000002b1c114d8082656e3039654e7e7f65002e4d6500000f1c',
88
+ '00000000000000000b1b320243996a6e435b0526435b00000c10000000000000',
89
+ '0000000000000000002a2900507b692d1e8d942c4168735c002e2e0000000000',
90
+ '00000000001100001eae2b143dbf582a009e0000008f5b2600062d1400000000',
91
+ '0000000000000000120909126835356868353568438687420026260000000000',
92
+ '00000000000000001b02011b67413e6710949010009793000014140000000000',
93
+ '0000000000000000170e0e168768687f7c747576538d914e0a17180900000000',
94
+ '000000000000000017080818278d8c29008f9000338383331d05051d00000000',
95
+ '00000000000000001b02021b5d4c4461069e9609007b860000802500002d0000',
96
+ '00000000000000000f2b2b0e1f58c0320066550040b25523132e2e1300000000',
97
+ '00000000000f993000247a00225967001f576d0000247a00000e973000000000',
98
+ '0000000000444400004f4f00004f4f00004f4f00004f4f000043430000000000',
99
+ '00000000309a110000811e00007e4e2200864a1f00801f003098100000000000',
100
+ '000000000000000000000000327e154e581c863b000000000000000000000000',
101
+ ];
@@ -0,0 +1,198 @@
1
+ import chalk from 'chalk';
2
+ import { gasGiantLines, moonLines, tint } from './planet.js';
3
+ import { getCapabilities } from '../core/capabilities.js';
4
+ import { pictureMode } from '../core/pixel-art.js';
5
+ const RAMP = '.-:=+*xo#%&@';
6
+ const ROWS = 6;
7
+ const MOON_COLS = 12;
8
+ const MOON_AT = 7;
9
+ const WIDTH = 22;
10
+ const STAR_PALE = [200, 212, 255];
11
+ const STAR_WARM = [255, 226, 150];
12
+ const STARS = [
13
+ [1, 1, '*', STAR_WARM],
14
+ [4, 0, '.', STAR_PALE],
15
+ [3, 3, '+', STAR_PALE],
16
+ [0, 5, '.', STAR_PALE],
17
+ [6, 4, '.', STAR_WARM],
18
+ [MOON_AT + MOON_COLS + 1, 1, '.', STAR_PALE],
19
+ [MOON_AT + MOON_COLS + 2, 3, '*', STAR_WARM],
20
+ [MOON_AT + MOON_COLS, 5, '.', STAR_PALE],
21
+ ];
22
+ const plain = (mode) => mode === 'tone';
23
+ /** A crescent moon among a few stars: nothing is happening tonight. */
24
+ export function moonSky(mode = pictureMode()) {
25
+ const waning = new Date(Date.UTC(2000, 0, 6, 18, 14) + 21.5 * 86_400_000);
26
+ const moon = moonLines(MOON_COLS, waning, mode === 'fine' ? 'color' : mode) ?? [];
27
+ return Array.from({ length: ROWS }, (_, row) => {
28
+ const cells = new Map(STARS.filter(([, starRow]) => starRow === row).map(([col, , char, color]) => [
29
+ col,
30
+ plain(mode) ? char : chalk.rgb(...color)(char),
31
+ ]));
32
+ const moonLine = moon[row] ?? '';
33
+ const moonWidth = Math.max(0, ...moon.map((line) => line.replace(/\x1b\[[0-9;]*m/g, '').length));
34
+ let line = '';
35
+ for (let col = 0; col < MOON_AT; col++)
36
+ line += cells.get(col) ?? ' ';
37
+ line += moonLine;
38
+ const visible = moonLine.replace(/\x1b\[[0-9;]*m/g, '').length;
39
+ line += ' '.repeat(Math.max(0, moonWidth - visible));
40
+ for (let col = MOON_AT + moonWidth; col < WIDTH; col++)
41
+ line += cells.get(col) ?? ' ';
42
+ return line.replace(/\s+$/, '');
43
+ });
44
+ }
45
+ const GLASS = [125, 211, 252];
46
+ const HANDLE = [245, 158, 11];
47
+ const LENS = { x: 5.6, y: 5.4, radius: 5.1, rim: 1.25 };
48
+ /** A magnifying glass with a rim lit from the upper left and a glint on the glass. */
49
+ export function magnifier(mode = pictureMode()) {
50
+ const handleFrom = {
51
+ x: LENS.x + LENS.radius * 0.72,
52
+ y: LENS.y + LENS.radius * 0.72,
53
+ };
54
+ const handleTo = { x: handleFrom.x + 4.2, y: handleFrom.y + 3.2 };
55
+ const segment = { x: handleTo.x - handleFrom.x, y: handleTo.y - handleFrom.y };
56
+ const length = segment.x ** 2 + segment.y ** 2;
57
+ return Array.from({ length: ROWS }, (_, row) => {
58
+ let line = '';
59
+ for (let col = 0; col < 14; col++) {
60
+ const x = col + 0.5;
61
+ const y = (row + 0.5) * 2;
62
+ const dx = x - LENS.x;
63
+ const dy = y - LENS.y;
64
+ const dist = Math.hypot(dx, dy);
65
+ const along = Math.max(0, Math.min(1, ((x - handleFrom.x) * segment.x + (y - handleFrom.y) * segment.y) / length));
66
+ const handleDist = Math.hypot(x - (handleFrom.x + along * segment.x), y - (handleFrom.y + along * segment.y));
67
+ let char = ' ';
68
+ let color = GLASS;
69
+ let level = 0;
70
+ if (Math.abs(dist - LENS.radius) < LENS.rim) {
71
+ const facing = (-dx - dy) / (dist * Math.SQRT2 || 1);
72
+ level = 0.62 + 0.38 * facing;
73
+ char = RAMP[Math.min(RAMP.length - 1, Math.max(6, Math.floor(level * RAMP.length)))] ?? '+';
74
+ }
75
+ else if (handleDist < 1.2 && dist > LENS.radius) {
76
+ char = '\\';
77
+ color = HANDLE;
78
+ level = 0.55;
79
+ }
80
+ else if (dist < LENS.radius) {
81
+ const glint = Math.max(0, 1 - Math.hypot(x - (LENS.x - 1.9), y - (LENS.y - 1.9)) / 2.2);
82
+ if (glint > 0.2) {
83
+ level = glint;
84
+ char = RAMP[Math.min(5, Math.floor(glint * 6))] ?? '.';
85
+ color = [226, 242, 255];
86
+ }
87
+ }
88
+ line += char === ' ' || plain(mode) ? char : tint(color, level, char);
89
+ }
90
+ return line.replace(/\s+$/, '');
91
+ });
92
+ }
93
+ const ANSI = /(?:\x1b\[[0-9;]*m)*[^\x1b](?:\x1b\[[0-9;]*m)*/gu;
94
+ const ORBIT = {
95
+ cols: 22,
96
+ centre: { x: 11, y: 3 },
97
+ radiusX: 10,
98
+ radiusY: 2.3,
99
+ period: 3200,
100
+ spin: 6400,
101
+ };
102
+ const MOON_ON_ORBIT = [226, 232, 240];
103
+ const ORBIT_TRAIL = [71, 85, 105];
104
+ /** A small planet with a moon circling it; the moon moves one step per frame. */
105
+ export function orbit(at, mode = pictureMode()) {
106
+ const grid = Array.from({ length: ROWS }, () => Array.from({ length: ORBIT.cols }, () => ' '));
107
+ const place = (x, y, cell) => {
108
+ const row = grid[y];
109
+ if (row && x >= 0 && x < ORBIT.cols)
110
+ row[x] = cell;
111
+ };
112
+ const dot = (angle, cell) => {
113
+ place(Math.round(ORBIT.centre.x + ORBIT.radiusX * Math.cos(angle)), Math.round(ORBIT.centre.y - 0.5 + ORBIT.radiusY * Math.sin(angle)), cell);
114
+ };
115
+ const trail = plain(mode) ? '.' : chalk.rgb(...ORBIT_TRAIL)('.');
116
+ for (let step = 0; step < 24; step++)
117
+ dot((step / 24) * 2 * Math.PI, trail);
118
+ const turn = (at / ORBIT.spin) * 2 * Math.PI;
119
+ const planet = gasGiantLines(8, mode === 'fine' ? 'color' : mode, turn) ?? [];
120
+ planet.forEach((line, index) => {
121
+ (line.match(ANSI) ?? []).forEach((cell, col) => {
122
+ if (cell.replace(/\x1b\[[0-9;]*m/g, '') !== ' ')
123
+ place(7 + col, 1 + index, cell);
124
+ });
125
+ });
126
+ const angle = ((at % ORBIT.period) / ORBIT.period) * 2 * Math.PI;
127
+ const x = Math.round(ORBIT.centre.x + ORBIT.radiusX * Math.cos(angle));
128
+ const y = Math.round(ORBIT.centre.y - 0.5 + ORBIT.radiusY * Math.sin(angle));
129
+ const behind = Math.sin(angle) < 0 && grid[y]?.[x] !== ' ' && x > 6 && x < 15 && y > 0 && y < 5;
130
+ if (!behind)
131
+ place(x, y, plain(mode) ? 'o' : chalk.rgb(...MOON_ON_ORBIT)('o'));
132
+ return grid.map((row) => row.join('').replace(/\s+$/, ''));
133
+ }
134
+ const STEEL = [148, 163, 184];
135
+ const WAVE = [71, 85, 105];
136
+ const LOST = [248, 113, 113];
137
+ const TOWER = [
138
+ [[' x', LOST]],
139
+ [
140
+ [' . ( ', WAVE],
141
+ ['|', STEEL],
142
+ [' ) .', WAVE],
143
+ ],
144
+ [
145
+ [' . ( ( ', WAVE],
146
+ ['/|\\', STEEL],
147
+ [' ) ) .', WAVE],
148
+ ],
149
+ [
150
+ [' ', undefined],
151
+ ['/|\\', STEEL],
152
+ ],
153
+ [
154
+ [' ', undefined],
155
+ ['/ | \\', STEEL],
156
+ ],
157
+ [
158
+ [' ', undefined],
159
+ ['/__|__\\', STEEL],
160
+ ],
161
+ ];
162
+ /** A transmitter whose signal has dropped: dim waves and a red cross above the mast. */
163
+ export function signalLost(mode = pictureMode()) {
164
+ return TOWER.map((parts) => parts
165
+ .map(([text, color]) => (plain(mode) || !color ? text : chalk.rgb(...color)(text)))
166
+ .join('')
167
+ .replace(/\s+$/, ''));
168
+ }
169
+ const PORCELAIN = [226, 232, 240];
170
+ const TEA = [217, 119, 6];
171
+ const STEAM = [148, 163, 184];
172
+ const STEAM_FRAMES = [
173
+ [' ) ( ) ', ' ( ) ( '],
174
+ [' ( ) ( ', ' ) ( ) '],
175
+ [' ( ) ( ', ' ) ( ) '],
176
+ [' ) ( ) ', ' ( ) ( '],
177
+ ];
178
+ const STEAM_MS = 500;
179
+ const CUP = [
180
+ [[' ,---------.', PORCELAIN]],
181
+ [
182
+ [' |', PORCELAIN],
183
+ ['~~~~~~~~~', TEA],
184
+ ['|', PORCELAIN],
185
+ ['-.', PORCELAIN],
186
+ ],
187
+ [[' | | |', PORCELAIN]],
188
+ [[" \\_______/-'", PORCELAIN]],
189
+ ];
190
+ /** A cup of tea with drifting steam: a day without classes. */
191
+ export function teaCup(at, mode = pictureMode()) {
192
+ const frame = getCapabilities().reducedMotion
193
+ ? 0
194
+ : Math.floor(at / STEAM_MS) % STEAM_FRAMES.length;
195
+ const steam = (STEAM_FRAMES[frame] ?? STEAM_FRAMES[0] ?? ['', '']).map((line) => plain(mode) ? line : chalk.rgb(...STEAM)(line));
196
+ const cup = CUP.map((parts) => parts.map(([text, color]) => (plain(mode) ? text : chalk.rgb(...color)(text))).join(''));
197
+ return [...steam, ...cup].map((line) => line.replace(/\s+$/, ''));
198
+ }
@@ -0,0 +1,43 @@
1
+ function hash(x, y, z, seed) {
2
+ let h = Math.imul(x, 374761393) ^ Math.imul(y, 668265263) ^ Math.imul(z, 1274126177) ^ seed;
3
+ h = Math.imul(h ^ (h >>> 13), 1274126177);
4
+ return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
5
+ }
6
+ const smooth = (t) => t * t * (3 - 2 * t);
7
+ const mix = (a, b, t) => a + (b - a) * t;
8
+ /** Smooth value noise in 0..1. */
9
+ export function noise3([x, y, z], seed = 0) {
10
+ const [ix, iy, iz] = [Math.floor(x), Math.floor(y), Math.floor(z)];
11
+ const [fx, fy, fz] = [smooth(x - ix), smooth(y - iy), smooth(z - iz)];
12
+ const at = (dx, dy, dz) => hash(ix + dx, iy + dy, iz + dz, seed);
13
+ return mix(mix(mix(at(0, 0, 0), at(1, 0, 0), fx), mix(at(0, 1, 0), at(1, 1, 0), fx), fy), mix(mix(at(0, 0, 1), at(1, 0, 1), fx), mix(at(0, 1, 1), at(1, 1, 1), fx), fy), fz);
14
+ }
15
+ /** Fractal Brownian motion: octaves of value noise, in 0..1. */
16
+ export function fbm3(p, octaves = 3, seed = 0) {
17
+ let sum = 0;
18
+ let amplitude = 0.5;
19
+ let scale = 1;
20
+ for (let octave = 0; octave < octaves; octave++) {
21
+ sum += amplitude * noise3([p[0] * scale, p[1] * scale, p[2] * scale], seed + octave * 17);
22
+ amplitude /= 2;
23
+ scale *= 2;
24
+ }
25
+ return sum / (1 - 0.5 ** octaves);
26
+ }
27
+ /** Worley noise: distance to the nearest of one random feature point per unit cell, which makes crater bowls. */
28
+ export function worley3([x, y, z], seed = 0) {
29
+ const [ix, iy, iz] = [Math.floor(x), Math.floor(y), Math.floor(z)];
30
+ let nearest = Number.POSITIVE_INFINITY;
31
+ for (let dx = -1; dx <= 1; dx++) {
32
+ for (let dy = -1; dy <= 1; dy++) {
33
+ for (let dz = -1; dz <= 1; dz++) {
34
+ const [cx, cy, cz] = [ix + dx, iy + dy, iz + dz];
35
+ const px = cx + hash(cx, cy, cz, seed + 1);
36
+ const py = cy + hash(cx, cy, cz, seed + 2);
37
+ const pz = cz + hash(cx, cy, cz, seed + 3);
38
+ nearest = Math.min(nearest, (px - x) ** 2 + (py - y) ** 2 + (pz - z) ** 2);
39
+ }
40
+ }
41
+ }
42
+ return Math.sqrt(nearest);
43
+ }
@@ -0,0 +1,184 @@
1
+ import chalk from 'chalk';
2
+ import { fbm3, worley3 } from './noise.js';
3
+ import { moonPhase } from './scene.js';
4
+ import { pictureMode } from '../core/pixel-art.js';
5
+ // Light to dark ink, as in classic tone-shaded ASCII spheres.
6
+ const RAMP = '.-:=+*xo#%&@';
7
+ const MIN_COLS = 8;
8
+ const MAX_COLS = 44;
9
+ const normalize = (v) => {
10
+ const length = Math.hypot(...v);
11
+ return [v[0] / length, v[1] / length, v[2] / length];
12
+ };
13
+ function shadeAt(sphere, x, y) {
14
+ const radius = x * x + y * y;
15
+ if (radius > 1)
16
+ return undefined;
17
+ const z = Math.sqrt(1 - radius);
18
+ const surface = sphere.sample([x, y, z]);
19
+ const [nx, ny, nz] = surface.normal ?? [x, y, z];
20
+ const lit = Math.max(0, nx * sphere.light[0] + ny * sphere.light[1] + nz * sphere.light[2]);
21
+ return { level: Math.min(1, lit * surface.albedo * (0.55 + 0.45 * z)), color: surface.color };
22
+ }
23
+ const NIGHT = [48, 54, 70];
24
+ export function tint(color, level, char) {
25
+ const gain = 0.62 + 0.38 * level;
26
+ return chalk.rgb(Math.round(color[0] * gain), Math.round(color[1] * gain), Math.round(color[2] * gain))(char);
27
+ }
28
+ function drawSphere(cols, sphere, mode) {
29
+ if (mode === 'fine')
30
+ return drawFineSphere(cols, sphere);
31
+ const rows = Math.round(cols / 2);
32
+ const colour = mode !== 'tone';
33
+ return Array.from({ length: rows }, (_, row) => {
34
+ let line = '';
35
+ for (let col = 0; col < cols; col++) {
36
+ const shade = shadeAt(sphere, ((col + 0.5) / cols) * 2 - 1, ((row + 0.5) / rows) * 2 - 1);
37
+ if (!shade) {
38
+ line += ' ';
39
+ continue;
40
+ }
41
+ if (shade.level < 0.02) {
42
+ line += colour ? chalk.rgb(...NIGHT)('.') : ' ';
43
+ continue;
44
+ }
45
+ const char = RAMP[Math.min(RAMP.length - 1, Math.floor(shade.level * RAMP.length))] ?? ' ';
46
+ line += colour ? tint(shade.color, shade.level, char) : char;
47
+ }
48
+ return line.replace(/\s+$/, '');
49
+ });
50
+ }
51
+ const SUPERSAMPLE = 3;
52
+ const EDGE_COVERAGE = 0.4;
53
+ /** One truecolor pixel of the sphere, antialiased at the rim; undefined outside it. */
54
+ function fineColor(sphere, cols, px, py) {
55
+ const size = cols;
56
+ let covered = 0;
57
+ const sum = [0, 0, 0];
58
+ for (let i = 0; i < SUPERSAMPLE; i++) {
59
+ for (let j = 0; j < SUPERSAMPLE; j++) {
60
+ const x = ((px + (i + 0.5) / SUPERSAMPLE) / size) * 2 - 1;
61
+ const y = ((py + (j + 0.5) / SUPERSAMPLE) / size) * 2 - 1;
62
+ const shade = shadeAt(sphere, x, y);
63
+ if (!shade)
64
+ continue;
65
+ covered++;
66
+ const bright = Math.pow(shade.level, 0.75);
67
+ for (let channel = 0; channel < 3; channel++)
68
+ sum[channel] =
69
+ (sum[channel] ?? 0) +
70
+ (NIGHT[channel] ?? 0) * 0.6 * (1 - bright) +
71
+ (shade.color[channel] ?? 0) * bright;
72
+ }
73
+ }
74
+ if (covered < EDGE_COVERAGE * SUPERSAMPLE ** 2)
75
+ return undefined;
76
+ return [sum[0] / covered, sum[1] / covered, sum[2] / covered];
77
+ }
78
+ /** Half blocks with a smoothly shaded colour on each half: twice the character resolution, no dithering. */
79
+ function drawFineSphere(cols, sphere) {
80
+ const rows = Math.round(cols / 2);
81
+ return Array.from({ length: rows }, (_, row) => {
82
+ let line = '';
83
+ for (let col = 0; col < cols; col++) {
84
+ const top = fineColor(sphere, cols, col, row * 2);
85
+ const bottom = fineColor(sphere, cols, col, row * 2 + 1);
86
+ const rgb = (v) => [Math.round(v[0]), Math.round(v[1]), Math.round(v[2])];
87
+ if (top && bottom)
88
+ line += chalk.rgb(...rgb(top)).bgRgb(...rgb(bottom))('▀');
89
+ else if (top)
90
+ line += chalk.rgb(...rgb(top))('▀');
91
+ else if (bottom)
92
+ line += chalk.rgb(...rgb(bottom))('▄');
93
+ else
94
+ line += ' ';
95
+ }
96
+ return line.replace(/\s+$/, '');
97
+ });
98
+ }
99
+ const smoothstep = (low, high, value) => {
100
+ const t = Math.min(1, Math.max(0, (value - low) / (high - low)));
101
+ return t * t * (3 - 2 * t);
102
+ };
103
+ /** The point as the texture sees it: turned about the vertical axis. */
104
+ function turned([x, y, z], angle) {
105
+ const [cos, sin] = [Math.cos(angle), Math.sin(angle)];
106
+ return [x * cos + z * sin, y, -x * sin + z * cos];
107
+ }
108
+ const EPSILON = 0.03;
109
+ const RELIEF = 0.9;
110
+ /**
111
+ * Bumps a unit sphere's normal by the slope of a height field, as bump mapping does: the field's gradient,
112
+ * with its component along the normal removed, tilts the normal away from rising ground.
113
+ */
114
+ function bumped(point, height) {
115
+ const base = height(point);
116
+ const gradient = [
117
+ (height([point[0] + EPSILON, point[1], point[2]]) - base) / EPSILON,
118
+ (height([point[0], point[1] + EPSILON, point[2]]) - base) / EPSILON,
119
+ (height([point[0], point[1], point[2] + EPSILON]) - base) / EPSILON,
120
+ ];
121
+ const along = gradient[0] * point[0] + gradient[1] * point[1] + gradient[2] * point[2];
122
+ return normalize([
123
+ point[0] - RELIEF * (gradient[0] - along * point[0]),
124
+ point[1] - RELIEF * (gradient[1] - along * point[1]),
125
+ point[2] - RELIEF * (gradient[2] - along * point[2]),
126
+ ]);
127
+ }
128
+ const CRATER = 0.34;
129
+ /** Crater bowls with raised rims over rolling highlands. */
130
+ function lunarHeight(p, turn) {
131
+ const q = turned(p, turn);
132
+ const scaled = [q[0] * 1.7, q[1] * 1.7, q[2] * 1.7];
133
+ const d = worley3(scaled, 7);
134
+ const bowl = -Math.pow(Math.max(0, 1 - d / CRATER), 0.7) * 0.38;
135
+ const rim = Math.exp(-(((d - CRATER) / 0.06) ** 2)) * 0.2;
136
+ return bowl + rim + fbm3([q[0] * 3, q[1] * 3, q[2] * 3], 3, 3) * 0.12;
137
+ }
138
+ /** The moon at today's phase, lit from the side the real moon is lit from. */
139
+ export function moonLines(cols, now, mode = pictureMode(), turn = 0.9) {
140
+ if (cols < MIN_COLS)
141
+ return undefined;
142
+ const angle = 2 * Math.PI * moonPhase(now);
143
+ return drawSphere(Math.min(cols, MAX_COLS), {
144
+ light: normalize([Math.sin(angle), -0.12, -Math.cos(angle)]),
145
+ sample: (point) => {
146
+ const q = turned(point, turn);
147
+ const maria = smoothstep(0.42, 0.6, fbm3([q[0] * 1.3, q[1] * 1.3, q[2] * 1.3], 3, 11));
148
+ return {
149
+ albedo: 1 - 0.38 * maria,
150
+ color: [236, 238, 246],
151
+ normal: bumped(point, (p) => lunarHeight(p, turn)),
152
+ };
153
+ },
154
+ }, mode);
155
+ }
156
+ const STORM = { lon: 0.55, lat: 0.34, width: 0.5, height: 0.15 };
157
+ /** A banded gas giant lit from the upper left, with turbulent bands and a storm that turns with it. */
158
+ export function gasGiantLines(cols, mode = pictureMode(), turn = 0.9) {
159
+ if (cols < MIN_COLS)
160
+ return undefined;
161
+ return drawSphere(Math.min(cols, MAX_COLS), {
162
+ light: normalize([-0.6, -0.45, 0.66]),
163
+ sample: (point) => {
164
+ const q = turned(point, turn);
165
+ const turbulence = fbm3([q[0] * 2.4, q[1] * 9, q[2] * 2.4], 3, 5);
166
+ const band = 0.5 + 0.5 * Math.sin(q[1] * 11 + turbulence * 3.2);
167
+ const lon = Math.atan2(q[0], q[2]);
168
+ const spread = Math.cos(Math.asin(Math.max(-1, Math.min(1, q[1]))));
169
+ const dLon = ((lon - STORM.lon + Math.PI * 3) % (Math.PI * 2)) - Math.PI;
170
+ const storm = Math.exp(-(((dLon * spread) / STORM.width) ** 2) - ((q[1] - STORM.lat) / STORM.height) ** 2);
171
+ const base = [236 - 40 * band, 176 - 30 * band, 120 + 40 * band];
172
+ const spot = [214, 92, 66];
173
+ return {
174
+ albedo: 0.82 + 0.18 * band + 0.1 * storm,
175
+ color: [
176
+ base[0] + (spot[0] - base[0]) * storm,
177
+ base[1] + (spot[1] - base[1]) * storm,
178
+ base[2] + (spot[2] - base[2]) * storm,
179
+ ],
180
+ };
181
+ },
182
+ }, mode);
183
+ }
184
+ export const planetSize = (cols, rows) => Math.min(MAX_COLS, cols, rows * 2);