2 * engineoutput.c - split-off backe-end from Engine output (PV) by HGM
4 * Author: Alessandro Scotti (Dec 2005)
6 * Copyright 2005 Alessandro Scotti
8 * ------------------------------------------------------------------------
10 * GNU XBoard is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or (at
13 * your option) any later version.
15 * GNU XBoard is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see http://www.gnu.org/licenses/. *
23 *------------------------------------------------------------------------
24 ** See the file ChangeLog for a revision history. */
26 #define SHOW_PONDERING
35 #else /* not STDC_HEADERS */
38 # else /* not HAVE_STRING_H */
40 # endif /* not HAVE_STRING_H */
41 #endif /* not STDC_HEADERS */
47 #include "engineoutput.h"
62 // called by other front-end
63 void EngineOutputUpdate( FrontEndProgramStats * stats );
64 void OutputKibitz(int window, char *text);
66 // module back-end routines
67 static void VerifyDisplayMode();
68 static void UpdateControls( EngineOutputData * ed );
70 static int lastDepth[2] = { -1, -1 };
71 static int lastForwardMostMove[2] = { -1, -1 };
72 static int engineState[2] = { -1, -1 };
73 static char lastLine[2][MSG_SIZ];
74 static char header[MSG_SIZ];
77 static int scores[MAX_VAR], textEnd[MAX_VAR], curDepth[2], nrVariations[2];
79 extern int initialRulePlies;
81 void MakeEngineOutputTitle()
83 static char buf[MSG_SIZ];
84 static char oldTitle[MSG_SIZ];
85 char *title = "Engine Output";
86 int count, rule = 2*appData.ruleMoves;
87 // figure out value of 50-move counter
89 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove ) count--;
90 if( count == backwardMostMove ) count -= initialRulePlies;
91 count = currentMove - count;
92 snprintf(buf, MSG_SIZ, "%s (%d reversible plies)", title, count);
94 if(count >= rule - 40 && !appData.icsActive) title = buf;
95 if(!strcmp(oldTitle, title)) return;
96 safeStrCpy(oldTitle, title, MSG_SIZ);
97 SetEngineOutputTitle(title);
100 // back end, due to front-end wrapper for SetWindowText, and new SetIcon arguments
101 void SetEngineState( int which, int state, char * state_data )
103 int x_which = 1 - which;
105 if( engineState[ which ] != state ) {
106 engineState[ which ] = state;
110 SetIcon( which, nStateIcon, nThinking );
111 if( engineState[ x_which ] == STATE_THINKING ) {
112 SetEngineState( x_which, STATE_IDLE, "" );
115 case STATE_PONDERING:
116 SetIcon( which, nStateIcon, nPondering );
118 case STATE_ANALYZING:
119 SetIcon( which, nStateIcon, nAnalyzing );
122 SetIcon( which, nStateIcon, nClear );
127 if( state_data != 0 ) {
128 DoSetWindowText( which, nStateData, state_data );
132 // back end, now the front-end wrapper ClearMemo is used, and ed no longer contains handles.
133 void SetProgramStats( FrontEndProgramStats * stats ) // now directly called by back-end
136 int clearMemo = FALSE;
137 int which, depth, multi;
140 SetEngineState( 0, STATE_IDLE, "" );
141 SetEngineState( 1, STATE_IDLE, "" );
145 if(gameMode == IcsObserving && !appData.icsEngineAnalyze)
146 return; // [HGM] kibitz: shut up engine if we are observing an ICS game
148 which = stats->which;
149 depth = stats->depth;
151 if( which < 0 || which > 1 || depth < 0 || stats->time < 0 || stats->pv == 0 ) {
155 if( !EngineOutputDialogExists() ) {
163 ed.nodes = stats->nodes;
164 ed.score = stats->score;
165 ed.time = stats->time;
167 ed.hint = stats->hint;
168 ed.an_move_index = stats->an_move_index;
169 ed.an_move_count = stats->an_move_count;
171 /* Get target control. [HGM] this is moved to front end, which get them from a table */
173 ed.name = first.tidy;
176 ed.name = second.tidy;
179 if( ed.pv != 0 && ed.pv[0] == ' ' ) {
180 if( strncmp( ed.pv, " no PV", 6 ) == 0 ) { /* Hack on hack! :-O */
185 /* Clear memo if needed */
186 if( lastDepth[which] > depth || (lastDepth[which] == depth && depth <= 1 && ed.pv[0]) ) { // no reason to clear if we won't add line
190 if( lastForwardMostMove[which] != forwardMostMove ) {
195 DoClearMemo(which); nrVariations[which] = 0;
196 header[0] = NULLCHAR;
197 if(gameMode == AnalyzeMode && (multi = MultiPV(&first)) >= 0) {
198 snprintf(header, MSG_SIZ, "\t%s viewpoint\t\tfewer / Multi-PV setting = %d / more\n",
199 appData.whitePOV || appData.scoreWhite ? "white" : "mover", first.option[multi].value);
200 InsertIntoMemo( which, header, 0);
202 if(appData.ponderNextMove && lastLine[which][0]) {
203 InsertIntoMemo( which, lastLine[which], 0 );
204 InsertIntoMemo( which, "\n", 0 );
209 lastDepth[which] = depth == 1 && ed.nodes == 0 ? 0 : depth; // [HGM] info-line kudge
210 lastForwardMostMove[which] = forwardMostMove;
212 UpdateControls( &ed );
215 #define ENGINE_COLOR_WHITE 'w'
216 #define ENGINE_COLOR_BLACK 'b'
217 #define ENGINE_COLOR_UNKNOWN ' '
220 static char GetEngineColor( int which )
222 char result = ENGINE_COLOR_UNKNOWN;
224 if( which == 0 || which == 1 ) {
225 ChessProgramState * cps;
228 case MachinePlaysBlack:
229 case IcsPlayingBlack:
230 result = ENGINE_COLOR_BLACK;
232 case MachinePlaysWhite:
233 case IcsPlayingWhite:
234 result = ENGINE_COLOR_WHITE;
238 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
240 case TwoMachinesPlay:
241 cps = (which == 0) ? &first : &second;
242 result = cps->twoMachinesColor[0];
243 result = result == 'w' ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
245 default: ; // does not happen, but suppresses pedantic warnings
253 static char GetActiveEngineColor()
255 char result = ENGINE_COLOR_UNKNOWN;
257 if( gameMode == TwoMachinesPlay ) {
258 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
265 static int IsEnginePondering( int which )
270 case MachinePlaysBlack:
271 case IcsPlayingBlack:
272 if( WhiteOnMove(forwardMostMove) ) result = TRUE;
274 case MachinePlaysWhite:
275 case IcsPlayingWhite:
276 if( ! WhiteOnMove(forwardMostMove) ) result = TRUE;
278 case TwoMachinesPlay:
279 if( GetActiveEngineColor() != ENGINE_COLOR_UNKNOWN ) {
280 if( GetEngineColor( which ) != GetActiveEngineColor() ) result = TRUE;
283 default: ; // does not happen, but suppresses pedantic warnings
290 static void SetDisplayMode( int mode )
292 if( windowMode != mode ) {
295 ResizeWindowControls( mode );
300 static void VerifyDisplayMode()
304 /* Get proper mode for current game */
306 case IcsObserving: // [HGM] ICS analyze
307 if(!appData.icsEngineAnalyze) return;
310 case MachinePlaysWhite:
311 case MachinePlaysBlack:
314 case IcsPlayingWhite:
315 case IcsPlayingBlack:
316 mode = appData.zippyPlay && opponentKibitzes; // [HGM] kibitz
318 case TwoMachinesPlay:
326 SetDisplayMode( mode );
329 // back end. Determine what icon to set in the color-icon field, and print it
330 void SetEngineColorIcon( int which )
332 char color = GetEngineColor(which);
335 if( color == ENGINE_COLOR_BLACK )
337 else if( color == ENGINE_COLOR_WHITE )
340 nicon = nColorUnknown;
342 SetIcon( which, nColorIcon, nicon );
345 #define MAX_NAME_LENGTH 32
347 // [HGM] multivar: sort Thinking Output within one depth on score
349 static int InsertionPoint( int len, EngineOutputData * ed )
351 int i, offs = 0, newScore = ed->score, n = ed->which;
353 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
354 newScore = 1e6; // info lines inserted on top
355 if(ed->depth != curDepth[n]) { // depth has changed
356 curDepth[n] = ed->depth;
357 nrVariations[n] = 0; // throw away everything we had
359 // loop through all lines. Note even / odd used for different panes
360 for(i=nrVariations[n]-2; i>=0; i-=2) {
361 // put new item behind those we haven't looked at
363 textEnd[i+n+2] = offs + len;
364 scores[i+n+2] = newScore;
365 if(newScore < scores[i+n]) break;
366 // if it had higher score as previous, move previous in stead
367 scores[i+n+2] = scores[i+n];
368 textEnd[i+n+2] = textEnd[i+n] + len;
372 textEnd[n] = offs + len;
373 scores[n] = newScore;
375 nrVariations[n] += 2;
376 return offs + (gameMode == AnalyzeMode)*strlen(header);
380 // pure back end, now SetWindowText is called via wrapper DoSetWindowText
381 static void UpdateControls( EngineOutputData * ed )
383 // int isPondering = FALSE;
385 char s_label[MAX_NAME_LENGTH + 32];
387 char * name = ed->name;
390 if( name == 0 || *name == '\0' ) {
394 strncpy( s_label, name, MAX_NAME_LENGTH );
395 s_label[ MAX_NAME_LENGTH-1 ] = '\0';
397 #ifdef SHOW_PONDERING
398 if( IsEnginePondering( ed->which ) ) {
403 if( ed->hint != 0 && *ed->hint != '\0' ) {
404 strncpy( buf, ed->hint, sizeof(buf) );
405 buf[sizeof(buf)-1] = '\0';
407 else if( ed->pv != 0 && *ed->pv != '\0' ) {
408 char * sep = strchr( ed->pv, ' ' );
409 int buflen = sizeof(buf);
412 buflen = sep - ed->pv + 1;
413 if( buflen > sizeof(buf) ) buflen = sizeof(buf);
416 strncpy( buf, ed->pv, buflen );
417 buf[ buflen-1 ] = '\0';
420 SetEngineState( ed->which, STATE_PONDERING, buf );
422 else if( gameMode == TwoMachinesPlay ) {
423 SetEngineState( ed->which, STATE_THINKING, "" );
425 else if( gameMode == AnalyzeMode || gameMode == AnalyzeFile
426 || (gameMode == IcsObserving && appData.icsEngineAnalyze)) { // [HGM] ICS-analyze
428 int time_secs = ed->time / 100;
429 int time_mins = time_secs / 60;
433 if( ed->an_move_index != 0 && ed->an_move_count != 0 && *ed->hint != '\0' ) {
436 strncpy( mov, ed->hint, sizeof(mov) );
437 mov[ sizeof(mov)-1 ] = '\0';
439 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "[%d] %d/%d: %s [%02d:%02d:%02d]", ed->depth, ed->an_move_index,
440 ed->an_move_count, mov, time_mins / 60, time_mins % 60, time_secs % 60 );
443 SetEngineState( ed->which, STATE_ANALYZING, buf );
446 SetEngineState( ed->which, STATE_IDLE, "" );
450 DoSetWindowText( ed->which, nLabel, s_label );
454 if( ed->time > 0 && ed->nodes > 0 ) {
455 unsigned long nps_100 = ed->nodes / ed->time;
457 if( nps_100 < 100000 ) {
458 snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "NPS: %lu", nps_100 * 100 );
461 snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "NPS: %.1fk", nps_100 / 10.0 );
465 DoSetWindowText( ed->which, nLabelNPS, s_label );
468 if( ed->pv != 0 && *ed->pv != '\0' ) {
474 int time_secs = ed->time / 100;
475 int time_cent = ed->time % 100;
478 if( ed->nodes < 1000000 ) {
479 snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), u64Display, ed->nodes );
482 snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), "%.1fM", u64ToDouble(ed->nodes) / 1000000.0 );
486 h = ((gameMode == AnalyzeMode && appData.whitePOV || appData.scoreWhite) && !WhiteOnMove(currentMove) ? -1 : 1) * ed->score;
488 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "+%.2f", h / 100.0 );
491 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "%.2f", h / 100.0 );
495 snprintf( s_time, sizeof(s_time)/sizeof(s_time[0]), "%d:%02d.%02d", time_secs / 60, time_secs % 60, time_cent );
497 /* Put all together... */
498 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
499 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t", ed->depth );
501 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t%s\t%s\t%s\t", ed->depth, s_score, s_nodes, s_time );
504 buflen = strlen(buf);
506 strncpy( buf + buflen, ed->pv, sizeof(buf) - buflen );
508 buf[ sizeof(buf) - 3 ] = '\0';
510 strcat( buf + buflen, "\r\n" );
513 InsertIntoMemo( ed->which, buf, InsertionPoint(strlen(buf), ed) );
514 strncpy(lastLine[ed->which], buf, MSG_SIZ);
518 SetEngineColorIcon( ed->which );
521 // [HGM] kibitz: write kibitz line; split window for it if necessary
522 void OutputKibitz(int window, char *text)
524 static int currentLineEnd[2];
526 if(!EngineOutputIsUp()) return;
527 if(!opponentKibitzes) { // on first kibitz of game, clear memos
528 DoClearMemo(1); currentLineEnd[1] = 0;
529 if(gameMode == IcsObserving) { DoClearMemo(0); currentLineEnd[0] = 0; }
531 opponentKibitzes = TRUE; // this causes split window DisplayMode in ICS modes.
533 strncpy(text+strlen(text)-1, "\r\n",sizeof(text+strlen(text)-1)); // to not lose line breaks on copying
534 if(gameMode == IcsObserving) {
535 DoSetWindowText(0, nLabel, gameInfo.white);
536 SetIcon( 0, nColorIcon, nColorWhite);
537 SetIcon( 0, nStateIcon, nClear);
539 DoSetWindowText(1, nLabel, gameMode == IcsPlayingBlack ? gameInfo.white : gameInfo.black); // opponent name
540 SetIcon( 1, nColorIcon, gameMode == IcsPlayingBlack ? nColorWhite : nColorBlack);
541 SetIcon( 1, nStateIcon, nClear);
542 if(strstr(text, "\\ ") == text) where = currentLineEnd[window-1]; // continuation line
543 //if(appData.debugMode) fprintf(debugFP, "insert '%s' at %d (end = %d,%d)\n", text, where, currentLineEnd[0], currentLineEnd[1]);
544 InsertIntoMemo(window-1, text, where); // [HGM] multivar: always at top
545 currentLineEnd[window-1] = where + strlen(text);