feat : add stockfish 16.1

This commit is contained in:
GuillaumeSD
2024-09-30 02:01:07 +02:00
parent 2baf9b76ad
commit 94b7c9d9f7
25 changed files with 110 additions and 47 deletions

6
package-lock.json generated
View File

@@ -4765,9 +4765,9 @@
}
},
"node_modules/micromatch": {
"version": "4.0.7",
"resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.7.tgz",
"integrity": "sha512-LPP/3KorzCwBxfeUuZmaR6bG2kdeHSbe0P2tY3FLRU4vYrjYz5hI4QZwV0njUx3jeuKe67YukQ1LSPZBKDqO/Q==",
"version": "4.0.8",
"resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz",
"integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==",
"dev": true,
"dependencies": {
"braces": "^3.0.3",

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@@ -1,5 +1,7 @@
import { isWasmSupported } from "@/lib/engine/shared";
import { Stockfish11 } from "@/lib/engine/stockfish11";
import { Stockfish16 } from "@/lib/engine/stockfish16";
import { Stockfish16_1 } from "@/lib/engine/stockfish16_1";
import { UciEngine } from "@/lib/engine/uciEngine";
import { EngineName } from "@/types/enums";
import { useEffect, useState } from "react";
@@ -10,7 +12,7 @@ export const useEngine = (engineName: EngineName | undefined) => {
useEffect(() => {
if (!engineName) return;
if (engineName.includes("stockfish_16") && !Stockfish16.isSupported()) {
if (engineName !== EngineName.Stockfish11 && !isWasmSupported()) {
return;
}
@@ -29,13 +31,15 @@ export const useEngine = (engineName: EngineName | undefined) => {
const pickEngine = (engine: EngineName): UciEngine => {
switch (engine) {
case EngineName.Stockfish16_1:
return new Stockfish16_1(false);
case EngineName.Stockfish16_1Lite:
return new Stockfish16_1(true);
case EngineName.Stockfish16:
return new Stockfish16(false);
case EngineName.Stockfish16NNUE:
return new Stockfish16(true);
case EngineName.Stockfish11:
return new Stockfish11();
default:
throw new Error(`Engine ${engine} does not exist ?!`);
}
};

7
src/lib/engine/shared.ts Normal file
View File

@@ -0,0 +1,7 @@
export const isWasmSupported = () =>
typeof WebAssembly === "object" &&
WebAssembly.validate(
Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)
);
export const isMultiThreadSupported = () => SharedArrayBuffer !== undefined;

View File

@@ -1,18 +1,19 @@
import { EngineName } from "@/types/enums";
import { UciEngine } from "./uciEngine";
import { isMultiThreadSupported, isWasmSupported } from "./shared";
export class Stockfish16 extends UciEngine {
constructor(nnue?: boolean) {
if (!Stockfish16.isSupported()) {
if (!isWasmSupported()) {
throw new Error("Stockfish 16 is not supported");
}
const isMultiThreadSupported = Stockfish16.isMultiThreadSupported();
if (!isMultiThreadSupported) console.log("Single thread mode");
const multiThreadIsSupported = isMultiThreadSupported();
if (!multiThreadIsSupported) console.log("Single thread mode");
const enginePath = isMultiThreadSupported
? "engines/stockfish-16-wasm/stockfish-nnue-16.js"
: "engines/stockfish-16-wasm/stockfish-nnue-16-single.js";
const enginePath = multiThreadIsSupported
? "engines/stockfish-16/stockfish-nnue-16.js"
: "engines/stockfish-16/stockfish-nnue-16-single.js";
const customEngineInit = async () => {
await this.sendCommands(
@@ -23,17 +24,4 @@ export class Stockfish16 extends UciEngine {
super(EngineName.Stockfish16, enginePath, customEngineInit);
}
public static isSupported() {
return (
typeof WebAssembly === "object" &&
WebAssembly.validate(
Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)
)
);
}
public static isMultiThreadSupported() {
return SharedArrayBuffer !== undefined;
}
}

View File

@@ -0,0 +1,23 @@
import { EngineName } from "@/types/enums";
import { UciEngine } from "./uciEngine";
import { isMultiThreadSupported, isWasmSupported } from "./shared";
export class Stockfish16_1 extends UciEngine {
constructor(lite?: boolean) {
if (!isWasmSupported()) {
throw new Error("Stockfish 16.1 is not supported");
}
const multiThreadIsSupported = isMultiThreadSupported();
if (!multiThreadIsSupported) console.log("Single thread mode");
const enginePath = `engines/stockfish-16.1/stockfish-16.1${
lite ? "-lite" : ""
}${multiThreadIsSupported ? "" : "-single"}.js`;
super(
lite ? EngineName.Stockfish16_1Lite : EngineName.Stockfish16_1,
enginePath
);
}
}

View File

@@ -12,7 +12,7 @@ export const boardOrientationAtom = atom(true);
export const showBestMoveArrowAtom = atom(true);
export const showPlayerMoveIconAtom = atom(true);
export const engineNameAtom = atom<EngineName>(EngineName.Stockfish16);
export const engineNameAtom = atom<EngineName>(EngineName.Stockfish16_1Lite);
export const engineDepthAtom = atom(16);
export const engineMultiPvAtom = atom(3);
export const evaluationProgressAtom = atom(0);

View File

@@ -21,8 +21,8 @@ import {
} from "../analysis/states";
import ArrowOptions from "./arrowOptions";
import { useAtomLocalStorage } from "@/hooks/useAtomLocalStorage";
import { Stockfish16 } from "@/lib/engine/stockfish16";
import { useEffect } from "react";
import { isWasmSupported } from "@/lib/engine/shared";
interface Props {
open: boolean;
@@ -44,7 +44,7 @@ export default function EngineSettingsDialog({ open, onClose }: Props) {
);
useEffect(() => {
if (!Stockfish16.isSupported()) {
if (!isWasmSupported()) {
setEngineName(EngineName.Stockfish11);
}
}, [setEngineName]);
@@ -56,9 +56,9 @@ export default function EngineSettingsDialog({ open, onClose }: Props) {
</DialogTitle>
<DialogContent sx={{ paddingBottom: 0 }}>
<Typography>
Stockfish 16 Lite (HCE) is the default engine. It offers the best
balance between speed and strength. Stockfish 16 is the strongest
engine available, note that it requires a one time download of 40MB.
Stockfish 16.1 Lite is the default engine. It offers the best balance
between speed and strength. Stockfish 16.1 is the strongest engine
available, note that it requires a one time download of 64MB.
</Typography>
<Grid
marginTop={4}
@@ -86,9 +86,7 @@ export default function EngineSettingsDialog({ open, onClose }: Props) {
key={engine}
value={engine}
disabled={
engine.includes("stockfish_16")
? !Stockfish16.isSupported()
: false
engine !== EngineName.Stockfish11 && !isWasmSupported()
}
>
{engineLabel[engine]}
@@ -129,7 +127,9 @@ export default function EngineSettingsDialog({ open, onClose }: Props) {
}
const engineLabel: Record<EngineName, string> = {
[EngineName.Stockfish16_1]: "Stockfish 16.1 (64MB)",
[EngineName.Stockfish16_1Lite]: "Stockfish 16.1 Lite (6MB)",
[EngineName.Stockfish16NNUE]: "Stockfish 16 (40MB)",
[EngineName.Stockfish16]: "Stockfish 16 Lite (HCE)",
[EngineName.Stockfish16NNUE]: "Stockfish 16 (40MB download)",
[EngineName.Stockfish11]: "Stockfish 11",
};

View File

@@ -29,8 +29,8 @@ import {
import { useChessActions } from "@/hooks/useChessActions";
import { playGameStartSound } from "@/lib/sounds";
import { logAnalyticsEvent } from "@/lib/firebase";
import { Stockfish16 } from "@/lib/engine/stockfish16";
import { useEffect } from "react";
import { isWasmSupported } from "@/lib/engine/shared";
interface Props {
open: boolean;
@@ -69,7 +69,7 @@ export default function GameSettingsDialog({ open, onClose }: Props) {
};
useEffect(() => {
if (!Stockfish16.isSupported()) {
if (!isWasmSupported()) {
setEngineName(EngineName.Stockfish11);
}
}, [setEngineName]);
@@ -81,10 +81,9 @@ export default function GameSettingsDialog({ open, onClose }: Props) {
</DialogTitle>
<DialogContent sx={{ paddingBottom: 0 }}>
<Typography>
Stockfish 16 Lite (HCE) is the default engine. It offers the best
balance between speed and strength. Stockfish 16 is the strongest
engine available, but please note that it requires a one time download
of 40MB.
Stockfish 16.1 Lite is the default engine. It offers the best balance
between speed and strength. Stockfish 16.1 is the strongest engine
available, note that it requires a one time download of 64MB.
</Typography>
<Grid
marginTop={4}
@@ -112,9 +111,7 @@ export default function GameSettingsDialog({ open, onClose }: Props) {
key={engine}
value={engine}
disabled={
engine.includes("stockfish_16")
? !Stockfish16.isSupported()
: false
engine !== EngineName.Stockfish11 && !isWasmSupported()
}
>
{engineLabel[engine]}
@@ -168,7 +165,9 @@ export default function GameSettingsDialog({ open, onClose }: Props) {
}
const engineLabel: Record<EngineName, string> = {
[EngineName.Stockfish16_1]: "Stockfish 16.1 (64MB)",
[EngineName.Stockfish16_1Lite]: "Stockfish 16.1 Lite (6MB)",
[EngineName.Stockfish16NNUE]: "Stockfish 16 (40MB)",
[EngineName.Stockfish16]: "Stockfish 16 Lite (HCE)",
[EngineName.Stockfish16NNUE]: "Stockfish 16 (40MB download)",
[EngineName.Stockfish11]: "Stockfish 11",
};

View File

@@ -6,6 +6,8 @@ import { atom } from "jotai";
export const gameAtom = atom(new Chess());
export const gameDataAtom = atom<CurrentPosition>({});
export const playerColorAtom = atom<Color>(Color.White);
export const enginePlayNameAtom = atom<EngineName>(EngineName.Stockfish16);
export const enginePlayNameAtom = atom<EngineName>(
EngineName.Stockfish16_1Lite
);
export const engineSkillLevelAtom = atom<number>(1);
export const isGameInProgressAtom = atom(false);

View File

@@ -5,8 +5,10 @@ export enum GameOrigin {
}
export enum EngineName {
Stockfish16 = "stockfish_16",
Stockfish16_1 = "stockfish_16_1",
Stockfish16_1Lite = "stockfish_16_1_lite",
Stockfish16NNUE = "stockfish_16_nnue",
Stockfish16 = "stockfish_16",
Stockfish11 = "stockfish_11",
}