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"
51 # define _(s) gettext (s)
52 # define N_(s) gettext_noop (s)
71 // called by other front-end
72 void EngineOutputUpdate( FrontEndProgramStats * stats );
73 void OutputKibitz(int window, char *text);
75 // module back-end routines
76 static void VerifyDisplayMode();
77 static void UpdateControls( EngineOutputData * ed );
79 static int lastDepth[2] = { -1, -1 };
80 static int lastForwardMostMove[2] = { -1, -1 };
81 static int engineState[2] = { -1, -1 };
82 static char lastLine[2][MSG_SIZ];
83 static char header[MSG_SIZ];
86 static int scores[MAX_VAR], textEnd[MAX_VAR], curDepth[2], nrVariations[2];
88 extern int initialRulePlies;
90 void MakeEngineOutputTitle()
92 static char buf[MSG_SIZ];
93 static char oldTitle[MSG_SIZ];
95 int count, rule = 2*appData.ruleMoves;
97 snprintf(title, MSG_SIZ, _("Engine Output") );
99 if(!EngineOutputIsUp()) return;
100 // figure out value of 50-move counter
102 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove ) count--;
103 if( count == backwardMostMove ) count -= initialRulePlies;
104 count = currentMove - count;
105 snprintf(buf, MSG_SIZ, "%s (%d reversible plies)", title, count);
106 if(!rule) rule = 100;
107 if(count >= rule - 40 && (!appData.icsActive || gameMode == IcsObserving))
108 safeStrCpy(title, buf, MSG_SIZ);
109 if(!strcmp(oldTitle, title)) return;
110 safeStrCpy(oldTitle, title, MSG_SIZ);
111 SetEngineOutputTitle(title);
114 // back end, due to front-end wrapper for SetWindowText, and new SetIcon arguments
115 void SetEngineState( int which, int state, char * state_data )
117 int x_which = 1 - which;
119 if( engineState[ which ] != state ) {
120 engineState[ which ] = state;
124 SetIcon( which, nStateIcon, nThinking );
125 if( engineState[ x_which ] == STATE_THINKING ) {
126 SetEngineState( x_which, STATE_IDLE, "" );
129 case STATE_PONDERING:
130 SetIcon( which, nStateIcon, nPondering );
132 case STATE_ANALYZING:
133 SetIcon( which, nStateIcon, nAnalyzing );
136 SetIcon( which, nStateIcon, nClear );
141 if( state_data != 0 ) {
142 DoSetWindowText( which, nStateData, state_data );
146 // back end, now the front-end wrapper ClearMemo is used, and ed no longer contains handles.
147 void SetProgramStats( FrontEndProgramStats * stats ) // now directly called by back-end
150 int clearMemo = FALSE;
151 int which, depth, multi;
154 SetEngineState( 0, STATE_IDLE, "" );
155 SetEngineState( 1, STATE_IDLE, "" );
159 if(gameMode == IcsObserving && !appData.icsEngineAnalyze)
160 return; // [HGM] kibitz: shut up engine if we are observing an ICS game
162 which = stats->which;
163 depth = stats->depth;
165 if( which < 0 || which > 1 || depth < 0 || stats->time < 0 || stats->pv == 0 ) {
169 if( !EngineOutputDialogExists() ) {
177 ed.nodes = stats->nodes;
178 ed.score = stats->score;
179 ed.time = stats->time;
181 ed.hint = stats->hint;
182 ed.an_move_index = stats->an_move_index;
183 ed.an_move_count = stats->an_move_count;
185 /* Get target control. [HGM] this is moved to front end, which get them from a table */
187 ed.name = first.tidy;
190 ed.name = second.tidy;
193 if( ed.pv != 0 && ed.pv[0] == ' ' ) {
194 if( strncmp( ed.pv, " no PV", 6 ) == 0 ) { /* Hack on hack! :-O */
199 /* Clear memo if needed */
200 if( lastDepth[which] > depth || (lastDepth[which] == depth && depth <= 1 && ed.pv[0]) ) { // no reason to clear if we won't add line
204 if( lastForwardMostMove[which] != forwardMostMove ) {
209 DoClearMemo(which); nrVariations[which] = 0;
210 header[0] = NULLCHAR;
211 if(gameMode == AnalyzeMode && (multi = MultiPV(&first)) >= 0) {
212 snprintf(header, MSG_SIZ, "\t%s viewpoint\t\tfewer / Multi-PV setting = %d / more\n",
213 appData.whitePOV || appData.scoreWhite ? "white" : "mover", first.option[multi].value);
214 InsertIntoMemo( which, header, 0);
216 if(appData.ponderNextMove && lastLine[which][0]) {
217 InsertIntoMemo( which, lastLine[which], 0 );
218 InsertIntoMemo( which, "\n", 0 );
223 lastDepth[which] = depth == 1 && ed.nodes == 0 ? 0 : depth; // [HGM] info-line kudge
224 lastForwardMostMove[which] = forwardMostMove;
226 UpdateControls( &ed );
229 #define ENGINE_COLOR_WHITE 'w'
230 #define ENGINE_COLOR_BLACK 'b'
231 #define ENGINE_COLOR_UNKNOWN ' '
234 static char GetEngineColor( int which )
236 char result = ENGINE_COLOR_UNKNOWN;
238 if( which == 0 || which == 1 ) {
239 ChessProgramState * cps;
242 case MachinePlaysBlack:
243 case IcsPlayingBlack:
244 result = ENGINE_COLOR_BLACK;
246 case MachinePlaysWhite:
247 case IcsPlayingWhite:
248 result = ENGINE_COLOR_WHITE;
252 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
254 case TwoMachinesPlay:
255 cps = (which == 0) ? &first : &second;
256 result = cps->twoMachinesColor[0];
257 result = result == 'w' ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
259 default: ; // does not happen, but suppresses pedantic warnings
267 static char GetActiveEngineColor()
269 char result = ENGINE_COLOR_UNKNOWN;
271 if( gameMode == TwoMachinesPlay ) {
272 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
279 static int IsEnginePondering( int which )
284 case MachinePlaysBlack:
285 case IcsPlayingBlack:
286 if( WhiteOnMove(forwardMostMove) ) result = TRUE;
288 case MachinePlaysWhite:
289 case IcsPlayingWhite:
290 if( ! WhiteOnMove(forwardMostMove) ) result = TRUE;
292 case TwoMachinesPlay:
293 if( GetActiveEngineColor() != ENGINE_COLOR_UNKNOWN ) {
294 if( GetEngineColor( which ) != GetActiveEngineColor() ) result = TRUE;
297 default: ; // does not happen, but suppresses pedantic warnings
304 static void SetDisplayMode( int mode )
306 if( windowMode != mode ) {
309 ResizeWindowControls( mode );
314 static void VerifyDisplayMode()
318 /* Get proper mode for current game */
320 case IcsObserving: // [HGM] ICS analyze
321 if(!appData.icsEngineAnalyze) return;
324 case MachinePlaysWhite:
325 case MachinePlaysBlack:
328 case IcsPlayingWhite:
329 case IcsPlayingBlack:
330 mode = appData.zippyPlay && opponentKibitzes; // [HGM] kibitz
332 case TwoMachinesPlay:
340 SetDisplayMode( mode );
343 // back end. Determine what icon to set in the color-icon field, and print it
344 void SetEngineColorIcon( int which )
346 char color = GetEngineColor(which);
349 if( color == ENGINE_COLOR_BLACK )
351 else if( color == ENGINE_COLOR_WHITE )
354 nicon = nColorUnknown;
356 SetIcon( which, nColorIcon, nicon );
359 #define MAX_NAME_LENGTH 32
361 // [HGM] multivar: sort Thinking Output within one depth on score
363 static int InsertionPoint( int len, EngineOutputData * ed )
365 int i, offs = 0, newScore = ed->score, n = ed->which;
367 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
368 newScore = 1e6; // info lines inserted on top
369 if(ed->depth != curDepth[n]) { // depth has changed
370 curDepth[n] = ed->depth;
371 nrVariations[n] = 0; // throw away everything we had
373 // loop through all lines. Note even / odd used for different panes
374 for(i=nrVariations[n]-2; i>=0; i-=2) {
375 // put new item behind those we haven't looked at
377 textEnd[i+n+2] = offs + len;
378 scores[i+n+2] = newScore;
379 if(newScore < scores[i+n]) break;
380 // if it had higher score as previous, move previous in stead
381 scores[i+n+2] = scores[i+n];
382 textEnd[i+n+2] = textEnd[i+n] + len;
386 textEnd[n] = offs + len;
387 scores[n] = newScore;
389 nrVariations[n] += 2;
390 return offs + (gameMode == AnalyzeMode)*strlen(header);
394 // pure back end, now SetWindowText is called via wrapper DoSetWindowText
395 static void UpdateControls( EngineOutputData * ed )
397 // int isPondering = FALSE;
399 char s_label[MAX_NAME_LENGTH + 32];
401 char * name = ed->name;
404 if( name == 0 || *name == '\0' ) {
408 strncpy( s_label, name, MAX_NAME_LENGTH );
409 s_label[ MAX_NAME_LENGTH-1 ] = '\0';
411 #ifdef SHOW_PONDERING
412 if( IsEnginePondering( ed->which ) ) {
417 if( ed->hint != 0 && *ed->hint != '\0' ) {
418 strncpy( buf, ed->hint, sizeof(buf) );
419 buf[sizeof(buf)-1] = '\0';
421 else if( ed->pv != 0 && *ed->pv != '\0' ) {
422 char * sep = strchr( ed->pv, ' ' );
423 int buflen = sizeof(buf);
426 buflen = sep - ed->pv + 1;
427 if( buflen > sizeof(buf) ) buflen = sizeof(buf);
430 strncpy( buf, ed->pv, buflen );
431 buf[ buflen-1 ] = '\0';
434 SetEngineState( ed->which, STATE_PONDERING, buf );
436 else if( gameMode == TwoMachinesPlay ) {
437 SetEngineState( ed->which, STATE_THINKING, "" );
439 else if( gameMode == AnalyzeMode || gameMode == AnalyzeFile
440 || (gameMode == IcsObserving && appData.icsEngineAnalyze)) { // [HGM] ICS-analyze
442 int time_secs = ed->time / 100;
443 int time_mins = time_secs / 60;
447 if( ed->an_move_index != 0 && ed->an_move_count != 0 && *ed->hint != '\0' ) {
450 strncpy( mov, ed->hint, sizeof(mov) );
451 mov[ sizeof(mov)-1 ] = '\0';
453 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "[%d] %d/%d: %s [%02d:%02d:%02d]", ed->depth, ed->an_move_index,
454 ed->an_move_count, mov, time_mins / 60, time_mins % 60, time_secs % 60 );
457 SetEngineState( ed->which, STATE_ANALYZING, buf );
460 SetEngineState( ed->which, STATE_IDLE, "" );
464 DoSetWindowText( ed->which, nLabel, s_label );
468 if( ed->time > 0 && ed->nodes > 0 ) {
469 unsigned long nps_100 = ed->nodes / ed->time;
471 if( nps_100 < 100000 ) {
472 snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "NPS: %lu", nps_100 * 100 );
475 snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "NPS: %.1fk", nps_100 / 10.0 );
479 DoSetWindowText( ed->which, nLabelNPS, s_label );
482 if( ed->pv != 0 && *ed->pv != '\0' ) {
488 int time_secs = ed->time / 100;
489 int time_cent = ed->time % 100;
492 if( ed->nodes < 1000000 ) {
493 snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), u64Display, ed->nodes );
496 snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), "%.1fM", u64ToDouble(ed->nodes) / 1000000.0 );
500 h = ((gameMode == AnalyzeMode && appData.whitePOV || appData.scoreWhite) && !WhiteOnMove(currentMove) ? -1 : 1) * ed->score;
502 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "+%.2f", h / 100.0 );
505 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "%.2f", h / 100.0 );
509 snprintf( s_time, sizeof(s_time)/sizeof(s_time[0]), "%d:%02d.%02d", time_secs / 60, time_secs % 60, time_cent );
511 /* Put all together... */
512 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
513 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t", ed->depth );
515 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t%s\t%s\t%s\t", ed->depth, s_score, s_nodes, s_time );
518 buflen = strlen(buf);
520 strncpy( buf + buflen, ed->pv, sizeof(buf) - buflen );
522 buf[ sizeof(buf) - 3 ] = '\0';
524 strcat( buf + buflen, "\r\n" );
527 InsertIntoMemo( ed->which, buf, InsertionPoint(strlen(buf), ed) );
528 strncpy(lastLine[ed->which], buf, MSG_SIZ);
532 SetEngineColorIcon( ed->which );
535 // [HGM] kibitz: write kibitz line; split window for it if necessary
536 void OutputKibitz(int window, char *text)
538 static int currentLineEnd[2];
540 if(!EngineOutputIsUp()) return;
541 if(!opponentKibitzes) { // on first kibitz of game, clear memos
542 DoClearMemo(1); currentLineEnd[1] = 0;
543 if(gameMode == IcsObserving) { DoClearMemo(0); currentLineEnd[0] = 0; }
545 opponentKibitzes = TRUE; // this causes split window DisplayMode in ICS modes.
547 strncpy(text+strlen(text)-1, "\r\n",sizeof(text+strlen(text)-1)); // to not lose line breaks on copying
548 if(gameMode == IcsObserving) {
549 DoSetWindowText(0, nLabel, gameInfo.white);
550 SetIcon( 0, nColorIcon, nColorWhite);
551 SetIcon( 0, nStateIcon, nClear);
553 DoSetWindowText(1, nLabel, gameMode == IcsPlayingBlack ? gameInfo.white : gameInfo.black); // opponent name
554 SetIcon( 1, nColorIcon, gameMode == IcsPlayingBlack ? nColorWhite : nColorBlack);
555 SetIcon( 1, nStateIcon, nClear);
556 if(strstr(text, "\\ ") == text) where = currentLineEnd[window-1]; // continuation line
557 //if(appData.debugMode) fprintf(debugFP, "insert '%s' at %d (end = %d,%d)\n", text, where, currentLineEnd[0], currentLineEnd[1]);
558 InsertIntoMemo(window-1, text, where); // [HGM] multivar: always at top
559 currentLineEnd[window-1] = where + strlen(text);