@intrastellar/lozza 0.1.0 → 0.2.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 (4) hide show
  1. package/README.md +7 -0
  2. package/index.js +67 -10
  3. package/package.json +2 -1
  4. package/worker.js +4932 -0
package/README.md CHANGED
@@ -4,3 +4,10 @@ Browser-compatible Lozza chess engine for userscripts and Node.js.
4
4
 
5
5
  The package exposes `getBestMoveFromFen(fen, options)` through CommonJS and
6
6
  also publishes `globalThis.DLPLozza` when loaded as a browser script.
7
+
8
+ `getBestMoveFromFenPersistent(fen, options)` keeps Lozza's transposition table
9
+ between searches. The userscript worker build uses a 16 MB hash setting and
10
+ keeps the search off the page's main thread.
11
+
12
+ For userscripts, load `worker.js` with `@require`. It publishes
13
+ `globalThis.DLPLozzaWorkerURL`, which can be passed to a browser `Worker`.
package/index.js CHANGED
@@ -4769,7 +4769,7 @@ function captureUciOutput (commands) {
4769
4769
 
4770
4770
  }
4771
4771
 
4772
- function getBestMoveFromFen (fen, options) {
4772
+ function normalizeSearchRequest (fen, options) {
4773
4773
 
4774
4774
  options = options || {};
4775
4775
 
@@ -4797,13 +4797,11 @@ function getBestMoveFromFen (fen, options) {
4797
4797
  if (sanitizedMoves.length > 0)
4798
4798
  positionCommand += ' moves ' + sanitizedMoves.join(' ');
4799
4799
 
4800
- const output = captureUciOutput(
4801
- 'setoption name hash value ' + hash + '\n' +
4802
- 'setoption name multipv value 1\n' +
4803
- 'ucinewgame\n' +
4804
- positionCommand + '\n' +
4805
- 'go depth ' + depth
4806
- );
4800
+ return { depth, hash, positionCommand };
4801
+
4802
+ }
4803
+
4804
+ function parseBestMoveOutput (output, depth) {
4807
4805
 
4808
4806
  const bestMoveLine = output.find(line => line.startsWith('bestmove '));
4809
4807
 
@@ -4822,13 +4820,72 @@ function getBestMoveFromFen (fen, options) {
4822
4820
 
4823
4821
  }
4824
4822
 
4823
+ function getBestMoveFromFen (fen, options) {
4824
+
4825
+ const request = normalizeSearchRequest(fen, options);
4826
+
4827
+ const output = captureUciOutput(
4828
+ 'setoption name hash value ' + request.hash + '\n' +
4829
+ 'setoption name multipv value 1\n' +
4830
+ 'ucinewgame\n' +
4831
+ request.positionCommand + '\n' +
4832
+ 'go depth ' + request.depth
4833
+ );
4834
+
4835
+ return parseBestMoveOutput(output, request.depth);
4836
+
4837
+ }
4838
+
4839
+ let persistentEngineHash = null;
4840
+ let persistentEngineInitialized = false;
4841
+
4842
+ function initializePersistentEngine (hash) {
4843
+
4844
+ hash = Math.max(parseInt(hash || ttDefault, 10) || ttDefault, 1);
4845
+
4846
+ captureUciOutput(
4847
+ 'setoption name hash value ' + hash + '\n' +
4848
+ 'setoption name multipv value 1\n' +
4849
+ 'ucinewgame'
4850
+ );
4851
+
4852
+ persistentEngineHash = hash;
4853
+ persistentEngineInitialized = true;
4854
+
4855
+ return { hash };
4856
+
4857
+ }
4858
+
4859
+ function getBestMoveFromFenPersistent (fen, options) {
4860
+
4861
+ options = options || {};
4862
+ const requestedHash = Math.max(parseInt(options.hash || ttDefault, 10) || ttDefault, 1);
4863
+
4864
+ if (options.newGame || !persistentEngineInitialized || persistentEngineHash !== requestedHash)
4865
+ initializePersistentEngine(requestedHash);
4866
+
4867
+ const request = normalizeSearchRequest(fen, options);
4868
+ const output = captureUciOutput(
4869
+ request.positionCommand + '\n' +
4870
+ 'go depth ' + request.depth
4871
+ );
4872
+
4873
+ return parseBestMoveOutput(output, request.depth);
4874
+ }
4875
+
4825
4876
  if (nodeHost) {
4826
4877
  module.exports = {
4827
- getBestMoveFromFen: getBestMoveFromFen
4878
+ getBestMoveFromFen: getBestMoveFromFen,
4879
+ getBestMoveFromFenPersistent: getBestMoveFromFenPersistent,
4880
+ initializePersistentEngine: initializePersistentEngine
4828
4881
  };
4829
4882
  }
4830
4883
 
4831
4884
  if (typeof globalThis !== 'undefined') {
4832
- globalThis.DLPLozza = { getBestMoveFromFen };
4885
+ globalThis.DLPLozza = {
4886
+ getBestMoveFromFen,
4887
+ getBestMoveFromFenPersistent,
4888
+ initializePersistentEngine
4889
+ };
4833
4890
  }
4834
4891
 
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@intrastellar/lozza",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Browser-compatible Lozza chess engine for userscripts and Node.js",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
7
7
  "files": [
8
8
  "index.js",
9
+ "worker.js",
9
10
  "README.md"
10
11
  ],
11
12
  "keywords": [