@datamoc/mw_games 0.20.0 → 0.22.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.
Files changed (115) hide show
  1. package/CHANGELOG.md +83 -0
  2. package/dist/audio/Channels.d.ts +19 -0
  3. package/dist/audio/Channels.d.ts.map +1 -1
  4. package/dist/audio/Channels.js +33 -4
  5. package/dist/audio/Channels.js.map +1 -1
  6. package/dist/audio/LoopTags.d.ts +37 -0
  7. package/dist/audio/LoopTags.d.ts.map +1 -0
  8. package/dist/audio/LoopTags.js +116 -0
  9. package/dist/audio/LoopTags.js.map +1 -0
  10. package/dist/audio/Midi.d.ts +5 -0
  11. package/dist/audio/Midi.d.ts.map +1 -1
  12. package/dist/audio/Midi.js +9 -1
  13. package/dist/audio/Midi.js.map +1 -1
  14. package/dist/audio/MidiRender.d.ts +3 -2
  15. package/dist/audio/MidiRender.d.ts.map +1 -1
  16. package/dist/audio/MidiRender.js +2 -2
  17. package/dist/audio/MidiRender.js.map +1 -1
  18. package/dist/audio/index.d.ts +3 -1
  19. package/dist/audio/index.d.ts.map +1 -1
  20. package/dist/audio/index.js +1 -0
  21. package/dist/audio/index.js.map +1 -1
  22. package/dist/board/bitboard.d.ts +132 -0
  23. package/dist/board/bitboard.d.ts.map +1 -0
  24. package/dist/board/bitboard.js +495 -0
  25. package/dist/board/bitboard.js.map +1 -0
  26. package/dist/board/chess.d.ts +16 -0
  27. package/dist/board/chess.d.ts.map +1 -1
  28. package/dist/board/chess.js +43 -0
  29. package/dist/board/chess.js.map +1 -1
  30. package/dist/board/index.d.ts +9 -1
  31. package/dist/board/index.d.ts.map +1 -1
  32. package/dist/board/index.js +5 -1
  33. package/dist/board/index.js.map +1 -1
  34. package/dist/board/openings.d.ts +58 -0
  35. package/dist/board/openings.d.ts.map +1 -0
  36. package/dist/board/openings.js +45 -0
  37. package/dist/board/openings.js.map +1 -0
  38. package/dist/board/tablebase.d.ts +118 -0
  39. package/dist/board/tablebase.d.ts.map +1 -0
  40. package/dist/board/tablebase.js +414 -0
  41. package/dist/board/tablebase.js.map +1 -0
  42. package/dist/board/tourney.d.ts +90 -0
  43. package/dist/board/tourney.d.ts.map +1 -0
  44. package/dist/board/tourney.js +1013 -0
  45. package/dist/board/tourney.js.map +1 -0
  46. package/dist/core/JavaRandom.d.ts +55 -0
  47. package/dist/core/JavaRandom.d.ts.map +1 -0
  48. package/dist/core/JavaRandom.js +81 -0
  49. package/dist/core/JavaRandom.js.map +1 -0
  50. package/dist/core/index.d.ts +2 -0
  51. package/dist/core/index.d.ts.map +1 -1
  52. package/dist/core/index.js +1 -0
  53. package/dist/core/index.js.map +1 -1
  54. package/dist/mw_games.global.js +123 -116
  55. package/dist/rpg/EventRunner.d.ts +7 -0
  56. package/dist/rpg/EventRunner.d.ts.map +1 -1
  57. package/dist/rpg/EventRunner.js +6 -0
  58. package/dist/rpg/EventRunner.js.map +1 -1
  59. package/dist/two-d/render/FogLayer.d.ts +68 -0
  60. package/dist/two-d/render/FogLayer.d.ts.map +1 -0
  61. package/dist/two-d/render/FogLayer.js +83 -0
  62. package/dist/two-d/render/FogLayer.js.map +1 -0
  63. package/dist/two-d/render/LiquidLayer.d.ts +62 -0
  64. package/dist/two-d/render/LiquidLayer.d.ts.map +1 -0
  65. package/dist/two-d/render/LiquidLayer.js +102 -0
  66. package/dist/two-d/render/LiquidLayer.js.map +1 -0
  67. package/dist/two-d/render/RpgmAutotile.d.ts +5 -0
  68. package/dist/two-d/render/RpgmAutotile.d.ts.map +1 -1
  69. package/dist/two-d/render/RpgmAutotile.js +4 -3
  70. package/dist/two-d/render/RpgmAutotile.js.map +1 -1
  71. package/dist/two-d/render/TileMap.d.ts +14 -2
  72. package/dist/two-d/render/TileMap.d.ts.map +1 -1
  73. package/dist/two-d/render/TileMap.js +32 -2
  74. package/dist/two-d/render/TileMap.js.map +1 -1
  75. package/dist/two-d/render/index.d.ts +4 -0
  76. package/dist/two-d/render/index.d.ts.map +1 -1
  77. package/dist/two-d/render/index.js +2 -0
  78. package/dist/two-d/render/index.js.map +1 -1
  79. package/dist/two-d/ui/Label.d.ts.map +1 -1
  80. package/dist/two-d/ui/Label.js +11 -2
  81. package/dist/two-d/ui/Label.js.map +1 -1
  82. package/dist/two-d/ui/ListView.d.ts +24 -0
  83. package/dist/two-d/ui/ListView.d.ts.map +1 -1
  84. package/dist/two-d/ui/ListView.js +97 -4
  85. package/dist/two-d/ui/ListView.js.map +1 -1
  86. package/dist/two-d/ui/MessageLog.d.ts +62 -0
  87. package/dist/two-d/ui/MessageLog.d.ts.map +1 -0
  88. package/dist/two-d/ui/MessageLog.js +114 -0
  89. package/dist/two-d/ui/MessageLog.js.map +1 -0
  90. package/dist/two-d/ui/RadioGroup.d.ts +68 -0
  91. package/dist/two-d/ui/RadioGroup.d.ts.map +1 -0
  92. package/dist/two-d/ui/RadioGroup.js +177 -0
  93. package/dist/two-d/ui/RadioGroup.js.map +1 -0
  94. package/dist/two-d/ui/TextModel.d.ts +26 -1
  95. package/dist/two-d/ui/TextModel.d.ts.map +1 -1
  96. package/dist/two-d/ui/TextModel.js +56 -7
  97. package/dist/two-d/ui/TextModel.js.map +1 -1
  98. package/dist/two-d/ui/TextSharpness.d.ts +33 -0
  99. package/dist/two-d/ui/TextSharpness.d.ts.map +1 -0
  100. package/dist/two-d/ui/TextSharpness.js +56 -0
  101. package/dist/two-d/ui/TextSharpness.js.map +1 -0
  102. package/dist/two-d/ui/index.d.ts +5 -0
  103. package/dist/two-d/ui/index.d.ts.map +1 -1
  104. package/dist/two-d/ui/index.js +3 -0
  105. package/dist/two-d/ui/index.js.map +1 -1
  106. package/dist/version.d.ts +1 -1
  107. package/dist/version.js +1 -1
  108. package/package.json +14 -2
  109. package/sbom.cdx.json +6 -6
  110. package/tools/build-tablebases.d.mts +22 -0
  111. package/tools/build-tablebases.mjs +549 -0
  112. package/tools/compile-openings.d.mts +20 -0
  113. package/tools/compile-openings.mjs +130 -0
  114. package/tools/play-uci.d.mts +26 -0
  115. package/tools/play-uci.mjs +194 -0
@@ -0,0 +1,26 @@
1
+ import type { ChessMove } from '../src/board/chess.ts';
2
+ import type { OpeningBook } from '../src/board/openings.ts';
3
+ import type { LoadedEnding } from '../src/board/tablebase.ts';
4
+
5
+ export interface UciSessionOptions {
6
+ /** plies when `go` names no depth */
7
+ depth?: number;
8
+ /** compiled opening book, probed before anything thinks */
9
+ book?: OpeningBook | null;
10
+ /** loaded tablebase endings, probed before the search */
11
+ tables?: Record<string, LoadedEnding>;
12
+ }
13
+
14
+ export interface UciSession {
15
+ /** one input line in, zero or more output lines out; never throws */
16
+ input(line: string): string[];
17
+ }
18
+
19
+ /** a move in UCI coordinates (`e2e4`, `e7e8q`). */
20
+ export function uciString(move: ChessMove): string;
21
+
22
+ /**
23
+ * One stateful UCI conversation over the tournament engine: book, then
24
+ * tablebase, then `searchTourney`. Feed it input lines, read back output lines.
25
+ */
26
+ export function createUciSession(options?: UciSessionOptions): UciSession;
@@ -0,0 +1,194 @@
1
+ #!/usr/bin/env node
2
+ import { readFileSync, readdirSync } from 'node:fs';
3
+ import { join, basename, extname } from 'node:path';
4
+ import { createInterface } from 'node:readline';
5
+ import { applyMove, cloneChess, legalMoves, parseFen, squareName, startingChess } from '../src/board/chess.ts';
6
+ import { parseUciMove, probeBook } from '../src/board/openings.ts';
7
+ import { searchTourney } from '../src/board/tourney.ts';
8
+ import { loadTablebaseEnding, probeTablebase } from '../src/board/tablebase.ts';
9
+
10
+ /**
11
+ * A UCI adapter over the tournament engine: `position`/`go` on stdin, `bestmove`
12
+ * on stdout, the way a GUI expects. Move choice runs book first, then tablebase,
13
+ * then `searchTourney`, so each layer only pays when the earlier ones miss.
14
+ *
15
+ * This is a Node tool, never part of the browser bundle: it reads files and
16
+ * stdio, which a `file://` page cannot do. `info score` is centipawns from the
17
+ * side to move's view (documented because GUIs vary); `bestmove 0000` answers
18
+ * a position with no legal moves, per the UCI convention.
19
+ *
20
+ * usage: node tools/play-uci.mjs [--book <book.json>] [--tables <dir>] [--depth N]
21
+ */
22
+
23
+ const PROMOTION_LETTER = { queen: 'q', rook: 'r', bishop: 'b', knight: 'n' };
24
+
25
+ export function uciString(move) {
26
+ return squareName(move.from) + squareName(move.to) + (move.promotion ? PROMOTION_LETTER[move.promotion] : '');
27
+ }
28
+
29
+ /**
30
+ * One stateful conversation: feed it input lines, read back output lines. Pure
31
+ * except for the injected search, book and tables, so tests drive transcripts
32
+ * without spawning anything.
33
+ */
34
+ export function createUciSession(options = {}) {
35
+ const book = options.book ?? null;
36
+ const tables = options.tables ?? {};
37
+ const fallbackDepth = options.depth ?? 4;
38
+ let state = startingChess();
39
+ let halfmove = 0;
40
+
41
+ const findMove = (token) => {
42
+ let parsed;
43
+ try {
44
+ parsed = parseUciMove(token);
45
+ } catch {
46
+ return null;
47
+ }
48
+ return (
49
+ legalMoves(state).find(
50
+ (move) =>
51
+ move.from === parsed.from &&
52
+ move.to === parsed.to &&
53
+ (parsed.promotion === undefined
54
+ ? move.promotion === undefined
55
+ : move.promotion === parsed.promotion),
56
+ ) ?? null
57
+ );
58
+ };
59
+
60
+ const play = (move) => {
61
+ const target = move.to === state.enPassant && state.board[move.from]?.kind === 'pawn';
62
+ const capture = target || state.board[move.to] !== null;
63
+ const pawn = state.board[move.from]?.kind === 'pawn';
64
+ applyMove(state, move);
65
+ halfmove = pawn || capture ? 0 : halfmove + 1;
66
+ };
67
+
68
+ const tablebest = () => {
69
+ const ids = Object.keys(tables);
70
+ if (!ids.length) return null;
71
+ let best = null;
72
+ let complete = true;
73
+ for (const move of legalMoves(state)) {
74
+ const next = cloneChess(state);
75
+ applyMove(next, move);
76
+ const probe = probeTablebase(tables, next, halfmove);
77
+ if (!probe) {
78
+ complete = false;
79
+ continue;
80
+ }
81
+ const score =
82
+ probe.outcome === 'loss' ? 100000 - probe.dtm : probe.outcome === 'draw' ? 0 : -(100000 - probe.dtm);
83
+ if (best === null || score > best.score) best = { move, score };
84
+ }
85
+ //a forced mate beats any unprobed capture line; otherwise every successor
86
+ //must probe, or the search (which sees captures) takes the move instead
87
+ if (best !== null && (best.score > 90000 || complete)) return best;
88
+ return null;
89
+ };
90
+
91
+ const go = (args) => {
92
+ let depth = fallbackDepth;
93
+ let maxNodes;
94
+ let timeMs;
95
+ for (let i = 0; i < args.length; i++) {
96
+ if (args[i] === 'depth' && args[i + 1] !== undefined) depth = Math.max(1, Math.floor(Number(args[i + 1])));
97
+ if (args[i] === 'nodes' && args[i + 1] !== undefined)
98
+ maxNodes = Math.max(1, Math.floor(Number(args[i + 1])));
99
+ if (args[i] === 'movetime' && args[i + 1] !== undefined)
100
+ timeMs = Math.max(1, Math.floor(Number(args[i + 1])));
101
+ }
102
+ if (!Number.isInteger(depth)) depth = fallbackDepth;
103
+ const moves = legalMoves(state);
104
+ if (!moves.length) return ['bestmove 0000'];
105
+ const entry = book ? probeBook(book, state) : null;
106
+ if (entry?.moves.length) return ['info string book', `bestmove ${entry.moves[0]}`];
107
+ const tabled = tablebest();
108
+ if (tabled) return ['info string tablebase', `bestmove ${uciString(tabled.move)}`];
109
+ const result = searchTourney(state, { depth, maxNodes, timeMs });
110
+ if (!result.move) return ['bestmove 0000'];
111
+ return [
112
+ `info depth ${result.depth} score cp ${result.score} nodes ${result.nodes}`,
113
+ `bestmove ${uciString(result.move)}`,
114
+ ];
115
+ };
116
+
117
+ const position = (args) => {
118
+ const movesAt = args.indexOf('moves');
119
+ const head = movesAt === -1 ? args : args.slice(0, movesAt);
120
+ const tail = movesAt === -1 ? [] : args.slice(movesAt + 1);
121
+ if (head[0] === 'startpos') {
122
+ state = startingChess();
123
+ halfmove = 0;
124
+ } else if (head[0] === 'fen') {
125
+ try {
126
+ const fen = head.slice(1).join(' ');
127
+ state = parseFen(fen);
128
+ const parts = fen.split(' ');
129
+ halfmove = parts[4] !== undefined && /^\d+$/.test(parts[4]) ? Number(parts[4]) : 0;
130
+ } catch {
131
+ return [`info string bad fen: ${head.slice(1).join(' ')}`];
132
+ }
133
+ } else {
134
+ return [`info string unknown position: ${head[0] ?? ''}`];
135
+ }
136
+ for (const token of tail) {
137
+ const move = findMove(token);
138
+ if (!move) return [`info string illegal move: ${token}`];
139
+ play(move);
140
+ }
141
+ return [];
142
+ };
143
+
144
+ return {
145
+ input(line) {
146
+ const parts = line
147
+ .trim()
148
+ .split(/\s+/)
149
+ .filter((part) => part.length);
150
+ if (!parts.length) return [];
151
+ const [command, ...args] = parts;
152
+ if (command === 'uci') return ['id name mwg tourney', 'uciok'];
153
+ if (command === 'isready') return ['readyok'];
154
+ if (command === 'ucinewgame') {
155
+ state = startingChess();
156
+ halfmove = 0;
157
+ return [];
158
+ }
159
+ if (command === 'position') return position(args);
160
+ if (command === 'go') return go(args);
161
+ if (command === 'quit') return [];
162
+ return [`info string unknown command: ${command}`];
163
+ },
164
+ };
165
+ }
166
+
167
+ const isMain = process.argv[1] && import.meta.url.endsWith(basename(process.argv[1]));
168
+
169
+ if (isMain) {
170
+ const args = process.argv.slice(2);
171
+ const flag = (name) => {
172
+ const at = args.indexOf(name);
173
+ return at === -1 ? undefined : args[at + 1];
174
+ };
175
+ const bookPath = flag('--book');
176
+ const tablesDir = flag('--tables');
177
+ const depth = flag('--depth') === undefined ? 4 : Math.max(1, Math.floor(Number(flag('--depth'))));
178
+ const book = bookPath ? JSON.parse(readFileSync(bookPath, 'utf8')) : null;
179
+ if (book && book.version !== 1) throw new Error(`${bookPath}: unknown opening-book version`);
180
+ const tables = {};
181
+ if (tablesDir) {
182
+ for (const file of readdirSync(tablesDir)) {
183
+ if (extname(file) !== '.json') continue;
184
+ const ending = loadTablebaseEnding(JSON.parse(readFileSync(join(tablesDir, file), 'utf8')));
185
+ tables[ending.id] = ending;
186
+ }
187
+ }
188
+ const session = createUciSession({ book, tables, depth });
189
+ const lines = createInterface({ input: process.stdin, output: process.stdout, terminal: false });
190
+ for await (const line of lines) {
191
+ if (line.trim() === 'quit') break;
192
+ for (const out of session.input(line)) console.log(out);
193
+ }
194
+ }