Skip to content

Commit

Permalink
use ucinewgame instead of destroying ceval between puzzles (fixes #16067
Browse files Browse the repository at this point in the history
)
  • Loading branch information
niklasf committed Oct 5, 2024
1 parent 86232af commit 66701f0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 46 deletions.
2 changes: 1 addition & 1 deletion ui/analyse/src/ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ export default class AnalyseCtrl {
startCeval = throttle(800, () => {
if (this.ceval?.enabled()) {
if (this.canUseCeval()) {
this.ceval.start(this.path, this.nodeList, this.threatMode());
this.ceval.start(this.path, this.nodeList, undefined, this.threatMode());
this.evalCache.fetch(this.path, this.ceval.search.multiPv);
} else this.ceval.stop();
}
Expand Down
22 changes: 12 additions & 10 deletions ui/ceval/src/ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,22 @@ export default class CevalCtrl {
}
});

private doStart = (path: Tree.Path, steps: Step[], threatMode: boolean) => {
private doStart = (path: Tree.Path, steps: Step[], gameId: string | undefined, threatMode: boolean) => {
if (!this.enabled() || !this.possible || !enabledAfterDisable()) return;
const step = steps[steps.length - 1];
if (
!this.isDeeper() &&
'movetime' in this.search.by &&
((threatMode ? step.threat : step.ceval)?.millis ?? 0) >= this.search.by.movetime
) {
this.lastStarted = { path, steps, threatMode };
this.lastStarted = { path, steps, gameId, threatMode };
return;
}
const work: Work = {
variant: this.opts.variant.key,
threads: this.threads,
hashSize: this.hashSize,
gameId,
stopRequested: false,
initialFen: steps[0].fen,
moves: [],
Expand Down Expand Up @@ -143,23 +144,29 @@ export default class CevalCtrl {
this.lastStarted = {
path,
steps,
gameId,
threatMode,
};
};

goDeeper = (): void => {
if (!this.lastStarted) return;
this.isDeeper(true);
this.doStart(this.lastStarted.path, this.lastStarted.steps, this.lastStarted.threatMode);
this.doStart(
this.lastStarted.path,
this.lastStarted.steps,
this.lastStarted.gameId,
this.lastStarted.threatMode,
);
};

stop = (): void => {
this.worker?.stop();
};

start = (path: string, steps: Step[], threatMode?: boolean): void => {
start = (path: string, steps: Step[], gameId: string | undefined, threatMode?: boolean): void => {
this.isDeeper(false);
this.doStart(path, steps, !!threatMode);
this.doStart(path, steps, gameId, !!threatMode);
};

get state(): CevalState {
Expand Down Expand Up @@ -269,11 +276,6 @@ export default class CevalCtrl {
}
};

destroy = (): void => {
this.worker?.destroy();
this.worker = undefined;
};

engineFailed(msg: string): void {
if (msg.includes('Blocking on the main thread')) return; // mostly harmless
showEngineError(this.engines.active?.name ?? 'Engine', msg);
Expand Down
4 changes: 4 additions & 0 deletions ui/ceval/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class Protocol {

private work: Work | undefined;
private currentEval: Tree.LocalEval | undefined;
private gameId: string | undefined;
private expectedPvs = 1;

private nextWork: Work | undefined;
Expand Down Expand Up @@ -170,6 +171,9 @@ export class Protocol {
this.setOption('Hash', this.work.hashSize || 16);
this.setOption('MultiPV', Math.max(1, this.work.multiPv));

if (this.gameId && this.gameId != this.work.gameId) this.send('ucinewgame');
this.gameId = this.work.gameId;

this.send(['position fen', this.work.initialFen, 'moves', ...this.work.moves].join(' '));
const [by, value] = Object.entries(this.work.search)[0];
this.send(`go ${by} ${value}`);
Expand Down
2 changes: 2 additions & 0 deletions ui/ceval/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Work {
variant: VariantKey;
threads: number;
hashSize: number | undefined;
gameId: string | undefined; // send ucinewgame when changed
stopRequested: boolean;

path: string;
Expand Down Expand Up @@ -107,6 +108,7 @@ export interface PvBoard {
export interface Started {
path: string;
steps: Step[];
gameId: string | undefined;
threatMode: boolean;
}

Expand Down
67 changes: 32 additions & 35 deletions ui/puzzle/src/ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,35 @@ export default class PuzzleCtrl implements ParentCtrl {
redraw,
);

this.ceval = new CevalCtrl({
redraw: this.redraw,
variant: {
short: 'Std',
name: 'Standard',
key: 'standard',
},
externalEngines:
this.data.externalEngines?.map(engine => ({
...engine,
endpoint: this.opts.externalEngineEndpoint,
})) || [],
initialFen: undefined, // always standard starting position
possible: true,
emit: (ev, work) => {
this.tree.updateAt(work.path, node => {
if (work.threatMode) {
const threat = ev;
if (!node.threat || node.threat.depth <= threat.depth) node.threat = threat;
} else if (!node.ceval || node.ceval.depth <= ev.depth) node.ceval = ev;
if (work.path === this.path) {
this.setAutoShapes();
this.redraw();
}
});
},
setAutoShapes: this.setAutoShapes,
});

this.keyboardHelp = propWithEffect(location.hash === '#keyboard', this.redraw);
keyboard(this);

Expand Down Expand Up @@ -210,8 +239,6 @@ export default class PuzzleCtrl implements ParentCtrl {
g.setShapes([]);
this.showGround(g);
});

this.instanciateCeval();
};

position = (): Chess => {
Expand Down Expand Up @@ -456,38 +483,6 @@ export default class PuzzleCtrl implements ParentCtrl {
}
};

instanciateCeval = (): void => {
this.ceval?.destroy();
this.ceval = new CevalCtrl({
redraw: this.redraw,
variant: {
short: 'Std',
name: 'Standard',
key: 'standard',
},
externalEngines:
this.data.externalEngines?.map(engine => ({
...engine,
endpoint: this.opts.externalEngineEndpoint,
})) || [],
initialFen: undefined, // always standard starting position
possible: true,
emit: (ev, work) => {
this.tree.updateAt(work.path, node => {
if (work.threatMode) {
const threat = ev;
if (!node.threat || node.threat.depth <= threat.depth) node.threat = threat;
} else if (!node.ceval || node.ceval.depth <= ev.depth) node.ceval = ev;
if (work.path === this.path) {
this.setAutoShapes();
this.redraw();
}
});
},
setAutoShapes: this.setAutoShapes,
});
};

setAutoShapes = (): void =>
this.withGround(g =>
g.setAutoShapes(
Expand All @@ -505,7 +500,9 @@ export default class PuzzleCtrl implements ParentCtrl {
if (this.ceval.enabled() && this.canUseCeval()) this.doStartCeval();
};

private doStartCeval = throttle(800, () => this.ceval.start(this.path, this.nodeList, this.threatMode()));
private doStartCeval = throttle(800, () =>
this.ceval.start(this.path, this.nodeList, this.data.puzzle.id, this.threatMode()),
);

nextNodeBest = () => treeOps.withMainlineChild(this.node, n => n.eval?.best);

Expand Down

0 comments on commit 66701f0

Please sign in to comment.