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 * Enhancements Copyright 1995, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
10 * ------------------------------------------------------------------------
12 * GNU XBoard is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or (at
15 * your option) any later version.
17 * GNU XBoard is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see http://www.gnu.org/licenses/. *
25 *------------------------------------------------------------------------
26 ** See the file ChangeLog for a revision history. */
28 #define SHOW_PONDERING
37 #else /* not STDC_HEADERS */
40 # else /* not HAVE_STRING_H */
42 # endif /* not HAVE_STRING_H */
43 #endif /* not STDC_HEADERS */
49 #include "engineoutput.h"
53 # define _(s) gettext (s)
54 # define N_(s) gettext_noop (s)
59 # define ngettext(s,p,n) T_(p)
80 // called by other front-end
81 void EngineOutputUpdate( FrontEndProgramStats * stats );
82 void OutputKibitz(int window, char *text);
84 // module back-end routines
85 static void VerifyDisplayMode();
86 static void UpdateControls( EngineOutputData * ed );
88 static int lastDepth[2] = { -1, -1 };
89 static int lastForwardMostMove[2] = { -1, -1 };
90 static int engineState[2] = { -1, -1 };
91 static char lastLine[2][MSG_SIZ];
92 static char header[2][MSG_SIZ];
93 static char columnHeader[MSG_SIZ] = "dep\tscore\tnodes\ttime\n";
94 static int columnMask;
97 static int scores[MAX_VAR], textEnd[MAX_VAR], keys[MAX_VAR], curDepth[2], nrVariations[2];
99 extern int initialRulePlies;
102 MakeEngineOutputTitle ()
104 static char buf[MSG_SIZ];
105 static char oldTitle[MSG_SIZ];
107 int count, rule = 2*appData.ruleMoves;
109 snprintf(title, MSG_SIZ, _("Engine Output") );
111 if(!EngineOutputIsUp()) return;
112 // figure out value of 50-move counter
114 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove ) count--;
115 if( count == backwardMostMove ) count -= initialRulePlies;
116 count = currentMove - count;
117 if(!rule) rule = 100;
118 if(count >= rule - 40 && (!appData.icsActive || gameMode == IcsObserving || appData.zippyPlay)) {
119 snprintf(buf, MSG_SIZ, ngettext("%s (%d reversible ply)", "%s (%d reversible plies)", count), title, count);
120 safeStrCpy(title, buf, MSG_SIZ);
122 if(!strcmp(oldTitle, title)) return;
123 safeStrCpy(oldTitle, title, MSG_SIZ);
124 SetEngineOutputTitle(title);
127 // back end, due to front-end wrapper for SetWindowText, and new SetIcon arguments
129 SetEngineState (int which, enum ENGINE_STATE state, char * state_data)
131 int x_which = 1 - which;
133 if( engineState[ which ] != state ) {
134 engineState[ which ] = state;
138 SetIcon( which, nStateIcon, nThinking );
139 if( engineState[ x_which ] == STATE_THINKING ) {
140 SetEngineState( x_which, STATE_IDLE, "" );
143 case STATE_PONDERING:
144 SetIcon( which, nStateIcon, nPondering );
146 case STATE_ANALYZING:
147 SetIcon( which, nStateIcon, nAnalyzing );
150 SetIcon( which, nStateIcon, nClear );
155 if( state_data != 0 ) {
156 DoSetWindowText( which, nStateData, state_data );
160 // back end, now the front-end wrapper ClearMemo is used, and ed no longer contains handles.
162 SetProgramStats (FrontEndProgramStats * stats) // now directly called by back-end
165 int clearMemo = FALSE;
166 int which, depth, multi;
172 SetEngineState( 0, STATE_IDLE, "" );
173 SetEngineState( 1, STATE_IDLE, "" );
177 if(gameMode == IcsObserving && !appData.icsEngineAnalyze)
178 return; // [HGM] kibitz: shut up engine if we are observing an ICS game
180 which = stats->which;
181 depth = stats->depth;
183 if( which < 0 || which > 1 || depth < 0 || stats->time < 0 || stats->pv == 0 ) {
187 if( !EngineOutputDialogExists() ) {
195 ed.nodes = stats->nodes;
196 ed.score = stats->score;
197 ed.time = stats->time;
199 ed.hint = stats->hint;
200 ed.an_move_index = stats->an_move_index;
201 ed.an_move_count = stats->an_move_count;
203 /* Get target control. [HGM] this is moved to front end, which get them from a table */
205 ed.name = first.tidy;
208 ed.name = second.tidy;
211 if( ed.pv != 0 && ed.pv[0] == ' ' ) {
212 if( strncmp( ed.pv, " no PV", 6 ) == 0 ) { /* Hack on hack! :-O */
217 /* Clear memo if needed */
218 if( lastDepth[which] > depth || (lastDepth[which] == depth && depth <= 1 && ed.pv[0]) ) { // no reason to clear if we won't add line
222 if( lastForwardMostMove[which] != forwardMostMove ) {
227 if(!appData.headers) columnHeader[0] = NULLCHAR;
228 DoClearMemo(which); nrVariations[which] = 0;
229 header[which][0] = NULLCHAR;
230 if(gameMode == AnalyzeMode) {
231 ChessProgramState *cps = (which ? &second : &first);
232 if((multi = MultiPV(cps)) >= 0) {
233 snprintf(header[which], MSG_SIZ, "\t%s viewpoint\t\tfewer / Multi-PV setting = %d / more\n",
234 appData.whitePOV || appData.scoreWhite ? "white" : "mover", cps->option[multi].value);
236 if(!which) snprintf(header[which]+strlen(header[which]), MSG_SIZ-strlen(header[which]), "%s%s", exclusionHeader, columnHeader);
237 InsertIntoMemo( which, header[which], 0);
239 snprintf(header[which], MSG_SIZ, "%s", columnHeader);
240 if(appData.ponderNextMove && lastLine[which][0]) {
241 InsertIntoMemo( which, lastLine[which], 0 );
242 InsertIntoMemo( which, "\n", 0 );
244 InsertIntoMemo( which, header[which], 0);
248 if(ed.pv && ed.pv[0] && ParseOneMove(ed.pv, currentMove, &moveType, &ff, &rf, &ft, &rt, &pc))
249 ed.moveKey = (ff<<24 | rf << 16 | ft << 8 | rt) ^ pc*87161;
250 else ed.moveKey = ed.nodes; // kludge to get unique key unlikely to match any move
253 lastDepth[which] = depth == 1 && ed.nodes == 0 ? 0 : depth; // [HGM] info-line kudge
254 lastForwardMostMove[which] = forwardMostMove;
256 UpdateControls( &ed );
259 #define ENGINE_COLOR_WHITE 'w'
260 #define ENGINE_COLOR_BLACK 'b'
261 #define ENGINE_COLOR_UNKNOWN ' '
265 GetEngineColor (int which)
267 char result = ENGINE_COLOR_UNKNOWN;
269 if( which == 0 || which == 1 ) {
270 ChessProgramState * cps;
273 case MachinePlaysBlack:
274 case IcsPlayingBlack:
275 result = ENGINE_COLOR_BLACK;
277 case MachinePlaysWhite:
278 case IcsPlayingWhite:
279 result = ENGINE_COLOR_WHITE;
283 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
285 case TwoMachinesPlay:
286 cps = (which == 0) ? &first : &second;
287 result = cps->twoMachinesColor[0];
288 result = result == 'w' ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
290 default: ; // does not happen, but suppresses pedantic warnings
299 GetActiveEngineColor ()
301 char result = ENGINE_COLOR_UNKNOWN;
303 if( gameMode == TwoMachinesPlay ) {
304 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
312 IsEnginePondering (int which)
317 case MachinePlaysBlack:
318 case IcsPlayingBlack:
319 if( WhiteOnMove(forwardMostMove) ) result = TRUE;
321 case MachinePlaysWhite:
322 case IcsPlayingWhite:
323 if( ! WhiteOnMove(forwardMostMove) ) result = TRUE;
325 case TwoMachinesPlay:
326 if( GetActiveEngineColor() != ENGINE_COLOR_UNKNOWN ) {
327 if( GetEngineColor( which ) != GetActiveEngineColor() ) result = TRUE;
330 default: ; // does not happen, but suppresses pedantic warnings
338 SetDisplayMode (int mode)
340 if( windowMode != mode ) {
343 ResizeWindowControls( mode );
353 /* Get proper mode for current game */
355 case IcsObserving: // [HGM] ICS analyze
356 if(!appData.icsEngineAnalyze) return;
358 case MachinePlaysWhite:
359 case MachinePlaysBlack:
363 mode = second.analyzing;
365 case IcsPlayingWhite:
366 case IcsPlayingBlack:
367 mode = appData.zippyPlay && opponentKibitzes; // [HGM] kibitz
369 case TwoMachinesPlay:
377 SetDisplayMode( mode );
380 // back end. Determine what icon to set in the color-icon field, and print it
382 SetEngineColorIcon (int which)
384 char color = GetEngineColor(which);
387 if( color == ENGINE_COLOR_BLACK )
389 else if( color == ENGINE_COLOR_WHITE )
392 nicon = nColorUnknown;
394 SetIcon( which, nColorIcon, nicon );
397 #define MAX_NAME_LENGTH 32
399 // [HGM] multivar: sort Thinking Output within one depth on score
402 InsertionPoint (int len, EngineOutputData *ed)
404 int i, offs = 0, newScore = ed->score, n = ed->which;
406 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
407 newScore = 1e6; // info lines inserted on top
408 if(ed->depth != curDepth[n]) { // depth has changed
409 curDepth[n] = ed->depth;
410 nrVariations[n] = 0; // throw away everything we had
412 // loop through all lines. Note even / odd used for different panes
413 for(i=nrVariations[n]-2; i>=0; i-=2) {
414 // put new item behind those we haven't looked at
416 textEnd[i+n+2] = offs + len;
417 scores[i+n+2] = newScore;
418 keys[i+n+2] = ed->moveKey;
419 if(ed->moveKey != keys[i+n] && // same move always tops previous one (as a higher score must be a fail low)
420 newScore < scores[i+n]) break;
421 // if it had higher score as previous, move previous in stead
422 scores[i+n+2] = ed->moveKey == keys[i+n] ? newScore : scores[i+n]; // correct scores of fail-low/high searches
423 textEnd[i+n+2] = textEnd[i+n] + len;
424 keys[i+n+2] = keys[i+n];
428 textEnd[n] = offs + len;
429 scores[n] = newScore;
431 nrVariations[n] += 2;
432 return offs + strlen(header[ed->which]);
436 // pure back end, now SetWindowText is called via wrapper DoSetWindowText
438 UpdateControls (EngineOutputData *ed)
440 // int isPondering = FALSE;
442 char s_label[MAX_NAME_LENGTH + 32];
444 char * name = ed->name;
447 if( name == 0 || *name == '\0' ) {
451 strncpy( s_label, name, MAX_NAME_LENGTH );
452 s_label[ MAX_NAME_LENGTH-1 ] = '\0';
454 #ifdef SHOW_PONDERING
455 if( IsEnginePondering( ed->which ) ) {
460 if( ed->hint != 0 && *ed->hint != '\0' ) {
461 strncpy( buf, ed->hint, sizeof(buf) );
462 buf[sizeof(buf)-1] = '\0';
464 else if( ed->pv != 0 && *ed->pv != '\0' ) {
465 char * sep = strchr( ed->pv, ' ' );
466 int buflen = sizeof(buf);
469 buflen = sep - ed->pv + 1;
470 if( buflen > sizeof(buf) ) buflen = sizeof(buf);
473 strncpy( buf, ed->pv, buflen );
474 buf[ buflen-1 ] = '\0';
477 SetEngineState( ed->which, STATE_PONDERING, buf );
479 else if( gameMode == TwoMachinesPlay ) {
480 SetEngineState( ed->which, STATE_THINKING, "" );
482 else if( gameMode == AnalyzeMode || gameMode == AnalyzeFile
483 || (gameMode == IcsObserving && appData.icsEngineAnalyze)) { // [HGM] ICS-analyze
485 int time_secs = ed->time / 100;
486 int time_mins = time_secs / 60;
490 if( ed->an_move_index != 0 && ed->an_move_count != 0 && *ed->hint != '\0' ) {
493 strncpy( mov, ed->hint, sizeof(mov) );
494 mov[ sizeof(mov)-1 ] = '\0';
496 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "[%d] %d/%d: %s [%02d:%02d:%02d]", ed->depth, ed->an_move_index,
497 ed->an_move_count, mov, time_mins / 60, time_mins % 60, time_secs % 60 );
500 SetEngineState( ed->which, STATE_ANALYZING, buf );
503 SetEngineState( ed->which, STATE_IDLE, "" );
507 DoSetWindowText( ed->which, nLabel, s_label );
511 if( ed->time > 0 && ed->nodes > 0 ) {
512 unsigned long nps_100 = ed->nodes / ed->time;
514 if( nps_100 < 100000 ) {
515 snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "%s: %lu", _("NPS"), nps_100 * 100 );
518 snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "%s: %.1fk", _("NPS"), nps_100 / 10.0 );
522 DoSetWindowText( ed->which, nLabelNPS, s_label );
525 if( ed->pv != 0 && *ed->pv != '\0' ) {
526 static char spaces[] = " "; // [HGM] align: spaces for padding
532 int time_secs = ed->time / 100;
533 int time_cent = ed->time % 100;
536 if( ed->nodes < 1000000 ) {
537 int h = ed->nodes, i=0;
538 while(h > 0) h /= 10, i++; // [HGM] align: count digits; pad with 2 spaces for every missing digit
539 snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), u64Display "%s\t", ed->nodes, spaces + 2*i);
542 snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), "%.1fM%s\t", u64ToDouble(ed->nodes) / 1000000.0,
543 spaces + 8 + 2*(ed->nodes > 1e7));
547 h = ((gameMode == AnalyzeMode && appData.whitePOV || appData.scoreWhite) && !WhiteOnMove(currentMove) ? -1 : 1) * ed->score;
549 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), " 0.00\t" );
552 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "+%.2f\t", h / 100.0 );
555 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), " %.2f\t", h / 100.0 );
559 snprintf( s_time, sizeof(s_time)/sizeof(s_time[0]), "%d:%02d.%02d\t", time_secs / 60, time_secs % 60, time_cent );
561 if(columnMask & 2) s_score[0] = NULLCHAR; // [HGM] hide: erase columns the user has hidden
562 if(columnMask & 4) s_nodes[0] = NULLCHAR;
563 if(columnMask & 8) s_time[0] = NULLCHAR;
565 /* Put all together... */
566 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
567 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t", ed->depth );
569 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t%s%s%s", ed->depth, s_score, s_nodes, s_time );
572 buflen = strlen(buf);
574 strncpy( buf + buflen, ed->pv, sizeof(buf) - buflen );
576 buf[ sizeof(buf) - 3 ] = '\0';
578 strcat( buf + buflen, "\r\n" );
581 InsertIntoMemo( ed->which, buf, InsertionPoint(strlen(buf), ed) );
582 strncpy(lastLine[ed->which], buf, MSG_SIZ);
586 SetEngineColorIcon( ed->which );
589 static char *titles[] = { "score\t", "nodes\t", "time\t" };
593 { // handle click on column headers, to hide / show them
594 int i, j, nr=0, m=~columnMask, Ncol=4;
595 for(i=0; columnHeader[i] && i<n; i++) nr += (columnHeader[i] == '\t');
596 if(!nr) return; // depth always shown, so clicks on it ignored
597 for(i=j=0; i<Ncol; i++) if(m & 1<<i) j++; // count hidden columns
598 if(nr < j) { // shown column clicked: hide it
599 for(i=j=0; i<Ncol; i++) if(m & 1<<i && j++ == nr) break;
601 } else { // hidden column clicked: show it
603 for(i=j=0; i<Ncol; i++) if(m & 1<<i && j++ == nr) break;
604 columnMask &= ~(1<<i);
606 // create new header line
607 strcpy(columnHeader, "dep\t");
609 for(i=j=1; i<Ncol; i++) if(m & 1<<i) strcat(columnHeader, titles[i-1]), j++;
610 if(j != Ncol) { // list hidden columns, so user ca click them
611 m = ~m; strcat(columnHeader, "(not shown: ");
612 for(i=1; i<Ncol; i++) if(m & 1<<i) strcat(columnHeader, titles[i-1]);
613 strcat(columnHeader, ")");
615 strcat(columnHeader, "\n");
618 // [HGM] kibitz: write kibitz line; split window for it if necessary
620 OutputKibitz (int window, char *text)
622 static int currentLineEnd[2];
624 if(!EngineOutputIsUp()) return;
625 if(!opponentKibitzes) { // on first kibitz of game, clear memos
626 DoClearMemo(1); currentLineEnd[1] = 0;
627 if(gameMode == IcsObserving) { DoClearMemo(0); currentLineEnd[0] = 0; }
629 opponentKibitzes = TRUE; // this causes split window DisplayMode in ICS modes.
631 strncpy(text+strlen(text)-1, "\r\n",sizeof(text+strlen(text)-1)); // to not lose line breaks on copying
632 if(gameMode == IcsObserving) {
633 DoSetWindowText(0, nLabel, gameInfo.white);
634 SetIcon( 0, nColorIcon, nColorWhite);
635 SetIcon( 0, nStateIcon, nClear);
637 DoSetWindowText(1, nLabel, gameMode == IcsPlayingBlack ? gameInfo.white : gameInfo.black); // opponent name
638 SetIcon( 1, nColorIcon, gameMode == IcsPlayingBlack ? nColorWhite : nColorBlack);
639 SetIcon( 1, nStateIcon, nClear);
640 if(strstr(text, "\\ ") == text) where = currentLineEnd[window-1]; // continuation line
641 //if(appData.debugMode) fprintf(debugFP, "insert '%s' at %d (end = %d,%d)\n", text, where, currentLineEnd[0], currentLineEnd[1]);
642 InsertIntoMemo(window-1, text, where); // [HGM] multivar: always at top
643 currentLineEnd[window-1] = where + strlen(text);