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 void MakeEngineOutputTitle()
81 static char buf[MSG_SIZ];
82 static char oldTitle[MSG_SIZ];
83 char *title = "Engine Output";
84 extern int initialRulePlies;
86 // figure out value of 50-move counter
88 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove ) count--;
89 if( count == backwardMostMove ) count -= initialRulePlies;
90 count += 2*appData.ruleMoves - currentMove;
91 snprintf(buf, MSG_SIZ, "%s (%d ply to draw)", title, count);
92 if(count <= 40) title = buf;
93 if(!strcmp(oldTitle, title)) return;
94 safeStrCpy(oldTitle, title, MSG_SIZ);
95 SetEngineOutputTitle(title);
98 // back end, due to front-end wrapper for SetWindowText, and new SetIcon arguments
99 void SetEngineState( int which, int state, char * state_data )
101 int x_which = 1 - which;
103 if( engineState[ which ] != state ) {
104 engineState[ which ] = state;
108 SetIcon( which, nStateIcon, nThinking );
109 if( engineState[ x_which ] == STATE_THINKING ) {
110 SetEngineState( x_which, STATE_IDLE, "" );
113 case STATE_PONDERING:
114 SetIcon( which, nStateIcon, nPondering );
116 case STATE_ANALYZING:
117 SetIcon( which, nStateIcon, nAnalyzing );
120 SetIcon( which, nStateIcon, nClear );
125 if( state_data != 0 ) {
126 DoSetWindowText( which, nStateData, state_data );
130 // back end, now the front-end wrapper ClearMemo is used, and ed no longer contains handles.
131 void SetProgramStats( FrontEndProgramStats * stats ) // now directly called by back-end
134 int clearMemo = FALSE;
135 int which, depth, multi;
138 SetEngineState( 0, STATE_IDLE, "" );
139 SetEngineState( 1, STATE_IDLE, "" );
143 if(gameMode == IcsObserving && !appData.icsEngineAnalyze)
144 return; // [HGM] kibitz: shut up engine if we are observing an ICS game
146 which = stats->which;
147 depth = stats->depth;
149 if( which < 0 || which > 1 || depth < 0 || stats->time < 0 || stats->pv == 0 ) {
153 if( !EngineOutputDialogExists() ) {
161 ed.nodes = stats->nodes;
162 ed.score = stats->score;
163 ed.time = stats->time;
165 ed.hint = stats->hint;
166 ed.an_move_index = stats->an_move_index;
167 ed.an_move_count = stats->an_move_count;
169 /* Get target control. [HGM] this is moved to front end, which get them from a table */
171 ed.name = first.tidy;
174 ed.name = second.tidy;
177 if( ed.pv != 0 && ed.pv[0] == ' ' ) {
178 if( strncmp( ed.pv, " no PV", 6 ) == 0 ) { /* Hack on hack! :-O */
183 /* Clear memo if needed */
184 if( lastDepth[which] > depth || (lastDepth[which] == depth && depth <= 1 && ed.pv[0]) ) { // no reason to clear if we won't add line
188 if( lastForwardMostMove[which] != forwardMostMove ) {
193 DoClearMemo(which); nrVariations[which] = 0;
194 header[0] = NULLCHAR;
195 if(gameMode == AnalyzeMode && (multi = MultiPV(&first)) >= 0) {
196 snprintf(header, MSG_SIZ, "\t%s viewpoint\t\tfewer / Multi-PV setting = %d / more\n",
197 appData.whitePOV ? "white" : "mover", first.option[multi].value);
198 InsertIntoMemo( which, header, 0);
200 if(appData.ponderNextMove && lastLine[which][0]) {
201 InsertIntoMemo( which, lastLine[which], 0 );
202 InsertIntoMemo( which, "\n", 0 );
207 lastDepth[which] = depth == 1 && ed.nodes == 0 ? 0 : depth; // [HGM] info-line kudge
208 lastForwardMostMove[which] = forwardMostMove;
210 UpdateControls( &ed );
213 #define ENGINE_COLOR_WHITE 'w'
214 #define ENGINE_COLOR_BLACK 'b'
215 #define ENGINE_COLOR_UNKNOWN ' '
218 static char GetEngineColor( int which )
220 char result = ENGINE_COLOR_UNKNOWN;
222 if( which == 0 || which == 1 ) {
223 ChessProgramState * cps;
226 case MachinePlaysBlack:
227 case IcsPlayingBlack:
228 result = ENGINE_COLOR_BLACK;
230 case MachinePlaysWhite:
231 case IcsPlayingWhite:
232 result = ENGINE_COLOR_WHITE;
236 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
238 case TwoMachinesPlay:
239 cps = (which == 0) ? &first : &second;
240 result = cps->twoMachinesColor[0];
241 result = result == 'w' ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
243 default: ; // does not happen, but suppresses pedantic warnings
251 static char GetActiveEngineColor()
253 char result = ENGINE_COLOR_UNKNOWN;
255 if( gameMode == TwoMachinesPlay ) {
256 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
263 static int IsEnginePondering( int which )
268 case MachinePlaysBlack:
269 case IcsPlayingBlack:
270 if( WhiteOnMove(forwardMostMove) ) result = TRUE;
272 case MachinePlaysWhite:
273 case IcsPlayingWhite:
274 if( ! WhiteOnMove(forwardMostMove) ) result = TRUE;
276 case TwoMachinesPlay:
277 if( GetActiveEngineColor() != ENGINE_COLOR_UNKNOWN ) {
278 if( GetEngineColor( which ) != GetActiveEngineColor() ) result = TRUE;
281 default: ; // does not happen, but suppresses pedantic warnings
288 static void SetDisplayMode( int mode )
290 if( windowMode != mode ) {
293 ResizeWindowControls( mode );
298 static void VerifyDisplayMode()
302 /* Get proper mode for current game */
304 case IcsObserving: // [HGM] ICS analyze
305 if(!appData.icsEngineAnalyze) return;
308 case MachinePlaysWhite:
309 case MachinePlaysBlack:
312 case IcsPlayingWhite:
313 case IcsPlayingBlack:
314 mode = appData.zippyPlay && opponentKibitzes; // [HGM] kibitz
316 case TwoMachinesPlay:
324 SetDisplayMode( mode );
327 // back end. Determine what icon to set in the color-icon field, and print it
328 void SetEngineColorIcon( int which )
330 char color = GetEngineColor(which);
333 if( color == ENGINE_COLOR_BLACK )
335 else if( color == ENGINE_COLOR_WHITE )
338 nicon = nColorUnknown;
340 SetIcon( which, nColorIcon, nicon );
343 #define MAX_NAME_LENGTH 32
345 // [HGM] multivar: sort Thinking Output within one depth on score
347 static int InsertionPoint( int len, EngineOutputData * ed )
349 int i, offs = 0, newScore = ed->score, n = ed->which;
351 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
352 newScore = 1e6; // info lines inserted on top
353 if(ed->depth != curDepth[n]) { // depth has changed
354 curDepth[n] = ed->depth;
355 nrVariations[n] = 0; // throw away everything we had
357 // loop through all lines. Note even / odd used for different panes
358 for(i=nrVariations[n]-2; i>=0; i-=2) {
359 // put new item behind those we haven't looked at
361 textEnd[i+n+2] = offs + len;
362 scores[i+n+2] = newScore;
363 if(newScore < scores[i+n]) break;
364 // if it had higher score as previous, move previous in stead
365 scores[i+n+2] = scores[i+n];
366 textEnd[i+n+2] = textEnd[i+n] + len;
370 textEnd[n] = offs + len;
371 scores[n] = newScore;
373 nrVariations[n] += 2;
374 return offs + (gameMode == AnalyzeMode)*strlen(header);
378 // pure back end, now SetWindowText is called via wrapper DoSetWindowText
379 static void UpdateControls( EngineOutputData * ed )
381 // int isPondering = FALSE;
383 char s_label[MAX_NAME_LENGTH + 32];
385 char * name = ed->name;
388 if( name == 0 || *name == '\0' ) {
392 strncpy( s_label, name, MAX_NAME_LENGTH );
393 s_label[ MAX_NAME_LENGTH-1 ] = '\0';
395 #ifdef SHOW_PONDERING
396 if( IsEnginePondering( ed->which ) ) {
401 if( ed->hint != 0 && *ed->hint != '\0' ) {
402 strncpy( buf, ed->hint, sizeof(buf) );
403 buf[sizeof(buf)-1] = '\0';
405 else if( ed->pv != 0 && *ed->pv != '\0' ) {
406 char * sep = strchr( ed->pv, ' ' );
407 int buflen = sizeof(buf);
410 buflen = sep - ed->pv + 1;
411 if( buflen > sizeof(buf) ) buflen = sizeof(buf);
414 strncpy( buf, ed->pv, buflen );
415 buf[ buflen-1 ] = '\0';
418 SetEngineState( ed->which, STATE_PONDERING, buf );
420 else if( gameMode == TwoMachinesPlay ) {
421 SetEngineState( ed->which, STATE_THINKING, "" );
423 else if( gameMode == AnalyzeMode || gameMode == AnalyzeFile
424 || (gameMode == IcsObserving && appData.icsEngineAnalyze)) { // [HGM] ICS-analyze
426 int time_secs = ed->time / 100;
427 int time_mins = time_secs / 60;
431 if( ed->an_move_index != 0 && ed->an_move_count != 0 && *ed->hint != '\0' ) {
434 strncpy( mov, ed->hint, sizeof(mov) );
435 mov[ sizeof(mov)-1 ] = '\0';
437 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "[%d] %d/%d: %s [%02d:%02d:%02d]", ed->depth, ed->an_move_index,
438 ed->an_move_count, mov, time_mins / 60, time_mins % 60, time_secs % 60 );
441 SetEngineState( ed->which, STATE_ANALYZING, buf );
444 SetEngineState( ed->which, STATE_IDLE, "" );
448 DoSetWindowText( ed->which, nLabel, s_label );
452 if( ed->time > 0 && ed->nodes > 0 ) {
453 unsigned long nps_100 = ed->nodes / ed->time;
455 if( nps_100 < 100000 ) {
456 snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "NPS: %lu", nps_100 * 100 );
459 snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "NPS: %.1fk", nps_100 / 10.0 );
463 DoSetWindowText( ed->which, nLabelNPS, s_label );
466 if( ed->pv != 0 && *ed->pv != '\0' ) {
472 int time_secs = ed->time / 100;
473 int time_cent = ed->time % 100;
476 if( ed->nodes < 1000000 ) {
477 snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), u64Display, ed->nodes );
480 snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), "%.1fM", u64ToDouble(ed->nodes) / 1000000.0 );
484 h = (gameMode == AnalyzeMode && appData.whitePOV && !WhiteOnMove(currentMove) ? -1 : 1) * ed->score;
486 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "+%.2f", h / 100.0 );
489 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "%.2f", h / 100.0 );
493 snprintf( s_time, sizeof(s_time)/sizeof(s_time[0]), "%d:%02d.%02d", time_secs / 60, time_secs % 60, time_cent );
495 /* Put all together... */
496 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
497 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t", ed->depth );
499 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t%s\t%s\t%s\t", ed->depth, s_score, s_nodes, s_time );
502 buflen = strlen(buf);
504 strncpy( buf + buflen, ed->pv, sizeof(buf) - buflen );
506 buf[ sizeof(buf) - 3 ] = '\0';
508 strcat( buf + buflen, "\r\n" );
511 InsertIntoMemo( ed->which, buf, InsertionPoint(strlen(buf), ed) );
512 strncpy(lastLine[ed->which], buf, MSG_SIZ);
516 SetEngineColorIcon( ed->which );
519 // [HGM] kibitz: write kibitz line; split window for it if necessary
520 void OutputKibitz(int window, char *text)
522 static int currentLineEnd[2];
524 if(!EngineOutputIsUp()) return;
525 if(!opponentKibitzes) { // on first kibitz of game, clear memos
526 DoClearMemo(1); currentLineEnd[1] = 0;
527 if(gameMode == IcsObserving) { DoClearMemo(0); currentLineEnd[0] = 0; }
529 opponentKibitzes = TRUE; // this causes split window DisplayMode in ICS modes.
531 strncpy(text+strlen(text)-1, "\r\n",sizeof(text+strlen(text)-1)); // to not lose line breaks on copying
532 if(gameMode == IcsObserving) {
533 DoSetWindowText(0, nLabel, gameInfo.white);
534 SetIcon( 0, nColorIcon, nColorWhite);
535 SetIcon( 0, nStateIcon, nClear);
537 DoSetWindowText(1, nLabel, gameMode == IcsPlayingBlack ? gameInfo.white : gameInfo.black); // opponent name
538 SetIcon( 1, nColorIcon, gameMode == IcsPlayingBlack ? nColorWhite : nColorBlack);
539 SetIcon( 1, nStateIcon, nClear);
540 if(strstr(text, "\\ ") == text) where = currentLineEnd[window-1]; // continuation line
541 //if(appData.debugMode) fprintf(debugFP, "insert '%s' at %d (end = %d,%d)\n", text, where, currentLineEnd[0], currentLineEnd[1]);
542 InsertIntoMemo(window-1, text, where); // [HGM] multivar: always at top
543 currentLineEnd[window-1] = where + strlen(text);