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 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)
79 // called by other front-end
80 void EngineOutputUpdate( FrontEndProgramStats * stats );
81 void OutputKibitz(int window, char *text);
83 // module back-end routines
84 static void VerifyDisplayMode();
85 static void UpdateControls( EngineOutputData * ed );
87 static int lastDepth[2] = { -1, -1 };
88 static int lastForwardMostMove[2] = { -1, -1 };
89 static int engineState[2] = { -1, -1 };
90 static char lastLine[2][MSG_SIZ];
91 static char header[MSG_SIZ];
94 static int scores[MAX_VAR], textEnd[MAX_VAR], curDepth[2], nrVariations[2];
96 extern int initialRulePlies;
99 MakeEngineOutputTitle ()
101 static char buf[MSG_SIZ];
102 static char oldTitle[MSG_SIZ];
104 int count, rule = 2*appData.ruleMoves;
106 snprintf(title, MSG_SIZ, _("Engine Output") );
108 if(!EngineOutputIsUp()) return;
109 // figure out value of 50-move counter
111 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove ) count--;
112 if( count == backwardMostMove ) count -= initialRulePlies;
113 count = currentMove - count;
114 if(!rule) rule = 100;
115 if(count >= rule - 40 && (!appData.icsActive || gameMode == IcsObserving)) {
116 snprintf(buf, MSG_SIZ, ngettext("%s (%d reversible ply)", "%s (%d reversible plies)", count), title, count);
117 safeStrCpy(title, buf, MSG_SIZ);
119 if(!strcmp(oldTitle, title)) return;
120 safeStrCpy(oldTitle, title, MSG_SIZ);
121 SetEngineOutputTitle(title);
124 // back end, due to front-end wrapper for SetWindowText, and new SetIcon arguments
126 SetEngineState (int which, int state, char * state_data)
128 int x_which = 1 - which;
130 if( engineState[ which ] != state ) {
131 engineState[ which ] = state;
135 SetIcon( which, nStateIcon, nThinking );
136 if( engineState[ x_which ] == STATE_THINKING ) {
137 SetEngineState( x_which, STATE_IDLE, "" );
140 case STATE_PONDERING:
141 SetIcon( which, nStateIcon, nPondering );
143 case STATE_ANALYZING:
144 SetIcon( which, nStateIcon, nAnalyzing );
147 SetIcon( which, nStateIcon, nClear );
152 if( state_data != 0 ) {
153 DoSetWindowText( which, nStateData, state_data );
157 // back end, now the front-end wrapper ClearMemo is used, and ed no longer contains handles.
159 SetProgramStats (FrontEndProgramStats * stats) // now directly called by back-end
162 int clearMemo = FALSE;
163 int which, depth, multi;
166 SetEngineState( 0, STATE_IDLE, "" );
167 SetEngineState( 1, STATE_IDLE, "" );
171 if(gameMode == IcsObserving && !appData.icsEngineAnalyze)
172 return; // [HGM] kibitz: shut up engine if we are observing an ICS game
174 which = stats->which;
175 depth = stats->depth;
177 if( which < 0 || which > 1 || depth < 0 || stats->time < 0 || stats->pv == 0 ) {
181 if( !EngineOutputDialogExists() ) {
189 ed.nodes = stats->nodes;
190 ed.score = stats->score;
191 ed.time = stats->time;
193 ed.hint = stats->hint;
194 ed.an_move_index = stats->an_move_index;
195 ed.an_move_count = stats->an_move_count;
197 /* Get target control. [HGM] this is moved to front end, which get them from a table */
199 ed.name = first.tidy;
202 ed.name = second.tidy;
205 if( ed.pv != 0 && ed.pv[0] == ' ' ) {
206 if( strncmp( ed.pv, " no PV", 6 ) == 0 ) { /* Hack on hack! :-O */
211 /* Clear memo if needed */
212 if( lastDepth[which] > depth || (lastDepth[which] == depth && depth <= 1 && ed.pv[0]) ) { // no reason to clear if we won't add line
216 if( lastForwardMostMove[which] != forwardMostMove ) {
221 DoClearMemo(which); nrVariations[which] = 0;
222 header[0] = NULLCHAR;
223 if(gameMode == AnalyzeMode) {
224 if((multi = MultiPV(&first)) >= 0) {
225 snprintf(header, MSG_SIZ, "\t%s viewpoint\t\tfewer / Multi-PV setting = %d / more\n",
226 appData.whitePOV || appData.scoreWhite ? "white" : "mover", first.option[multi].value);
228 snprintf(header+strlen(header), MSG_SIZ-strlen(header), exclusionHeader);
229 InsertIntoMemo( which, header, 0);
231 if(appData.ponderNextMove && lastLine[which][0]) {
232 InsertIntoMemo( which, lastLine[which], 0 );
233 InsertIntoMemo( which, "\n", 0 );
238 lastDepth[which] = depth == 1 && ed.nodes == 0 ? 0 : depth; // [HGM] info-line kudge
239 lastForwardMostMove[which] = forwardMostMove;
241 UpdateControls( &ed );
244 #define ENGINE_COLOR_WHITE 'w'
245 #define ENGINE_COLOR_BLACK 'b'
246 #define ENGINE_COLOR_UNKNOWN ' '
250 GetEngineColor (int which)
252 char result = ENGINE_COLOR_UNKNOWN;
254 if( which == 0 || which == 1 ) {
255 ChessProgramState * cps;
258 case MachinePlaysBlack:
259 case IcsPlayingBlack:
260 result = ENGINE_COLOR_BLACK;
262 case MachinePlaysWhite:
263 case IcsPlayingWhite:
264 result = ENGINE_COLOR_WHITE;
268 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
270 case TwoMachinesPlay:
271 cps = (which == 0) ? &first : &second;
272 result = cps->twoMachinesColor[0];
273 result = result == 'w' ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
275 default: ; // does not happen, but suppresses pedantic warnings
284 GetActiveEngineColor ()
286 char result = ENGINE_COLOR_UNKNOWN;
288 if( gameMode == TwoMachinesPlay ) {
289 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
297 IsEnginePondering (int which)
302 case MachinePlaysBlack:
303 case IcsPlayingBlack:
304 if( WhiteOnMove(forwardMostMove) ) result = TRUE;
306 case MachinePlaysWhite:
307 case IcsPlayingWhite:
308 if( ! WhiteOnMove(forwardMostMove) ) result = TRUE;
310 case TwoMachinesPlay:
311 if( GetActiveEngineColor() != ENGINE_COLOR_UNKNOWN ) {
312 if( GetEngineColor( which ) != GetActiveEngineColor() ) result = TRUE;
315 default: ; // does not happen, but suppresses pedantic warnings
323 SetDisplayMode (int mode)
325 if( windowMode != mode ) {
328 ResizeWindowControls( mode );
338 /* Get proper mode for current game */
340 case IcsObserving: // [HGM] ICS analyze
341 if(!appData.icsEngineAnalyze) return;
344 case MachinePlaysWhite:
345 case MachinePlaysBlack:
348 case IcsPlayingWhite:
349 case IcsPlayingBlack:
350 mode = appData.zippyPlay && opponentKibitzes; // [HGM] kibitz
352 case TwoMachinesPlay:
360 SetDisplayMode( mode );
363 // back end. Determine what icon to set in the color-icon field, and print it
365 SetEngineColorIcon (int which)
367 char color = GetEngineColor(which);
370 if( color == ENGINE_COLOR_BLACK )
372 else if( color == ENGINE_COLOR_WHITE )
375 nicon = nColorUnknown;
377 SetIcon( which, nColorIcon, nicon );
380 #define MAX_NAME_LENGTH 32
382 // [HGM] multivar: sort Thinking Output within one depth on score
385 InsertionPoint (int len, EngineOutputData *ed)
387 int i, offs = 0, newScore = ed->score, n = ed->which;
389 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
390 newScore = 1e6; // info lines inserted on top
391 if(ed->depth != curDepth[n]) { // depth has changed
392 curDepth[n] = ed->depth;
393 nrVariations[n] = 0; // throw away everything we had
395 // loop through all lines. Note even / odd used for different panes
396 for(i=nrVariations[n]-2; i>=0; i-=2) {
397 // put new item behind those we haven't looked at
399 textEnd[i+n+2] = offs + len;
400 scores[i+n+2] = newScore;
401 if(newScore < scores[i+n]) break;
402 // if it had higher score as previous, move previous in stead
403 scores[i+n+2] = scores[i+n];
404 textEnd[i+n+2] = textEnd[i+n] + len;
408 textEnd[n] = offs + len;
409 scores[n] = newScore;
411 nrVariations[n] += 2;
412 return offs + (gameMode == AnalyzeMode)*strlen(header);
416 // pure back end, now SetWindowText is called via wrapper DoSetWindowText
418 UpdateControls (EngineOutputData *ed)
420 // int isPondering = FALSE;
422 char s_label[MAX_NAME_LENGTH + 32];
424 char * name = ed->name;
427 if( name == 0 || *name == '\0' ) {
431 strncpy( s_label, name, MAX_NAME_LENGTH );
432 s_label[ MAX_NAME_LENGTH-1 ] = '\0';
434 #ifdef SHOW_PONDERING
435 if( IsEnginePondering( ed->which ) ) {
440 if( ed->hint != 0 && *ed->hint != '\0' ) {
441 strncpy( buf, ed->hint, sizeof(buf) );
442 buf[sizeof(buf)-1] = '\0';
444 else if( ed->pv != 0 && *ed->pv != '\0' ) {
445 char * sep = strchr( ed->pv, ' ' );
446 int buflen = sizeof(buf);
449 buflen = sep - ed->pv + 1;
450 if( buflen > sizeof(buf) ) buflen = sizeof(buf);
453 strncpy( buf, ed->pv, buflen );
454 buf[ buflen-1 ] = '\0';
457 SetEngineState( ed->which, STATE_PONDERING, buf );
459 else if( gameMode == TwoMachinesPlay ) {
460 SetEngineState( ed->which, STATE_THINKING, "" );
462 else if( gameMode == AnalyzeMode || gameMode == AnalyzeFile
463 || (gameMode == IcsObserving && appData.icsEngineAnalyze)) { // [HGM] ICS-analyze
465 int time_secs = ed->time / 100;
466 int time_mins = time_secs / 60;
470 if( ed->an_move_index != 0 && ed->an_move_count != 0 && *ed->hint != '\0' ) {
473 strncpy( mov, ed->hint, sizeof(mov) );
474 mov[ sizeof(mov)-1 ] = '\0';
476 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "[%d] %d/%d: %s [%02d:%02d:%02d]", ed->depth, ed->an_move_index,
477 ed->an_move_count, mov, time_mins / 60, time_mins % 60, time_secs % 60 );
480 SetEngineState( ed->which, STATE_ANALYZING, buf );
483 SetEngineState( ed->which, STATE_IDLE, "" );
487 DoSetWindowText( ed->which, nLabel, s_label );
491 if( ed->time > 0 && ed->nodes > 0 ) {
492 unsigned long nps_100 = ed->nodes / ed->time;
494 if( nps_100 < 100000 ) {
495 snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "NPS: %lu", nps_100 * 100 );
498 snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "NPS: %.1fk", nps_100 / 10.0 );
502 DoSetWindowText( ed->which, nLabelNPS, s_label );
505 if( ed->pv != 0 && *ed->pv != '\0' ) {
511 int time_secs = ed->time / 100;
512 int time_cent = ed->time % 100;
515 if( ed->nodes < 1000000 ) {
516 snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), u64Display, ed->nodes );
519 snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), "%.1fM", u64ToDouble(ed->nodes) / 1000000.0 );
523 h = ((gameMode == AnalyzeMode && appData.whitePOV || appData.scoreWhite) && !WhiteOnMove(currentMove) ? -1 : 1) * ed->score;
525 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "+%.2f", h / 100.0 );
528 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "%.2f", h / 100.0 );
532 snprintf( s_time, sizeof(s_time)/sizeof(s_time[0]), "%d:%02d.%02d", time_secs / 60, time_secs % 60, time_cent );
534 /* Put all together... */
535 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
536 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t", ed->depth );
538 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t%s\t%s\t%s\t", ed->depth, s_score, s_nodes, s_time );
541 buflen = strlen(buf);
543 strncpy( buf + buflen, ed->pv, sizeof(buf) - buflen );
545 buf[ sizeof(buf) - 3 ] = '\0';
547 strcat( buf + buflen, "\r\n" );
550 InsertIntoMemo( ed->which, buf, InsertionPoint(strlen(buf), ed) );
551 strncpy(lastLine[ed->which], buf, MSG_SIZ);
555 SetEngineColorIcon( ed->which );
558 // [HGM] kibitz: write kibitz line; split window for it if necessary
560 OutputKibitz (int window, char *text)
562 static int currentLineEnd[2];
564 if(!EngineOutputIsUp()) return;
565 if(!opponentKibitzes) { // on first kibitz of game, clear memos
566 DoClearMemo(1); currentLineEnd[1] = 0;
567 if(gameMode == IcsObserving) { DoClearMemo(0); currentLineEnd[0] = 0; }
569 opponentKibitzes = TRUE; // this causes split window DisplayMode in ICS modes.
571 strncpy(text+strlen(text)-1, "\r\n",sizeof(text+strlen(text)-1)); // to not lose line breaks on copying
572 if(gameMode == IcsObserving) {
573 DoSetWindowText(0, nLabel, gameInfo.white);
574 SetIcon( 0, nColorIcon, nColorWhite);
575 SetIcon( 0, nStateIcon, nClear);
577 DoSetWindowText(1, nLabel, gameMode == IcsPlayingBlack ? gameInfo.white : gameInfo.black); // opponent name
578 SetIcon( 1, nColorIcon, gameMode == IcsPlayingBlack ? nColorWhite : nColorBlack);
579 SetIcon( 1, nStateIcon, nClear);
580 if(strstr(text, "\\ ") == text) where = currentLineEnd[window-1]; // continuation line
581 //if(appData.debugMode) fprintf(debugFP, "insert '%s' at %d (end = %d,%d)\n", text, where, currentLineEnd[0], currentLineEnd[1]);
582 InsertIntoMemo(window-1, text, where); // [HGM] multivar: always at top
583 currentLineEnd[window-1] = where + strlen(text);